【发布时间】:2016-03-04 09:54:17
【问题描述】:
我正在尝试实现具有两个约束的随机梯度下降,因此不能使用 scikit-learn。不幸的是,在没有这两个限制的情况下,我已经在使用常规 SGD 苦苦挣扎。训练集上的损失(平方损失)在一些迭代中下降,但在一段时间后开始增加,如图所示。 这些是我使用的功能:
def loss_prime_simple(w,node,feature,data):
x = data[3]
y = data[2]
x_f = x[node][feature]
y_node = y[node]
ret = (y_node - w[feature] * x_f) * (-x_f)
return ret
def update_weights(w,data,predecs,children,node, learning_rate):
len_features = len(data[3][0])
w_new = np.zeros(len_features)
for feature_ in range(len_features):
w_new[feature_] = loss_prime_simple(w,node,feature_,data)
return w - learning_rate * w_new
def loss_simple(w,data):
y_p = data[2]
x = data[3]
return ((y_p - np.dot(w,np.array(x).T)) ** 2).sum()
这显示了具有两种不同学习率(0.001、0.0001)http://postimg.org/image/43nbmh8x5/的训练集上的损失
任何人都可以找到错误或有如何调试的建议吗? 谢谢
编辑:
正如 lejlot 指出的那样,拥有数据会很好。 这是我用于 x 的数据(单个样本):http://textuploader.com/5x0f1
y=2
这让我们失去了这个:http://postimg.org/image/o9d97kt9v/
更新后的代码:
def loss_prime_simple(w,node,feature,data):
x = data[3]
y = data[2]
x_f = x[node][feature]
y_node = y[node]
return -(y_node - w[feature] * x_f) * x_f
def update_weights(w,data,predecs,children,node, learning_rate):
len_features = len(data[3][0])
w_new = np.zeros(len_features)
for feature_ in range(len_features):
w_new[feature_] = loss_prime_simple(w,node,feature_,data)
return w - learning_rate * w_new
def loss_simple2(w,data):
y_p = data[2]
x = data[3]
return ((y_p - np.dot(w,np.array(x).T)) ** 2).sum()
import numpy as np
X = [#put array from http://textuploader.com/5x0f1 here]
y = [2]
data = None, None, y, X
w = np.random.rand(4096)
a = [ loss_simple2(w, data) ]
for _ in range(200):
for j in range(X.shape[0]):
w = update_weights(w,data,None,None,j, 0.0001)
a.append( loss_simple2(w, data) )
from matplotlib import pyplot as plt
plt.figure()
plt.plot(a)
plt.show()
【问题讨论】:
标签: python machine-learning gradient-descent