# 4. training train_losses = [] train_accuracies = [] test_accuracies = [] for idx in range(1, numiters+1): # 4-1. fetch mini-batch batch_mask = np.random.choice(sztrain, szbatch) train_x_batch = train_x[batch_mask] # 100 x 784 train_t_batch = train_t[batch_mask] # 100 x 10 # 4-2 gradient stime = time.time() # stopwatch: start gradient = network.numerical_gradient_net(train_x_batch, train_t_batch) elapsed = time.time() - stime # stopwatch: end # 4-3. update parameter for key in network.params: network.params[key] -= ratelearning * gradient[key] # 4-4. train loss loss = network.loss(train_x_batch, train_t_batch) train_losses.append(loss) # 4-5. accuracy per epoch if idx / szepoch == 0: train_accuracy = network.accuracy(train_x, train_t) train_accuracies.append(train_accuracy)
# 2. hyperparameter #==Numerical Gradient============================= # 3. initialize network network.initialize(3, 2, 3) _layers = [ Affine(network.params['w1'], network.params['b1']), ReLU(), Affine(network.params['w2'], network.params['b2']), SoftmaxWithLoss() ] grad = network.numerical_gradient_net(_x, _t) print(grad) #== Backpropagation Gradient============================= # 3. initialize network np.random.seed(3) network.initialize(3, 2, 3) _layers = [ Affine(network.params['w1'], network.params['b1']), ReLU(), Affine(network.params['w2'], network.params['b2']), SoftmaxWithLoss() ] grad = backpropagation_gradient_net(_x, _t)
# 4. training train_losses = [] train_accuracies = [] test_accuracies = [] for idx in range(1, numiters+1): # 4-1. fetch mini-batch batch_mask = np.random.choice(sztrain, szbatch) # 60,000 => szbatch train_x_batch = train_x[batch_mask] # szbatch * 784 train_t_batch = train_t[batch_mask] # szbatch * 10 # 4.-2. gradient start = time.time() # stopwatch:start gradient = network.numerical_gradient_net(train_x_batch, train_t_batch) # 모든 파라메터들에 대한 기울기 행렬 elapsed = time.time() - start # stopwatch:end # 4-3. update parameter for key in network.params: network.params[key] -= ratelearning * gradient[key] # 파라메터들을 각각의 기울기값으로 교정 # 4-4. train loss loss = network.loss(train_x_batch, train_t_batch) train_losses.append(loss) # 4-5 accuracy per epoch if idx % szepoch == 0: train_accuracy = network.accuracy(train_x, train_t) train_accuracies.append(train_accuracy)