def train(self, X_train, Y_train, X_test, Y_test, print_accuracy=False, show_loss_graph=False, show_accuracy_graph=False): x_train, y_train = self.normalize_vec(X_train, Y_train) x_test, y_test = self.normalize_vec(X_test, Y_test) train_loss = [] train_acc_histroy = [] test_acc_history = [] params = {} for i in range(self.epoch_num): loss_per_epoch = 0 minibatches = shuffle_batches(x_train, y_train, self.batch_size) for j in range(len(minibatches)): x_batch = minibatches[j][0] y_batch = minibatches[j][1] grads = self.gradient(x_batch, y_batch) self.W1 -= self.learning_rate * grads['W1'] self.b1 -= self.learning_rate * grads['b1'] self.W2 -= self.learning_rate * grads['W2'] self.b2 -= self.learning_rate * grads['b2'] loss = self.loss(x_batch, y_batch) loss_per_epoch += loss ave_loss = loss_per_epoch / len(minibatches) train_loss.append(ave_loss) if print_accuracy: train_accuracy = self.accuracy(x_train, y_train) test_accuracy = self.accuracy(x_test, y_test) train_acc_histroy.append(train_accuracy / X_train.shape[0]) test_acc_history.append(test_accuracy / X_test.shape[0]) print("Epoch {i}: Loss={ave_loss} Accuracy_train={train_acc:.4f} Accuracy_test={test_acc:.4f}"\ .format(i=i,ave_loss=ave_loss,train_acc=train_accuracy/X_train.shape[0],test_acc=test_accuracy/X_test.shape[0])) else: print("Epoch {i}: Loss={ave_loss}".format(i)) print("Final test_accuracy: {acc}".format( acc=self.accuracy(x_test, y_test) / X_test.shape[0])) if show_loss_graph: x = np.linspace(0, self.epoch_num, self.epoch_num) plt.plot(x, train_loss, label='loss') plt.title('Average Loss of Each Epoch') plt.xlabel('epoch') plt.ylabel('loss') plt.xlim(left=0) plt.ylim(bottom=0) # plt.savefig('fig_loss_{mid_node_num}n_{epoch_num}e.png'\ # .format(mid_node_num=self.sizes[1], epoch_num=self.epoch_num)) plt.show() if show_accuracy_graph: x2 = np.linspace(0, len(test_acc_history), len(test_acc_history)) plt.plot(x2, train_acc_histroy, label='train accuracy') plt.plot(x2, test_acc_history, label='test accuracy', linestyle='--') plt.title('Accuracy After Each Epoch') plt.xlabel('epoch') plt.ylabel('accuracy') plt.xlim(left=0) plt.ylim(0, 1.0) plt.legend(loc='lower right') # plt.savefig('fig_acc_{mid_node_num}n_{epoch_num}e.png'\ # .format(mid_node_num=self.sizes[1], epoch_num=self.epoch_num)) plt.show() params['W1'], params['b1'], params['W2'], params[ 'b2'] = self.W1, self.b1, self.W2, self.b2 print("Training, Done!") return params
def train(self, X_train, Y_train, X_test, Y_test, print_accuracy=False, show_loss_graph=False, show_accuracy_graph=False): x_train, y_train = self.normalize_vec(X_train, Y_train) x_test, y_test = self.normalize_vec(X_test, Y_test) train_loss = [] # epochごとの平均loss train_loss_iter = [] # iterationごとのloss train_acc_histroy = [] test_acc_history = [] params = {} for i in range(self.epoch_num): print("========= Epoch {i} Start =========".format(i=i)) loss_per_epoch = 0 accuracy_per_epoch = 0 minibatches = shuffle_batches(x_train, y_train, self.batch_size) bar = ShowProcess(len(minibatches)) for j in range(len(minibatches)): x_batch = minibatches[j][0] y_batch = minibatches[j][1] # 重み更新 grads = self.gradient(x_batch, y_batch) params = {'W1':self.W1, 'b1':self.b1, 'W2':self.W2, \ 'b2':self.b2, 'W3':self.W3, 'b3':self.b3,'gamma':self.gamma, 'beta':self.beta} self.optimizer.update(params, grads) if self.use_bn: self.running_mean = self.layers['BatchNorm'].running_mean self.running_var = self.layers['BatchNorm'].running_var # lossの計算 loss = self.loss(x_batch, y_batch) loss_per_epoch += loss bar.show_process() time.sleep(0.05) ave_loss = loss_per_epoch / len(minibatches) train_loss.append(ave_loss) if print_accuracy: # 2000枚の画像をランダムに抽出し、正解率を評価する train_mask = np.random.choice(X_train.shape[0], 2000) test_mask = np.random.choice(X_test.shape[0], 2000) x_train_randn = x_train[train_mask] y_train_randn = y_train[train_mask] x_test_randn = x_test[test_mask] y_test_randn = y_test[test_mask] train_accuracy = self.accuracy(x_train_randn, y_train_randn) test_accuracy = self.accuracy(x_test_randn, y_test_randn) train_acc_histroy.append(train_accuracy / 2000) test_acc_history.append(test_accuracy / 2000) print("Epoch {i}: Loss={ave_loss:.15f} acc_train={train_acc:.4f} acc_test={test_acc:.4f}"\ .format(i=i,ave_loss=ave_loss,train_acc=train_accuracy/2000,test_acc=test_accuracy/2000)) else: print("Epoch {i}: Ave_Loss={ave_loss}".format( i=i, ave_loss=ave_loss)) final_test_acc = self.accuracy(x_test, y_test) / x_test.shape[0] print("Final accuracy_test: {acc}".format(acc=final_test_acc)) ''' # lossデータを保存 path = os.path.dirname(os.path.abspath(__file__)) loss_filename = os.path.join(path,'loss_{opt}.txt'.format(opt=self.opt)) f = open(loss_filename, 'wb') pickle.dump(train_loss, f) ''' # 画像出力 if show_loss_graph: x = np.linspace(0, self.epoch_num, self.epoch_num) plt.plot(x, train_loss, label='loss') plt.title('Average Loss of Each Epoch--{activation}'.format( activation=self.activation)) plt.xlabel('epoch') plt.ylabel('loss') plt.xlim(left=0) plt.ylim(bottom=0) # plt.savefig(os.path.join(path,'fig_loss_{act_f}_{mid_node_num}n_{epoch_num}e.png'\ # .format(act_f=self.activation,mid_node_num=self.sizes[1], epoch_num=self.epoch_num))) plt.show() if show_accuracy_graph: x2 = np.linspace(0, len(test_acc_history), len(test_acc_history)) plt.plot(x2, train_acc_histroy, label='train accuracy') plt.plot(x2, test_acc_history, label='test accuracy', linestyle='--') plt.title('Accuracy After Each Epoch--{activation}'.format( activation=self.activation)) plt.xlabel('epoch') plt.ylabel('accuracy') plt.xlim(left=0) plt.ylim(0, 1.0) plt.legend(loc='lower right') # plt.savefig(os.path.join(path,'fig_acc_{act_f}_{mid_node_num}n_{epoch_num}e.png'\ # .format(act_f=self.activation,mid_node_num=self.sizes[1], epoch_num=self.epoch_num))) plt.show() # 重み獲得 if self.use_bn: params['W1'], params['b1'], params['W2'], params['b2'], params['W3'], params['b3'],\ params['gamma'], params['beta'], params['running_mean'], params['running_var']= \ self.W1, self.b1, self.W2, self.b2, self.W3, self.b3, self.gamma, self.beta, self.running_mean, self.running_var else: params['W1'], params['b1'], params['W2'], params['b2'], params['W3'], params['b3'],params['gamma'], params['beta']=\ self.W1, self.b1, self.W2, self.b2, self.W3, self.b3, self.gamma, self.beta # print("Network Architecture:{a_f}-Dropout({use_dropout}({drop_p}))-BatchNorm({use_bn})"\ # .format(a_f=self.activation,use_dropout=self.use_dropout,drop_p=self.dropout_p, use_bn=self.use_bn)) print("Training, Done!") return params
def train(self, X_train, Y_train, X_test, Y_test, print_accuracy=False, show_loss_graph=False, show_accuracy_graph=False): x_train, y_train = self.normalize_vec(X_train, Y_train) x_test, y_test = self.normalize_vec(X_test, Y_test) train_loss = [] train_acc_histroy = [] test_acc_history = [] params = {} for i in range(self.epoch_num): loss_per_epoch = 0 accuracy_per_epoch = 0 minibatches = shuffle_batches(x_train, y_train, self.batch_size) for j in range(len(minibatches)): x_batch = minibatches[j][0] y_batch = minibatches[j][1] grads = self.gradient(x_batch, y_batch) params = { 'W1': self.W1, 'b1': self.b1, 'W2': self.W2, 'b2': self.b2, 'gamma': self.gamma, 'beta': self.beta } self.optimizer.update(params, grads) if self.use_bn: self.running_mean = self.layers['BatchNorm'].running_mean self.running_var = self.layers['BatchNorm'].running_var loss = self.loss(x_batch, y_batch) loss_per_epoch += loss ave_loss = loss_per_epoch / len(minibatches) train_loss.append(ave_loss) if print_accuracy: train_accuracy = self.accuracy(x_train, y_train) test_accuracy = self.accuracy(x_test, y_test) train_acc_histroy.append(train_accuracy / X_train.shape[0]) test_acc_history.append(test_accuracy / X_test.shape[0]) print("Epoch {i}: Loss={ave_loss} Accuracy_train={train_acc:.4f} Accuracy_test={test_acc:.4f}"\ .format(i=i,ave_loss=ave_loss,train_acc=train_accuracy/X_train.shape[0],test_acc=test_accuracy/X_test.shape[0])) else: print("Epoch {i}: Loss={ave_loss}".format(i)) print("Final test_accuracy: {acc}".format( acc=self.accuracy(x_test, y_test) / X_test.shape[0])) path = os.path.dirname(os.path.abspath(__file__)) if show_loss_graph: x = np.linspace(0, self.epoch_num, self.epoch_num) plt.plot(x, train_loss, label='loss') plt.title('Average Loss of Each Epoch--{activation}'.format( activation=self.activation)) plt.xlabel('epoch') plt.ylabel('loss') plt.xlim(left=0) plt.ylim(bottom=0) # plt.savefig(os.path.join(path,'fig_loss_{act_f}_{mid_node_num}n_{epoch_num}e.png'\ # .format(act_f=self.activation,mid_node_num=self.sizes[1], epoch_num=self.epoch_num))) plt.show() if show_accuracy_graph: x2 = np.linspace(0, len(test_acc_history), len(test_acc_history)) plt.plot(x2, train_acc_histroy, label='train accuracy') plt.plot(x2, test_acc_history, label='test accuracy', linestyle='--') plt.title('Accuracy After Each Epoch--{activation}'.format( activation=self.activation)) plt.xlabel('epoch') plt.ylabel('accuracy') plt.xlim(left=0) plt.ylim(0, 1.0) plt.legend(loc='lower right') # plt.savefig(os.path.join(path,'fig_acc_{act_f}_{mid_node_num}n_{epoch_num}e.png'\ # .format(act_f=self.activation,mid_node_num=self.sizes[1], epoch_num=self.epoch_num))) plt.show() if self.use_bn: params['W1'], params['b1'], params['W2'], params['b2'] ,params['gamma'], params['beta'], params['running_mean'], params['running_var']= \ self.W1, self.b1, self.W2, self.b2, self.gamma, self.beta, self.running_mean,self.running_var else: params['W1'], params['b1'], params['W2'], params['b2'] ,params['gamma'], params['beta']= \ self.W1, self.b1, self.W2, self.b2, self.gamma, self.beta print("Network Architecture:{a_f}-Dropout({use_dropout}({drop_p}))-BatchNorm({use_bn})"\ .format(a_f=self.activation,use_dropout=self.use_dropout,drop_p=self.dropout_p, use_bn=self.use_bn)) print("Training, Done!") return params