'''
梯度下降算法
Batch Gradient Descent
Stochastic Gradient Descent SGD
'''
__author__ = 'epleone'
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import sys
使用随机数种子, 让每次的随机数生成相同,方便调试
np.random.seed(111111111)
class GradientDescent(object):
eps = 1.0e-8
max_iter = 1000000 # 暂时不需要
dim = 1
func_args = [2.1, 2.7] # [w_0, .., w_dim, b]
def init(self, func_arg=None, N=1000):
self.data_num = N
if func_arg is not None:
self.FuncArgs = func_arg
self._getData()
def _getData(self):
x = 20 * (np.random.rand(self.data_num, self.dim) - 0.5)
b_1 = np.ones((self.data_num, 1), dtype=np.float)
x = np.concatenate((x