batch_size = 100 #----------------------------------------------------- #iteration num_of_itr = 100 for i in range(num_of_itr): X_batch, T_batch = function1.batch(X_train, T_train, batch_size) # function1.show_img(X_batch,T_batch,1) #========================================= C = function1.convolute(X_batch, F) # function1.show_img(C,T_batch,1) Y = function1.affine(C, Wo, Bo, 'softmax') E = function1.error(Y, T_batch) E_save = np.append(E_save, E) Acc = function1.accuracy(Y, T_batch) accuracy_save = np.append(accuracy_save, Acc) #========================================= dWo = np.dot(C.T, (Y - T_batch)) dBo = np.reshape(np.sum(Y - T_batch, axis=0), (1, 10)) delta = np.dot(Y - T_batch, Wo.T) dF = function1.deconvolute(X_batch, delta) dF = np.average(dF, axis=0) dF = np.reshape(dF, (5, 5))
for M in range(batch_size): img = np.reshape(X_batch[M], (IMG_size, IMG_size)) FM_storage = [] for i in range(FM_size): for j in range(FM_size): pick_img = img[i:i + F_size, j:j + F_size] FM_storage = np.append(FM_storage, np.tensordot(F, pick_img)) FM_bias = FM_storage + Bc #Bias FM_relu = np.where(FM_bias < 0, 0, FM_bias) #ReLU FM_batch = np.vstack((FM_batch, FM_relu)) FM_bias_batch = np.vstack((FM_bias_batch, FM_bias)) Y = function1.affine(FM_batch, Wo, Bo, 'softmax') E = function1.error(Y, T_batch) E_save = np.append(E_save, E) Acc = function1.accuracy(Y, T_batch) accuracy_save = np.append(accuracy_save, Acc) #====================================================== dWo = np.dot(FM_batch.T, (Y - T_batch)) dBo = np.reshape(np.sum(Y - T_batch, axis=0), (1, 10)) #------------------------------------------------------ delta = np.where(FM_bias_batch <= 0, 0, 1) * np.dot(Y - T_batch, Wo.T) dF_batch = np.empty((0, F_size**2))
Bo = np.random.randn(1, 10) learning_rate = 0.001 E_save = [] accuracy_save = [] start_time = time.time() batch_size = 100 #iteration num_of_itr = 3000 for i in range(num_of_itr): X_batch, T_batch = function1.batch(X_train, T_train, batch_size) H = function1.affine(X_batch, Wh, Bh, 'sigmoid') Y = function1.affine(H, Wo, Bo, 'softmax') E = function1.error(Y, T_batch) E_save = np.append(E_save, E) Acc = function1.accuracy(Y, T_batch) accuracy_save = np.append(accuracy_save, Acc) #======================= dWo = np.dot(H.T, (Y - T_batch)) dBo = np.reshape(np.sum(Y - T_batch, axis=0), (1, 10)) dWh = np.dot(X_batch.T, H * (1 - H) * np.dot(Y - T_batch, Wo.T)) dBh = np.sum(H * (1 - H) * np.dot(Y - T_batch, Wo.T), axis=0,