深度学习——02、深度学习入门——python实现RNN算法
生活随笔
收集整理的這篇文章主要介紹了
深度学习——02、深度学习入门——python实现RNN算法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實際問題:二進制加法
1.遵循加法的規則
2.逢二進一
當前位的加法還要考慮前一位的進位。
完整代碼
import copy, numpy as np np.random.seed(0)# compute sigmoid nonlinearity def sigmoid(x):output = 1/(1+np.exp(-x))return output# convert output of sigmoid function to its derivative def sigmoid_output_to_derivative(output):return output*(1-output)# training dataset generation int2binary = {} binary_dim = 8largest_number = pow(2,binary_dim) binary = np.unpackbits(np.array([range(largest_number)],dtype=np.uint8).T,axis=1) for i in range(largest_number):int2binary[i] = binary[i]# input variables alpha = 0.1 input_dim = 2 hidden_dim = 16 output_dim = 1# initialize neural network weights 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)) - 1synapse_0_update = np.zeros_like(synapse_0) synapse_1_update = np.zeros_like(synapse_1) synapse_h_update = np.zeros_like(synapse_h)# training logic for j in range(10000):# generate a simple addition problem (a + b = c)a_int = np.random.randint(largest_number/2) # int versiona = int2binary[a_int] # binary encodingb_int = np.random.randint(largest_number/2) # int versionb = int2binary[b_int] # binary encoding# true answerc_int = a_int + b_intc = int2binary[c_int]# where we'll store our best guess (binary encoded)d = np.zeros_like(c)overallError = 0layer_2_deltas = list()layer_1_values = list()layer_1_values.append(np.zeros(hidden_dim))# moving along the positions in the binary encodingfor position in range(binary_dim):# generate input and outputX = np.array([[a[binary_dim - position - 1],b[binary_dim - position - 1]]])y = np.array([[c[binary_dim - position - 1]]]).T# hidden layer (input ~+ prev_hidden)layer_1 = sigmoid(np.dot(X,synapse_0) + np.dot(layer_1_values[-1],synapse_h))# output layer (new binary representation)layer_2 = sigmoid(np.dot(layer_1,synapse_1))# did we miss?... if so, by how much?layer_2_error = y - layer_2layer_2_deltas.append((layer_2_error)*sigmoid_output_to_derivative(layer_2))overallError += np.abs(layer_2_error[0])# decode estimate so we can print it outd[binary_dim - position - 1] = np.round(layer_2[0][0])# store hidden layer so we can use it in the next timesteplayer_1_values.append(copy.deepcopy(layer_1))future_layer_1_delta = np.zeros(hidden_dim)for position in range(binary_dim):X = np.array([[a[position],b[position]]])layer_1 = layer_1_values[-position-1]prev_layer_1 = layer_1_values[-position-2]# error at output layerlayer_2_delta = layer_2_deltas[-position-1]# error at hidden layerlayer_1_delta = (future_layer_1_delta.dot(synapse_h.T) + layer_2_delta.dot(synapse_1.T)) * sigmoid_output_to_derivative(layer_1)# let's update all our weights so we can try againsynapse_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_deltasynapse_0 += synapse_0_update * alphasynapse_1 += synapse_1_update * alphasynapse_h += synapse_h_update * alpha synapse_0_update *= 0synapse_1_update *= 0synapse_h_update *= 0# print out progressif(j % 1000 == 0):print ("Error:" + str(overallError))print ("Pred:" + str(d))print ("True:" + str(c))out = 0for index,x in enumerate(reversed(d)):out += x*pow(2,index)print (str(a_int) + " + " + str(b_int) + " = " + str(out))print ("------------")代碼分析
激活函數及其求導:
# compute sigmoid nonlinearity # 激活函數sigmoid def sigmoid(x):output = 1/(1+np.exp(-x))return output# convert output of sigmoid function to its derivative # 反向傳播sigmoid的導數值 def sigmoid_output_to_derivative(output):return output*(1-output)十進制與二進制的對應關系:
# training dataset generation int2binary = {} binary_dim = 8largest_number = pow(2,binary_dim) binary = np.unpackbits(np.array([range(largest_number)],dtype=np.uint8).T,axis=1) for i in range(largest_number):int2binary[i] = binary[i]網絡初始化:
# input variables alpha = 0.1 input_dim = 2 # 定義輸入的維度,即兩個數 hidden_dim = 16 # 16個中間神經元 output_dim = 1 # 定義輸出的維度,即一個數
初始化w0、w1、wh:
開始迭代:
# training logic for j in range(10000):隨機找a、b的值,要小于最大值的一半:
# generate a simple addition problem (a + b = c)a_int = np.random.randint(largest_number/2) # int versiona = int2binary[a_int] # binary encodingb_int = np.random.randint(largest_number/2) # int versionb = int2binary[b_int] # binary encoding得出c并轉換成二進制數:
# true answerc_int = a_int + b_intc = int2binary[c_int] # where we'll store our best guess (binary encoded)d = np.zeros_like(c)overallError = 0layer_2_deltas = list()layer_1_values = list()# L1層迭代的值layer_1_values.append(np.zeros(hidden_dim))# 第一次迭代的時候先全部初始化為0前向傳播遍歷每一位運算:
# moving along the positions in the binary encodingfor position in range(binary_dim): # generate input and outputX = np.array([[a[binary_dim - position - 1],b[binary_dim - position - 1]]])y = np.array([[c[binary_dim - position - 1]]]).TL1、L2層的值:
# hidden layer (input ~+ prev_hidden)layer_1 = sigmoid(np.dot(X,synapse_0) + np.dot(layer_1_values[-1],synapse_h))# output layer (new binary representation)layer_2 = sigmoid(np.dot(layer_1,synapse_1)) # did we miss?... if so, by how much?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])
實際的預測值:
因為L1層循環的原因,要保存其值:
# store hidden layer so we can use it in the next timesteplayer_1_values.append(copy.deepcopy(layer_1))反向傳播遍歷每一位運算:
for position in range(binary_dim): X = np.array([[a[position],b[position]]])layer_1 = layer_1_values[-position-1]prev_layer_1 = layer_1_values[-position-2]更新權重:
# error at output layerlayer_2_delta = layer_2_deltas[-position-1]# error at hidden layerlayer_1_delta = (future_layer_1_delta.dot(synapse_h.T) + layer_2_delta.dot(synapse_1.T)) * sigmoid_output_to_derivative(layer_1)# let's update all our weights so we can try againsynapse_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
參數更新:
打印結果:
# print out progressif(j % 1000 == 0):print ("Error:" + str(overallError))print ("Pred:" + str(d))print ("True:" + str(c))out = 0for index,x in enumerate(reversed(d)):out += x*pow(2,index)print (str(a_int) + " + " + str(b_int) + " = " + str(out))print ("------------")運行結果
Error:[3.45638663] Pred:[0 0 0 0 0 0 0 1] True:[0 1 0 0 0 1 0 1] 9 + 60 = 1 ------------ Error:[3.63389116] Pred:[1 1 1 1 1 1 1 1] True:[0 0 1 1 1 1 1 1] 28 + 35 = 255 ------------ Error:[3.91366595] Pred:[0 1 0 0 1 0 0 0] True:[1 0 1 0 0 0 0 0] 116 + 44 = 72 ------------ Error:[3.72191702] Pred:[1 1 0 1 1 1 1 1] True:[0 1 0 0 1 1 0 1] 4 + 73 = 223 ------------ Error:[3.5852713] Pred:[0 0 0 0 1 0 0 0] True:[0 1 0 1 0 0 1 0] 71 + 11 = 8 ------------ Error:[2.53352328] Pred:[1 0 1 0 0 0 1 0] True:[1 1 0 0 0 0 1 0] 81 + 113 = 162 ------------ Error:[0.57691441] Pred:[0 1 0 1 0 0 0 1] True:[0 1 0 1 0 0 0 1] 81 + 0 = 81 ------------ Error:[1.42589952] Pred:[1 0 0 0 0 0 0 1] True:[1 0 0 0 0 0 0 1] 4 + 125 = 129 ------------ Error:[0.47477457] Pred:[0 0 1 1 1 0 0 0] True:[0 0 1 1 1 0 0 0] 39 + 17 = 56 ------------ Error:[0.21595037] Pred:[0 0 0 0 1 1 1 0] True:[0 0 0 0 1 1 1 0] 11 + 3 = 14 ------------總結
以上是生活随笔為你收集整理的深度学习——02、深度学习入门——python实现RNN算法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ZYAR20A 亚克力2驱 蓝牙 298
- 下一篇: 深度学习——05、深度学习框架Caffe