我实施了NN,但它没有用。有人可以帮我弄明白,问题是什么?我尝试使用简单的,和/或xor函数。成本函数似乎减少,但在分类时失败,我将迭代设置为50,000,更改alpha的数量(0.1,0.01,0.001),代码二解,矢量化方式和逐个观察。
这是笔记本的观看者:http://nbviewer.ipython.org/gist/Abreu0101/05f6fe35b08eac1162c7
数据集:
def loadDataSet():
X = np.array([[1,1,1],[1,0,1],[1,1,0],[1,0,0]])
y = np.array([[1],[0],[0],[0]])
return X,y
代码:
X,y = loadDataSet()
n_observations,n_features = X.shape
weights_1 = np.random.rand(2,3)
weights_2 = np.random.rand(1,3)
maxIter = 90000
for currentIter in range(maxIter):
costError(weights_1,weights_2,currentIter)
#FeedFodward
z_1 = X.dot(weights_1.T)
a_1 = np.hstack((np.ones((n_observations,1)),sigmoid(z_1)))
z_2 = a_1.dot(weights_2.T)
a_2 = sigmoid(z_2)
#BackPropagation
d_2 = (y - a_2) * sigmoid(z_2,derivate=True)
d_1 = (d_2.dot(weights_2))[:,1:]
alpha = 1 #Learning Rate
weights_2 = weights_2 + alpha * d_2.T.dot(a_1)
weights_1 = weights_1 + alpha * d_1.T.dot(X)
成本函数:
def costError(w_1,w_2,currentIter):
z_1 = X.dot(w_1.T)
a_1 = np.hstack((np.ones((n_observations,1)),sigmoid(z_1)))
z_2 = a_1.dot(w_2.T)
a_2 = sigmoid(z_2)
sumError = np.sum(a_2)
print("Error : %f , Iter: %d"%(sumError,currentIter))
提前致谢。
答案 0 :(得分:0)
1)将d_2和d_1更改为:
d_2 = (y - a_2)# * sigmoid(z_2,derivate=True)
d_1 = (d_2.dot(weights_2))[:,1:]*sigmoid(z_1,derivate=True)
事实上,我不知道为什么* sigmoid(z_2,derivate=True)
需要被注释掉,但没有它会更快收敛,并且我看到的nn的每个实现都是在没有乘以输出的sigmoid函数的梯度的情况下实现的水平。
2)sumError = np.sum(a_2)
是什么意思?您需要考虑预测值和实际值之间的差异:
sumError = np.mean((y-a_2)**2)
为此损失函数计算您的衍生工具。