def perform_one_layer_nn(train_input, train_output, test_input, test_output): print( 'Perform one layer neural network [784 x 10] with softmax at the end') nn = NeuralNetwork([784, 10], activation_function=ActivationFunction.SOFTMAX) start = time() print('Start training') nn.learn(train_input, train_output, 0.01, 0.9, 100) val = time() - start print("Time", val) print("Mse: ", nn.mse(test_input, test_output)) print("Accuracy: ", nn.accuracy(test_input, test_output)) return val
def perform_two_layers_nn(train_input, train_output, test_input, test_output): print( 'Perform two layers neural network [784 x 300 x 10] with sigmoid at start and softmax at the end' ) nn = NeuralNetwork() nn.add_layer( Layer(shape=(784, 300), activation_function=ActivationFunction.SIGMOID)) nn.add_layer( Layer(shape=(300, 10), activation_function=ActivationFunction.SOFTMAX)) start = time() print('Start pre-training') nn.learn(train_input[:100], train_output[:100], 0.01, 0.49, 100) print('Start main training') nn.learn(train_input, train_output, 0.1, 0.7, 100) nn.learn(train_input, train_output, 0.01, 0.8, 100) val = time() - start print("Time:", val) print("Mse: ", nn.mse(test_input, test_output)) print("Accuracy: ", nn.accuracy(test_input, test_output)) return val
for eta in etas: nn = NeuralNetwork(Xtrain, ytrain, layers, Xtest=Xtest, ytest=ytest) n_batches = int(Xtrain.shape[0] / batch_size) a, b, c, d = nn.fit(n_epochs=100, eta=eta, n_batches=n_batches, lmbda=lmbda) accuracys_train.append(a) costs_train.append(b) accuracys_test.append(c) costs_test.append(d) print("Test accuracy:", nn.accuracy(Xtest, ytest)) # test accuracy after training plt.figure(figsize=(10, 8)) plt.title("Accuracy score for varying learning rate") plt.xlabel("Epoch") plt.ylabel("Accuracy") for i, eta in enumerate(etas): color = color_list[i] plt.plot(accuracys_train[i], "--", color=color, label="Train, eta = {:g}".format(eta)) plt.plot(accuracys_test[i], color=color, label="Test, eta = {:g}".format(eta))
def main(input_train, input_test, correct_train_orig, correct_test_orig, train_data_name, test_data_name): # input_train, correct_train: numpy.array # input_test, correct_test: list 要素はnumpy.arary n_train = len(input_train) # 学習データの数 n_test = len(input_test[0]) # テストデータの数 # 正解データをone-hot表現に correct_train = np.zeros((n_train, len(chars))) # 2000x20 correct_test = [ np.zeros((n_test, len(chars))) for i in range(len(input_test)) ] # 2000x20がlen(input_test)個 for i in range(n_train): correct_train[i, correct_train_orig[i]] = 1 for i in range(len(input_test)): for j in range(n_test): correct_test[i][j, correct_test_orig[i][j]] = 1 # 各層の初期化 middle_layer = MiddleLayer(n_input, n_middle, eta, alpha) output_layer = OutputLayer(n_middle, n_output, eta, alpha) # ネットワークの初期化 net = NeuralNetwork([middle_layer, output_layer]) # 誤差の記録 train_error_x = [] train_error_y = [] test_error_x = [] test_error_y = [[] for i in range(len(input_test))] n_batch = n_train // batch_size # 1epochあたりのバッチ数 for i in range(epoch): # 誤差の計算 net.forward_prop(input_train) # 順伝播 error_train = net.cross_entropy(correct_train, n_train) # クロスエントロピー誤差の計算 error_test = [] for j in range(len(input_test)): net.forward_prop(input_test[j]) # 順伝播 error_test.append(net.cross_entropy(correct_test[j], n_test)) # クロスエントロピー誤差の計算 # 誤差の記録 train_error_x.append(i) train_error_y.append(error_train) test_error_x.append(i) for j in range(len(input_test)): test_error_y[j].append(error_test[j]) # 学習経過 if i % interval == 0: print("Epoch: {}".format(i)) # クロスエントロピー誤差表示 print("Error_train({}): {}".format(train_data_name, error_train)) for j in range(len(input_test)): print("Error_test({}): {}".format(test_data_name[j], error_test[j])) # 正答率表示 print("Accuracy_train({}): {}".format( train_data_name, net.accuracy(input_train, correct_train))) for j in range(len(input_test)): print("Accuracy_test({}): {}".format( test_data_name[j], net.accuracy(input_test[j], correct_test[j]))) print("=" * 50) # 学習 index_random = np.arange(n_train) np.random.shuffle(index_random) for j in range(n_batch): # ミニバッチ作成 mb_index = index_random[j * batch_size:(j + 1) * batch_size] input_ = input_train[mb_index, :] # 入力 correct = correct_train[mb_index, :] # 正解 net.forward_prop(input_) # 順伝播 net.back_prop(correct) # 逆伝播 net.update_wb() # 重みとバイアスの更新 # 結果の保存 make_dir(os.getcwd() + os.sep + "result") graph_name = os.path.join(os.getcwd(), "result", "error.png") save_error_graph(train_error_x, train_error_y, test_error_x, test_error_y, train_data_name, test_data_name, graph_name) # グラフの表示 if show_error_graph: img = cv2.imread(graph_name) cv2.imshow(graph_name.split("/")[-1], img) cv2.waitKey(0) cv2.destroyAllWindows() # 正答率をファイルに書き込む if write_accuracy: now = datetime.datetime.now() file_name = "accuracy_" + now.strftime("%Y-%m-%d_%H-%M-%S") + ".txt" make_dir(os.getcwd() + os.sep + "result") with open(os.path.join("result", file_name), mode="w") as f: # 訓練データの正答率書き込み f.write("{}: {}\n".format(train_data_name, net.accuracy(input_train, correct_train))) # テストデータの正答率書き込み for j in range(len(input_test)): f.write("{}: {}\n".format( test_data_name[j], net.accuracy(input_test[j], correct_test[j]))) # 任意の文字「あ」を入力した時の出力層の各ユニットの出力値をファイルに書き込む if write_accuracy: input_ = input_train[0] # 一番初めの「あ」を取り出す input_ = input_.reshape((1, input_.shape[0])) # 行列に変換 net.forward_prop(input_) make_dir(os.getcwd() + os.sep + "result") # 書き込み with open(os.path.join("result", "output.txt"), mode="w") as f: output_result = "" for i, p in enumerate(net.output[0]): f.write("{}: {}\n".format(i, p))
import numpy as np from load_data import load_dataset from neural_network import NeuralNetwork from draw_graph import drawGraph train_x_orig, train_y, test_x_orig, test_y, classes = load_dataset() train_x_flatten = train_x_orig.reshape(train_x_orig.shape[0], -1).T test_x_flatten = test_x_orig.reshape(test_x_orig.shape[0], -1).T train_x = train_x_flatten / 255. test_x = test_x_flatten / 255. n_x = train_x.shape[0] layers_dims = [n_x, 20, 5, 7, 1] # 4-Layer Neural Network #layers_dims = [n_x, 7, 1] # 2-Layer Neural Network number_of_epochs = 2500 NN = NeuralNetwork(layers_dims, learning_rate=0.0075, num_iterations=number_of_epochs, verbose=True, regularization="L2") NN.fit(train_x, train_y) training_predictions = NN.predict(train_x) training_loss = NN.evaluate_loss() training_accuracy = NN.accuracy(training_predictions, train_y) print "Training Accuracy = %f " %(training_accuracy) + "%" testing_predictions = NN.predict(test_x) testing_accuracy = NN.accuracy(testing_predictions, test_y) print "Testing Accuracy = %f " %(testing_accuracy) + "%" drawGraph(number_of_epochs, training_loss, training_accuracy, testing_accuracy)