参考资料:
不想写解释了,以后再补。
我的参数矩阵跟别人的不太一样,代码:
import numpy as np
import matplotlib.pyplot as plt
def gradient_descent(x, y, w, col, alpha=10,times=100,eps=1e-8): # 调参有点难受
for i in range(times):
a = 1/(1+np.exp(-w.T*x))
dJ_dw = 1/col*x*(a.T-y.T)
w -= alpha*dJ_dw
if np.sum(np.abs(dJ_dw))<eps:
print('第{}次迭代后的结果'.format(i))
break
def recover_w(x_mean, x_std, w):
std = np.append(np.ones((1, 1)), x_std, axis=0)
mean = np.append(np.zeros((1, 1)), x_mean, axis=0)
w = np.array(w)
w /= std
w[0][0] -= np.sum(mean*w)
return w
x = np.loadtxt('ex4x.dat')
y = np.loadtxt('ex4y.dat')
x_mean = np.mean(x,0)
x_std = np.std(x,0)
x = (x-x_mean)/x_std
x=x.T
row,col=x.shape
b = np.ones((1, col))
x = np.append(b, x, axis=0)
y.resize((1, 80))
w = np.mat(np.zeros((row+1, 1)))
x = np.mat(x)
y = np.mat(y)
gradient_descent(x, y, w, col)
x_mean = x_mean.T
x_std = x_std.T
x_mean.resize((row, 1))
x_std.resize((row, 1))
w = recover_w(x_mean, x_std, w)
print(w)
输出:
第23次迭代后的结果
[[-16.37874363]
[ 0.14834078]
[ 0.15890845]]
完全没想到不归一化,完全收敛不了,应该是指数函数计算机算的误差太大了,归一化之后还要恢复系数,写完之后差点忘了,导致还以为收敛之后结果不对。