def run(): ## inputs = csv.open_card_csv('train.csv') ## #numbers hardcoded for now, and no functions specified ## out_node = Node(f) ## mid_layer1 = Layer(f_list1) ## mid_layer2 = Layer(f_list2) ## mid_layers = [mid_layer1,mid_layer2] ## network = Network(5,mid_layers,out_node) ## network = network.add_edges ## alg.train(inputs,network,10000) ## network.print_weights ## return t, v, r = icsv.csv_to_binary('train.csv') network = neural_net.Net([52, 30, 10]) network.SGD(t, 30, 10, 3.0, test_data=r)
def train_ann_classifier(step_num, eta, active_func_type): #preprocess() # run at the first time to split the data train_data = load_data("./data/iris_train.data") labels = load_label("./data/iris_label.data") feature_num = len(train_data[0]) - 1 hidden_node_num = feature_num + 8 label_num = len(labels) ''' print "feature_num: %d" % feature_num print "hidden_node_num: %d" % hidden_node_num print "label_num: %d" % label_num ''' # initialize simple ann ann_net = neural_net.Net(feature_num, hidden_node_num, label_num, active_func_type) # train for i in xrange(step_num): for ele in train_data: ann_net.train(ele[:-1], labels.index(ele[-1]), eta) # print accuracy of every 100 iterion if i % 100 == 0: m_count = 0 for ele in train_data: m_count += ann_net.test(ele[:-1], labels.index(ele[-1])) print "accuracy of %d steps: %f" % (i, 1.0 * m_count / len(train_data)) # test test_data = load_data("./data/iris_train.data") m_count = 0 for ele in test_data: m_count += ann_net.test(ele[:-1], labels.index(ele[-1])) print "final accuracy: %f" % (1.0 * m_count / len(test_data))
import game import neural_net import time import random net = neural_net.Net([198, 40, 1]) gammon = game.Game() print "Starting game..." # 2 players roll dice, the largest goes first using the roll # r1 is white, r2 is black r1, r2 = (0, 0) turn = 0 # keep rolling if get same while r1 == r2: r1, r2 = gammon.roll_dice() if r1 > r2: turn = 1 elif r2 > r1: turn = 0 start = 1 moves = 0 zero_actions = [] one_actions = [] while not gammon.game_over(): actions = [] if start: actions = gammon.get_actions((r1, r2), turn) start = 0 else: roll = gammon.roll_dice() actions = gammon.get_actions(roll, turn)
import mnist_loader import neural_net # load dataset training_data, validation_data, test_data = mnist_loader.load_data_wrapper() # create subset of trainng data training_data_subset = [] for i in range(0,10000): training_data_subset.append(training_data[i]) # neural_net.Net will only accept [784, 30, 10] -- will not work otherwise net = neural_net.Net([784, 30, 10]) # subset used to save time because it takes forever O(n^2) to train # pass in subset of training data and learning rate #net.train(training_data_subset, .3) # load saved network net.load() # solve for inputs net.imagine(3) # test the trained network #net.test(test_data) # save weights net.save()
def train_ann_classifier(step_num, eta): raw_train_data = load_one_hot_data("./data/train.csv") feature_num = raw_train_data.shape[1] - 1 hidden_node_num = 13 label_num = 10 batch_size = 50 print "feature_num: %d" % feature_num print "hidden_node_num: %d" % hidden_node_num print "label_num: %d" % label_num print "batch_size: %d" % batch_size # load pre-trained weight and bias input_hidden_w = load_pre_train_weight("./model/input_hidden_w.bin", (feature_num, hidden_node_num)) input_hidden_b = load_pre_train_weight("./model/input_hidden_b.bin", (1, hidden_node_num)) hidden_output_w = load_pre_train_weight("./model/hidden_output_w.bin", (hidden_node_num, label_num)) hidden_output_b = load_pre_train_weight("./model/hidden_output_b.bin", (1, label_num)) # initialize simple ann ann_net = neural_net.Net(batch_size, feature_num, hidden_node_num, label_num, input_hidden_w, input_hidden_b, hidden_output_w, hidden_output_b) # train np.random.shuffle(raw_train_data) train_len = int(raw_train_data.shape[0] * 0.8) train_data, valid_data = raw_train_data[:train_len], raw_train_data[ train_len:] valid_features, valid_labels = valid_data[:, 1:] / 255.0, labels_to_one_hot( valid_data[:, 0]) for i in xrange(step_num): features_data, labels_data = gen_batch_data(train_data, batch_size) #start_t = time.time() ann_net.train(features_data, labels_data, eta) #print "%d batch cost time: %d ms" % (i, int((time.time() - start_t)*1000)) # print accuracy of every one epoch if (i + 1) == 1 or (i + 1) % 840 == 0 or (i + 1) == step_num: print "accuracy of %d batch (%d epoch): %f" % ( (i + 1), (i + 1) / 840, ann_net.get_accuracy(valid_features, valid_labels)) ann_net.input_hidden_w.tofile("./model/input_hidden_w.bin") ann_net.input_hidden_b.tofile("./model/input_hidden_b.bin") ann_net.hidden_output_w.tofile("./model/hidden_output_w.bin") ann_net.hidden_output_b.tofile("./model/hidden_output_b.bin") # test test_data = load_one_hot_data("./data/test.csv") predict_label = ann_net.predict(test_data / 255.0) predict_label = [np.arange(1, 1 + len(predict_label)), predict_label] predict_label = np.transpose(predict_label) np.savetxt('submission.csv', predict_label, fmt='%i,%i', header='ImageId,Label', comments='')
import neural_net import mnist_loader import time import os.path if os.path.isfile("../data/weights_file1"): network = neural_net.Net([784, 20, 30, 10]) network.load() print("Network loaded.") else: training_data, validation_data, test_data = mnist_loader.load_data_wrapper() #network = neural_net.Net([784, 30, 10]) network = neural_net.Net([784, 20, 30, 10]) network.load() print("Training network...") # Training takes O(n^2) time now (a really long time for the whole data set) because I wanted to be very explicit about how errors and weights were being calculated. start = time.time() network.train(training_data, 3.0) end = time.time() print("Training time:") print(end - start) print("Test network...") start = time.time() network.test(test_data) end = time.time() print("Test time:") print(end - start) network.save() print("Network successfully saved.")
import numpy as np import neural_net as nn import matplotlib.pyplot as plt # Build fake data for y = 1 if (x > 5) else 0 # X in range [0,10] X_train = np.random.random((100, 64, 1, 1)) * 10 X_test = np.random.random((1000, 1, 1)) * 10 # Calculate y values for each X y_train = (X_train > 5).astype(int) y_test = (X_test > 5).astype(int) # Build and train the network L = [1, 16, 16, 1] A = [nn.sigmoid, nn.sigmoid, nn.sigmoid] net = nn.Net(L, A) net.train(X_train, y_train, X_test, y_test, 1000, nn.squared_error) _, _, y_pred = net.forward(X_test) plt.scatter(X_test.reshape(-1), y_pred.reshape(-1)) plt.show()