# RNN 神经网络及其变体网络——实验楼学习笔记

def sigmoid(x):
y = 1/(1+np.exp(-x))
return y
def dersigmoid(x):
y=x*(1-x)
return y

step = 0.1
ipnumber = 2
hdnumber = 32
opnumber = 2

neu_i2h = 2*np.random.random((ipnumber,hdnumber)) - 1
neu_h2o = 2*np.random.random((hdnumber,opnumber)) - 1
neu_h2h = 2*np.random.random((hdnumber,hdnumber)) - 1

neu_i2hN = np.zeros_like(neu_i2h)
neu_h2oN = np.zeros_like(neu_h2o)
neu_h2hN = np.zeros_like(neu_h2h)

### 训练数据集初始化

i2b = {}
bdigit = 8
MAXnumber = pow(2,bdigit)
bin = np.unpackbits(np.array([range(MAXnumber)],dtype=np.uint8).T,axis=1)
for i in range(MAXnumber):
i2b[i] = bin[i]
for j in range(20000):
a_decimal = np.random.randint(MAXnumber / 2)
b_decimal = np.random.randint(MAXnumber / 2)
c_decimal = a_decimal + b_decimal
a = i2b[a_decimal]
b = i2b[b_decimal]
c = i2b[c_decimal]
binary = np.zeros_like(c)
aError = 0
oplayer_der = list()
hdlayer_val = list()
hdlayer_val.append(np.zeros(hdnumber))

for locate in range(bdigit):
X = np.array([[a[bdigit - locate - 1],b[bdigit - locate - 1]]])
Y = np.array([[c[bdigit - locate - 1]]]).T
hdlayer = sigmoid(np.dot(X,neu_i2h) + np.dot(hdlayer_val[-1],neu_h2h))
oplayer = sigmoid(np.dot(hdlayer,neu_h2o))
oplayer_error = Y - oplayer
oplayer_der.append((oplayer_error)*dersigmoid(oplayer))
aError += np.abs(oplayer_error[0])#首先计算出输出层和真是值的误差  oplayer_erroroplayer_error  ，并将每一次训练输出值的导数进行储存，最后将句对误差进行相加。

binary[bdigit - locate - 1] = np.round(oplayer[0][0])

hdlayer_val.append(copy.deepcopy(hdlayer))

Fhdlayer_dels = np.zeros(hdnumber)
for locate in range(bdigit):
X = np.array([[a[locate],b[locate]]])
hdlayer = hdlayer_val[-locate-1]
hdlayer_pre = hdlayer_val[-locate-2]#反向将数据进行检索，以便于之后的误差的计算。通过前一个循环得到的隐藏层的数组，取出相应的隐藏层的值准备误差运算。

oplayer_dels = oplayer_der[-locate-1]
hdlayer_dels = (Fhdlayer_dels.dot(neu_h2h.T) + oplayer_dels.dot(neu_h2o.T)) * dersigmoid(hdlayer)#统计得到输出层和隐藏层的误差，以便权值进行更新。这里得到了两个网络层的误差，通过这两组误差将权值进行更新。

neu_h2oN += np.atleast_2d(hdlayer).T.dot(oplayer_dels)
neu_h2hN += np.atleast_2d(hdlayer_pre).T.dot(hdlayer_dels)
neu_i2hN += X.T.dot(hdlayer_dels)
Fhdlayer_dels = hdlayer_dels

neu_i2h += neu_i2hN * step
neu_h2o += neu_h2oN * step
neu_h2h += neu_h2hN * step

neu_i2hN *= 0
neu_h2oN *= 0
neu_h2hN *= 0#进行权值更新

08-06 752

06-09 5877
10-16 4306
07-05 1067
12-08 1605
03-10 613
02-11 416
07-03 911
09-22 466
10-05 2204
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客