def optimize(self, layer_i: int, optimizer: Optimizer): # 算出weight和bias在優化器中的識別碼後,放入優化器進行優化。 self.linear.weight = optimizer.update( layer_i * Layer._max_params_n + 0, self.linear.weight, self.linear.grad_weight) self.linear.bias = optimizer.update( layer_i * Layer._max_params_n + 1, self.linear.bias, self.linear.grad_bias)
def fit(self, train_images, train_labels, test_images, test_labels, optimization_method=OptimizationMethod.Adam, optimization_params={ 'learning_rate': 0.01, 'momentum': 0.9, 'beta1': 0.9, 'beta2': 0.999 }, iters_num=10000, mini_batch_size=100, samples_num_evaluated_per_epoc=100, is_activation_check=False): """Fits weight paramters by using optimization algorithm. """ costs = [] train_accuracies = [] test_accuracies = [] ITERS_NUM = iters_num MINI_BATCH_SIZE = mini_batch_size TRAIN_IMAGES_NUM = train_images.shape[0] ITER_PER_EPOC = max(TRAIN_IMAGES_NUM / MINI_BATCH_SIZE, 1) optimizer = Optimizer().optimizer( optimization_method=optimization_method, learning_rate=optimization_params.get('learning_rate'), momentum=optimization_params.get('momentum'), beta1=optimization_params.get('beta1'), beta2=optimization_params.get('beta2'), ) for i in range(ITERS_NUM): batch_mask = np.random.choice(TRAIN_IMAGES_NUM, MINI_BATCH_SIZE) x_batch = train_images[batch_mask] t_batch = train_labels[batch_mask] grads = nn.computeGradientWithBackPropagation( x_batch, t_batch, is_activation_check=is_activation_check) optimizer.update(nn.params, grads) costs.append(nn.computeCost(nn.forward(x_batch), t_batch)) print('cost {}'.format(costs[-1])) # check accuracy if i % ITER_PER_EPOC == 0: print('=========ITERATION {}=========='.format(i)) if samples_num_evaluated_per_epoc is None: samples_num_evaluated_per_epoc = -1 train_accuracies.append( nn.computeAccuracy( train_images[:samples_num_evaluated_per_epoc], train_labels[:samples_num_evaluated_per_epoc])) test_accuracies.append( nn.computeAccuracy( test_images[:samples_num_evaluated_per_epoc], test_labels[:samples_num_evaluated_per_epoc])) print("train accuracy {}, test accuracy {}".format( train_accuracies[-1], test_accuracies[-1])) return costs, train_accuracies, test_accuracies