【发布时间】:2015-08-04 18:08:33
【问题描述】:
我实现了一个 NN,但它不起作用。有人可以帮我弄清楚,是什么问题?我尝试使用简单的、and、or、xor 函数。代价函数好像变小了,但是在分类的那一刻失败了,我把迭代次数设置为50000,改变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))
提前致谢。
【问题讨论】:
标签: python machine-learning neural-network ipython-notebook