【发布时间】:2016-11-28 09:47:16
【问题描述】:
我正在尝试使用 2 个感知器网络创建一个 XOR 门,但由于某种原因,网络没有学习,当我在图中绘制误差变化时,误差达到静态水平并在该区域振荡。
目前我没有对网络添加任何偏见。
import numpy as np
def S(x):
return 1/(1+np.exp(-x))
win = np.random.randn(2,2)
wout = np.random.randn(2,1)
eta = 0.15
# win = [[1,1], [2,2]]
# wout = [[1],[2]]
obj = [[0,0],[1,0],[0,1],[1,1]]
target = [0,1,1,0]
epoch = int(10000)
emajor = ""
for r in range(0,epoch):
for xy in range(len(target)):
tar = target[xy]
fdata = obj[xy]
fdata = S(np.dot(1,fdata))
hnw = np.dot(fdata,win)
hnw = S(np.dot(fdata,win))
out = np.dot(hnw,wout)
out = S(out)
diff = tar-out
E = 0.5 * np.power(diff,2)
emajor += str(E[0]) + ",\n"
delta_out = (out-tar)*(out*(1-out))
nindelta_out = delta_out * eta
wout_change = np.dot(nindelta_out[0], hnw)
for x in range(len(wout_change)):
change = wout_change[x]
wout[x] -= change
delta_in = np.dot(hnw,(1-hnw)) * np.dot(delta_out[0], wout)
nindelta_in = eta * delta_in
for x in range(len(nindelta_in)):
midway = np.dot(nindelta_in[x][0], fdata)
for y in range(len(win)):
win[y][x] -= midway[y]
f = open('xor.csv','w')
f.write(emajor) # python will convert \n to os.linesep
f.close() # you can omit in most cases as the destructor will call it
这是随学习轮数变化的误差。它是否正确?红色线是我期望错误应该如何变化的线。
我在代码中做错了什么?因为我似乎无法弄清楚导致错误的原因。非常感谢帮助。
提前致谢
【问题讨论】:
-
您可能对我的博客文章感兴趣:XOR tutorial with TensorFlow
标签: python numpy machine-learning neural-network artificial-intelligence