def report_test_classification_error(): train_data, train_target, valid_data, valid_target, test_data, test_target = p11.load_data( ) W1_best = np.load("1000_best_W1_at108.npy") W2_best = np.load("1000_best_W2_at108.npy") test_error = p11.compute_2_layer_error(tf.cast(test_data, tf.float32), tf.one_hot(test_target, 10), W1_best, W2_best) with tf.Session() as sess: test_error = test_error.eval() print("test error:", test_error) with open("part_1_2_1.txt", "a") as file: file.write("test classification error: " + str(test_error) + "\n")
def train_dropout(): train_data, train_target, valid_data, valid_target, test_data, test_target = p11.load_data( ) valid_data = tf.cast(valid_data, tf.float32) valid_target_onehot = tf.one_hot(valid_target, 10) batch_size = 500 num_iterations = 4500 num_train = train_data.shape[0] num_batches = num_train // batch_size num_epochs = num_iterations // num_batches num_iterations_leftover = num_iterations % num_batches print("batch size:", batch_size, "; number of batches", num_batches) print("number of epochs:", num_epochs, "; iteration left-overs:", num_iterations_leftover) print("number of iterations:", num_iterations) X0, Y, S2, loss, optimizer = p11.build_2_layer_NN(dropout=True) init = tf.global_variables_initializer() valid_error_list = [] train_error_list = [] with tf.Session() as sess: sess.run(init) shuffled_inds = np.arange(num_train) batch_X0 = train_data[0:batch_size] batch_Y = train_target[0:batch_size] train_error, valid_error = compute_errors(sess, X0, S2, batch_X0, batch_Y, valid_data, valid_target_onehot) train_error_list.append(train_error) valid_error_list.append(valid_error) print("initial train classification error:", train_error) print("initial valid classification error:", valid_error) best_valid_errors = [] best_valid_error = 100000 for epoch in range(num_epochs): np.random.shuffle(shuffled_inds) temp_train_data = train_data[shuffled_inds] temp_train_targets = train_target[shuffled_inds] for j in range(num_batches): batch_X0 = temp_train_data[j * batch_size:(j + 1) * batch_size] batch_Y = temp_train_targets[j * batch_size:(j + 1) * batch_size] sess.run(optimizer, feed_dict={X0: batch_X0, Y: batch_Y}) train_error, valid_error = compute_errors(sess, X0, S2, batch_X0, batch_Y, valid_data, valid_target_onehot) train_error_list.append(train_error) valid_error_list.append(valid_error) if valid_error < best_valid_error: best_valid_errors.clear() best_valid_errors.append((epoch, valid_error)) best_valid_error = valid_error elif valid_error == best_valid_error: best_valid_errors.append((epoch, valid_error)) if epoch % 10 == 0: print("training classification error:", train_error) print("valid classification error:", valid_error) print("final training classification error:", train_error_list[-1]) print("final validation classification error:", valid_error_list[-1]) with open("part_1_3_1.txt", "a") as file: file.write("\n" + strftime("%Y-%m-%d %H:%M:%S", gmtime()) + "\n") file.write("final training classification error: " + str(train_error_list[-1]) + "\n") file.write("final validation classification error: " + str(valid_error_list[-1]) + "\n") file.write("best validation classification error: " + str(best_valid_errors) + "\n") plt.plot(train_error_list) plt.plot(valid_error_list) plt.xlabel("epoch #") plt.ylabel("classification error") plt.title("classification error vs epoch #") plt.legend(["training", "validation"]) plt.savefig("part_1_3_1", dpi=400) plt.gcf().clear()
def tune_num_of_hidden_units(): train_data, train_target, valid_data, valid_target, test_data, test_target = p11.load_data( ) valid_data = tf.cast(valid_data, tf.float32) valid_target_onehot = tf.one_hot(valid_target, 10) batch_size = 500 num_iterations = 4500 num_train = train_data.shape[0] num_batches = num_train // batch_size num_epochs = num_iterations // num_batches num_iterations_leftover = num_iterations % num_batches print("batch size:", batch_size, "; number of batches", num_batches) print("number of epochs:", num_epochs, "; iteration left-overs:", num_iterations_leftover) print("number of iterations:", num_iterations) hidden_units_num = [100, 500, 1000] for hidden_num in hidden_units_num: print("number of hidden units:", hidden_num) X0, Y, S2, loss, optimizer = p11.build_2_layer_NN( hidden_units_num=hidden_num) init = tf.global_variables_initializer() valid_error_list = [] best_models = [] best_valid_error = 100000000 with tf.Session() as sess: sess.run(init) shuffled_inds = np.arange(num_train) for epoch in range(num_epochs): np.random.shuffle(shuffled_inds) temp_train_data = train_data[shuffled_inds] temp_train_targets = train_target[shuffled_inds] for j in range(num_batches): batch_X0 = temp_train_data[j * batch_size:(j + 1) * batch_size] batch_Y = temp_train_targets[j * batch_size:(j + 1) * batch_size] sess.run(optimizer, feed_dict={X0: batch_X0, Y: batch_Y}) with tf.variable_scope("W", reuse=True): W1 = tf.get_variable("W" + str(p11.layer_num - 2)) W2 = tf.get_variable("W" + str(p11.layer_num - 1)) valid_error = p11.compute_2_layer_error( valid_data, valid_target_onehot, W1, W2).eval() # record best validation error if valid_error < best_valid_error: best_valid_error = valid_error best_models.clear() best_models.append( [epoch, valid_error, [W1.eval(), W2.eval()]]) elif valid_error == best_valid_error: best_models.append( [epoch, valid_error, [W1.eval(), W2.eval()]]) valid_error_list.append(valid_error) if epoch % 10 == 0: print("validation error:", valid_error) print("final validation classification error:", valid_error_list[-1]) with open("part_1_2_1.txt", "a") as file: file.write("\n" + strftime("%Y-%m-%d %H:%M:%S", gmtime()) + "\n") # save model for best in best_models: with open("part_1_2_1.txt", "a") as file: file.write( str(hidden_num) + "best validation classification errors: " + str(best[0:2]) + "\n") print("best valid classification errors:", best[:2]) np.save( str(hidden_num) + "_best_W1_at" + str(best[0]) + ".npy", best[-1][0]) np.save( str(hidden_num) + "_best_W2_at" + str(best[0]) + ".npy", best[-1][1]) with open("part_1_2_1.txt", "a") as file: file.write( str(hidden_num) + "final validation classification error: " + str(valid_error_list[-1]) + "\n") plt.plot(valid_error_list) plt.xlabel("epoch #") plt.ylabel("validation classification error") plt.title("validation classification error vs epoch #") plt.savefig("part_1_2_1_" + str(hidden_num), dpi=400) plt.gcf().clear()
def compare_NNs(): train_data, train_target, valid_data, valid_target, test_data, test_target = p11.load_data( ) test_data = tf.cast(test_data, tf.float32) test_target_onehot = tf.one_hot(test_target, 10) batch_size = 500 num_iterations = 4500 num_train = train_data.shape[0] num_batches = num_train // batch_size num_epochs = num_iterations // num_batches num_iterations_leftover = num_iterations % num_batches print("batch size:", batch_size, "; number of batches", num_batches) print("number of epochs:", num_epochs, "; iteration left-overs:", num_iterations_leftover) print("number of iterations:", num_iterations) X0_3, Y_3, S3, loss_3, optimizer_3 = build_3_layer_NN() X0_2, Y_2, S2, loss_2, optimizer_2 = p11.build_2_layer_NN() init = tf.global_variables_initializer() test_error_list_3 = [] test_error_list_2 = [] best_error_3 = 100000 best_error_2 = 100000 best_errors_3 = [] best_errors_2 = [] with tf.Session() as sess: sess.run(init) shuffled_inds = np.arange(num_train) test_error_3, test_error_2 = compute_test_errors( test_data, test_target_onehot) test_error_list_3.append(test_error_3) test_error_list_2.append(test_error_2) print("3 layer initial test error:", test_error_3) print("2 layer initial test error:", test_error_2) for epoch in range(num_epochs): np.random.shuffle(shuffled_inds) temp_train_data = train_data[shuffled_inds] temp_train_targets = train_target[shuffled_inds] for j in range(num_batches): batch_X0 = temp_train_data[j * batch_size:(j + 1) * batch_size] batch_Y = temp_train_targets[j * batch_size:(j + 1) * batch_size] sess.run([optimizer_3, optimizer_2], feed_dict={ X0_3: batch_X0, Y_3: batch_Y, X0_2: batch_X0, Y_2: batch_Y }) test_error_3, test_error_2 = compute_test_errors( test_data, test_target_onehot) test_error_list_3.append(test_error_3) test_error_list_2.append(test_error_2) if test_error_3 < best_error_3: best_errors_3.clear() best_errors_3.append((epoch, test_error_3)) best_error_3 = test_error_3 elif test_error_3 == best_error_3: best_errors_3.append((epoch, test_error_3)) if test_error_2 < best_error_2: best_errors_2.clear() best_errors_2.append((epoch, test_error_2)) best_error_2 = test_error_2 elif test_error_2 == best_error_2: best_errors_2.append((epoch, test_error_2)) if epoch % 10 == 0: print("3 layer test error:", test_error_3) print("2 layer test error:", test_error_2) with open("part_1_2_2.txt", "a") as file: file.write("\n" + strftime("%Y-%m-%d %H:%M:%S", gmtime()) + "\n") file.write("3 layer final test classification error: " + str(test_error_list_3[-1]) + "\n") file.write("2 layer final test classification error: " + str(test_error_list_2[-1]) + "\n") file.write("3 layer best test classification error: " + str(best_errors_3) + "\n") file.write("2 layer best test classification error: " + str(best_errors_2) + "\n") plt.plot(test_error_list_3) plt.plot(test_error_list_2) plt.legend(['3-layer NN', '2-layer NN']) plt.title("test classification error vs epoch #") plt.xlabel('epoch number') plt.ylabel('test classification error') plt.savefig("part_1_2_2_test", dpi=400)