Python实现的递归神经网络简单示例
本文实例讲述了Python实现的递归神经网络。分享给大家供大家参考,具体如下:
#RecurrentNeuralNetworks importcopy,numpyasnp np.random.seed(0) #computesigmoidnonlinearity defsigmoid(x): output=1/(1+np.exp(-x)) returnoutput #convertoutputofsigmoidfunctiontoitsderivative defsigmoid_output_to_derivative(output): returnoutput*(1-output) #trainingdatasetgeneration int2binary={} binary_dim=8 largest_number=pow(2,binary_dim) binary=np.unpackbits( np.array([range(largest_number)],dtype=np.uint8).T,axis=1) foriinrange(largest_number): int2binary[i]=binary[i] #inputvariables alpha=0.1 input_dim=2 hidden_dim=16 output_dim=1 #initializeneuralnetworkweights synapse_0=2*np.random.random((input_dim,hidden_dim))-1 synapse_1=2*np.random.random((hidden_dim,output_dim))-1 synapse_h=2*np.random.random((hidden_dim,hidden_dim))-1 synapse_0_update=np.zeros_like(synapse_0) synapse_1_update=np.zeros_like(synapse_1) synapse_h_update=np.zeros_like(synapse_h) #traininglogic forjinrange(10000): #generateasimpleadditionproblem(a+b=c) a_int=np.random.randint(largest_number/2)#intversion a=int2binary[a_int]#binaryencoding b_int=np.random.randint(largest_number/2)#intversion b=int2binary[b_int]#binaryencoding #trueanswer c_int=a_int+b_int c=int2binary[c_int] #wherewe'llstoreourbestguess(binaryencoded) d=np.zeros_like(c) overallError=0 layer_2_deltas=list() layer_1_values=list() layer_1_values.append(np.zeros(hidden_dim)) #movingalongthepositionsinthebinaryencoding forpositioninrange(binary_dim): #generateinputandoutput X=np.array([[a[binary_dim-position-1],b[binary_dim-position-1]]]) y=np.array([[c[binary_dim-position-1]]]).T #hiddenlayer(input~+prev_hidden) layer_1=sigmoid(np.dot(X,synapse_0)+np.dot(layer_1_values[-1],synapse_h)) #outputlayer(newbinaryrepresentation) layer_2=sigmoid(np.dot(layer_1,synapse_1)) #didwemiss?...ifso,byhowmuch? layer_2_error=y-layer_2 layer_2_deltas.append((layer_2_error)*sigmoid_output_to_derivative(layer_2)) overallError+=np.abs(layer_2_error[0]) #decodeestimatesowecanprint(itout) d[binary_dim-position-1]=np.round(layer_2[0][0]) #storehiddenlayersowecanuseitinthenexttimestep layer_1_values.append(copy.deepcopy(layer_1)) future_layer_1_delta=np.zeros(hidden_dim) forpositioninrange(binary_dim): X=np.array([[a[position],b[position]]]) layer_1=layer_1_values[-position-1] prev_layer_1=layer_1_values[-position-2] #erroratoutputlayer layer_2_delta=layer_2_deltas[-position-1] #errorathiddenlayer layer_1_delta=(future_layer_1_delta.dot(synapse_h.T)+layer_2_delta.dot(synapse_1.T))*sigmoid_output_to_derivative(layer_1) #let'supdateallourweightssowecantryagain synapse_1_update+=np.atleast_2d(layer_1).T.dot(layer_2_delta) synapse_h_update+=np.atleast_2d(prev_layer_1).T.dot(layer_1_delta) synapse_0_update+=X.T.dot(layer_1_delta) future_layer_1_delta=layer_1_delta synapse_0+=synapse_0_update*alpha synapse_1+=synapse_1_update*alpha synapse_h+=synapse_h_update*alpha synapse_0_update*=0 synapse_1_update*=0 synapse_h_update*=0 #print(outprogress) ifj%1000==0: print("Error:"+str(overallError)) print("Pred:"+str(d)) print("True:"+str(c)) out=0 forindex,xinenumerate(reversed(d)): out+=x*pow(2,index) print(str(a_int)+"+"+str(b_int)+"="+str(out)) print("------------")
运行输出:
Error:[3.45638663] Pred:[00000001] True:[01000101] 9+60=1 ------------ Error:[3.63389116] Pred:[11111111] True:[00111111] 28+35=255 ------------ Error:[3.91366595] Pred:[01001000] True:[10100000] 116+44=72 ------------ Error:[3.72191702] Pred:[11011111] True:[01001101] 4+73=223 ------------ Error:[3.5852713] Pred:[00001000] True:[01010010] 71+11=8 ------------ Error:[2.53352328] Pred:[10100010] True:[11000010] 81+113=162 ------------ Error:[0.57691441] Pred:[01010001] True:[01010001] 81+0=81 ------------ Error:[1.42589952] Pred:[10000001] True:[10000001] 4+125=129 ------------ Error:[0.47477457] Pred:[00111000] True:[00111000] 39+17=56 ------------ Error:[0.21595037] Pred:[00001110] True:[00001110] 11+3=14 ------------
英文原文:https://iamtrask.github.io/2015/11/15/anyone-can-code-lstm/
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。