暑期第二天回顾两层网络训练细节,对应CS231n-class-10,练习numpy写layer
1 import numpy as np 2 3 x = np.array([[0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]]) 4 y = np.array([[0, 1, 1, 0]]).T 5 print(x) 6 print(y) 7 8 w0 = 2*np.random.random((3, 4)) - 1 9 w1 = 2*np.random.random((4, 1)) - 1 10 11 for j in range(60000): 12 l1 = 1 / (1 + np.exp(-(np.dot(x, w0)))) 13 l2 = 1 / (1 + np.exp(-(np.dot(l1, w1)))) 14 l2_delta = (y-l2)*(l2*(1-l2)) 15 l1_delta = l2_delta.dot(w1.T) * (l1*(1-l1)) 16 w1 += l1.T.dot(l2_delta) 17 w0 += x.T.dot(l1_delta) 18 loss=y-l2 19 if(j%10000==0): 20 print(loss) 21 print(l2)