def test_forward_pass(self): batch_size = 100 input_shape = (10,batch_size) ann = ANN(input_shape=input_shape) X = np.random.rand(*input_shape) Y_pred = ann.forward_pass(X).T self.assertEqual(Y_pred.shape, (batch_size,1))
def CNN_2XConv2D_Relu_Dropout_FC(self, data, test_set=None): input_sizes, output_size, train_set, valid_set = data model = nn.Sequential( nn.Conv2d(in_channels=1, out_channels=16, kernel_size=5, stride=1, padding=2), #nn.BatchNorm2d(16), nn.ReLU(), nn.Dropout2d(p=0.5), nn.MaxPool2d(kernel_size=2), nn.Conv2d(in_channels=16, out_channels=32, kernel_size=5, stride=1, padding=2), #nn.BatchNorm2d(32), nn.ReLU(), nn.Dropout(p=0.5), nn.MaxPool2d(kernel_size=2), Flatten(), nn.Linear(32 * 25 * 40, output_size), nn.LogSoftmax(dim=1)).cuda() network = ANN("CNN:2XConv2D_Relu-Dropout-FC", model, cuda=True) network.train(train_set, epochs=40, batch_size=200, criterion=nn.NLLLoss(), optimizer=optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-1), valid_set=valid_set) return network
def LSTM_H256(self, data, test_set=None): input_sizes, output_size, train_set, valid_set = data hidden_layer = 512 model = nn.Sequential( Squeeze(), SwappSampleAxes(), nn.BatchNorm1d(input_sizes[1]), nn.LSTM(input_sizes[0], hidden_layer, batch_first=True), LSTM_Out(), nn.BatchNorm1d(hidden_layer), nn.Dropout(), nn.Linear(hidden_layer, 256), nn.ReLU(), nn.Dropout(), nn.Linear(256, output_size), #nn.BatchNorm1d(output_size), nn.LogSoftmax(dim=1)).cuda() network = ANN("LSTM_H512_FC256", model, cuda=True) network.train(train_set, epochs=60, batch_size=10, criterion=nn.NLLLoss(), optimizer=optim.Adam(model.parameters(), weight_decay=1e-6), valid_set=valid_set) return network
def train_and_evaluate(self): self.ann = ANN(eta=self.learning_rate, minibatch_size=self.batch_size, layer_config=self.layer_config, epochs=self.epochs) X, y = self.load_dataset(self.train_file, self.window_size) Xtest, ytest = self.load_dataset(self.test_file, self.window_size) train_data = [] train_labels = [] for batch in self.iterate_minibatches(X, y, shuffle=True): x_batch, y_batch = batch train_data.append(x_batch) train_labels.append(y_batch) valid_data = [] valid_labels = [] for batch in self.iterate_minibatches(Xtest, ytest, shuffle=True): x_batch, y_batch = batch valid_data.append(x_batch) valid_labels.append(y_batch) train_error, test_error = self.ann.evaluate(train_data, train_labels, valid_data, valid_labels, eval_train=True) self.show_errors_plot(train_error, test_error)
def __init__(self, num_actions, experience_replay_capacity, frame_skip, starting_experience, discount, batch_size, update_frequency, target_update_frequency, starting_epsilon, final_epsilon, final_epsilon_step): self.num_actions = num_actions self.model = ANN("value_network", [128, 4], num_actions) self.target_model = ANN("target_network", [128, 4], num_actions) self.experience_replay_capacity = experience_replay_capacity self.experience_replay = ExperienceReplay( self.experience_replay_capacity) self.model_parameters_copier = ModelParametersCopier( self.model, self.target_model) self.frame_skip = frame_skip self.starting_experience = starting_experience self.discount = discount self.batch_size = batch_size self.update_frequency = update_frequency self.target_update_frequency = target_update_frequency self.starting_epsilon = starting_epsilon self.final_epsilon = final_epsilon self.final_epsilon_step = final_epsilon_step self.epsilon_annealing_rate = ( self.final_epsilon - self.starting_epsilon) / ( self.final_epsilon_step * self.frame_skip) self.epsilon = self.starting_epsilon self.learn_step = 0 self.act_step = 0 self.frame = 0
class Agent(): def __init__(self, input_dims, classes, lr=1e-5): self.input_dims = input_dims self.classes = classes self.lr = lr self.ann = ANN(self.input_dims, self.classes, lr=lr) def learn(self, data, batch_size=10, epochs=10): self.ann.train() train_loader = DataLoader(dataset=data, batch_size=batch_size, shuffle=True) all_loss = [] all_acc = [] for ep in range(1, epochs+1): epoch_loss = 0 epoch_accuracy = 0 for x_batch, y_batch in train_loader: x_batch, y_batch = x_batch.to(self.ann.device), y_batch.to(self.ann.device) self.ann.optimizer.zero_grad() y_pred = self.ann(x_batch) loss = self.ann.loss(y_pred, y_batch.unsqueeze(1).type(T.float)) accuracy = self.accuracy(y_pred, y_batch.unsqueeze(1)) loss.backward() self.ann.optimizer.step() epoch_loss += loss.item() epoch_accuracy += accuracy.item() all_loss.append(epoch_loss/len(train_loader)) all_acc.append(epoch_accuracy/len(train_loader)) print(f'Epoch:{ep} | Epoch Loss:{epoch_loss/len(train_loader)} | Epoch Accuracy:{epoch_accuracy/len(train_loader)}') return all_loss, all_acc def accuracy(self, y_pred, y_test): y_pred = T.round(y_pred) correct_sum = (y_pred == y_test).sum().float() return correct_sum/y_test.shape[0] def evaluate(self, X): self.ann.eval() test_loader = DataLoader(dataset=X, batch_size=1) y_pred_list = [] with T.no_grad(): for x_batch in test_loader: x_batch = x_batch.to(self.ann.device).type(T.float) y_pred = self.ann(x_batch) y_pred = T.round(y_pred) y_pred_list.append(y_pred) return [i.squeeze().tolist() for i in y_pred_list]
def test_fit(self): input_shape = (10,1) n_samples = 1000 ann = ANN(input_shape=input_shape) X_train = np.random.rand(n_samples, *input_shape) y_train = np.random.randint(1, size=(n_samples,1,1)) cost = ann.fit(X_train, y_train) self.assertGreater(cost, 0)
def __init__(self): """ creating an ANN for identifying the sin of an angle. Since we'll be taking in values in the range of -2π, +2π, and outputting values from -1, 1, we're using the identity activation function for our input and output layers, saving the sigmoid for the hidden layers. """ self.myANN = ANN(layer_sizes=(1,10,10,1), activation_ids=(Activation_Type.IDENTITY, Activation_Type.LEAKY_RELU, Activation_Type.LEAKY_RELU, Activation_Type.IDENTITY))
def __init__(self, pos=[0, 0], score=0, num_layers=3, bias=1, activation=0, layers=[20, 16, 4], symbol=u'\u0041'): self.pos = pos self.old_pos = pos self.score = score if not(num_layers == len(layers)) and len(layers) > 2: num_layers = len(layers) self.ann = ANN(num_layers, bias, activation, layers) self.symbol = symbol self.status = 0 # 0 for moving or 1 for paused (stunned) self.scored = False
def test_backward_pass(self): batch_size = 100 input_shape = (10, batch_size) ann = ANN(input_shape=input_shape) X_train = np.random.rand(*input_shape) y_train = np.random.randint(1, size=(batch_size,1)) Y_pred = ann.forward_pass(X_train) cost = ann.backward_pass(Y_pred, y_train) self.assertIsInstance(cost, float) self.assertGreater(cost, 0)
def MyRNN_H256(self, data, test_set=None): input_sizes, output_size, train_set, valid_set = data model = nn.Sequential( Squeeze, RNN(input_sizes[0], output_size, hidden_size=256, cuda=True)) network = ANN("MyRNN_H256", model, cuda=True) network.train(train_set, epochs=60, batch_size=20, criterion=nn.NLLLoss(), optimizer=optim.Adam(model.parameters(), lr=0.01), valid_set=valid_set) return network
def train(n, weights=None): if weights is None: ann = ANN(shape=[198, 30, 30, 6]) else: ann = ANN(weights=weights) start = datetime.datetime.now() print(start) for i in range(n): steps, end = run_game(ann, lambd=.7, alpha=.1) with open('logs/games.csv', 'a') as games: games.write(f"{i}|{steps}|{end}|{datetime.datetime.now()}\n") with open('logs/weights', 'wb') as weight_file: pickle.dump(ann.weights, weight_file)
def train_best_ANNs(self, x_train, y_train): ANNs = [ ANN('adam', 'logistic', 'constant', 700, .01, .0001, (100, 90, 80, 70)), ANN('adam', 'tanh', 'constant', 6700, .001, .0001, (100, 90, 80, 70)), ANN('adam', 'tanh', 'adaptive', 8700, .0001, .0009, (100, 90, 80, 70)) ] for model in ANNs: self.models.append(model) model.train(x_train, y_train) self.settings.append(["ANN"])
def __process(nrOfIterations, learningRate, hiddenNeuronsNumber, aConst): dataset = ProblemData("resources/data.data") trainX, trainY, testX, testY = dataset.splitData() neuralNetwork = ANN(deepcopy(trainX), deepcopy(trainY), learningRate, hiddenNeuronsNumber, aConst) iterations = [] for i in range(nrOfIterations): neuralNetwork.feedForward() neuralNetwork.backProp() iterations.append(i) for i in range(len(testX)): predictedOut = neuralNetwork.getOutput(testX[i]) print("Predicted output: {0}\nReal value: {1}".format( predictedOut, testY[i])) matplotlib.pyplot.plot(iterations, neuralNetwork.getLoss(), label='loss value vs iteration') matplotlib.pyplot.xlabel('Iterations') matplotlib.pyplot.ylabel('Loss function') matplotlib.pyplot.legend() matplotlib.pyplot.show()
def train_meta(self, x_test, y_test): #loop through all the rows in the dataset and get the prediction from component model_selection #then append to it and add each row to self.meta_input for i in range(0, len(self.y_test)): prediction = self.build_metadata([self.x_test[i]]) self.save_results(self.meta_results, "New_data_predictions-LogR-only.csv") self.my_ANN = ANN('adam', 'logistic', 'constant', 700, .01, .0001, (100, 90, 80, 70)) #finally the dataset with the meta data gets feed into an ANN x_train, x_test, y_train, y_test = self.my_ANN.split_data( self.meta_input, self.y_test, .25) self.my_ANN.train(x_train, y_train) print "FINAL META ACCURACY", self.my_ANN.report_accuracy( x_test, y_test)
class UI: def __init__(self): self.ANN = ANN("data.txt") def run(self): while True: print("0-exit") print("1-run") choice = int(input()) if choice == 1: print("Give noOfIterations(You can try 1000)") noOfIterations = int(input()) self.ANN.train(noOfIterations) else: return
def evalANN(individual): # indivual here is a list of weight of ANN count = 0 ann = ANN(num_inputs, num_hidden_nodes, num_outputs, individual) # TODO check, how pass in the sumError = 0 while count < 5500: outputarray = ann.evaluate(matrixOfTestData[count]) expectedOutputArray = outputcheckarray[count] for i in range(0, len(outputarray)): error = pow(outputarray[i] - expectedOutputArray[i],2) #if (error < bestError): # bestError = error sumError = sumError + error count = count + 1 return sumError,
def RNN_H256(self, data, test_set=None): input_sizes, output_size, train_set, valid_set = data hidden_layer = 256 batch_size = 50 model = nn.Sequential( Squeeze(), SwappSampleAxes(), nn.RNN(input_sizes[0], hidden_layer, batch_first=True), RNN_Out(), nn.Linear(hidden_layer, output_size), nn.LogSoftmax(dim=1)).cuda() network = ANN("RNN_H256", model, cuda=True) network.train(train_set, epochs=60, batch_size=batch_size, criterion=nn.NLLLoss(), optimizer=optim.Adam(model.parameters()), valid_set=valid_set) return network
def train(trainDat, trainLabs, R, hyperparam): ''' Train a classifier of your choice on the data. @param trainDat an ndarray of training data, one row per example @param trainLabs an ndarray of training data labels, one row per training example @param R the input to the ANN classification algorithm (radius) @param hyperparam the hyper-parameter to train with (number of neighbors to use) @raise LogicalError if the training labels do not match the training data @return the trained classifier ''' if len(trainLabs) != len(trainDat): raise LogicalError, "Method \"train\": training labels don't match with training data." classifier = ANN(R, hyperparam) # TODO: Solve the radius situation classifier = classifier.train(trainDat, trainLabs) return classifier
def merge(results, in_layer, hidden_layers, out_layer): ann = ANN(in_layer, hidden_layers, out_layer) results.sort(key=lambda x: x[1], reverse=True) ratings = map(lambda x: x[1], results) i_h1_list = map(lambda i: i[0].i_h1_weights, results) ann.i_h1_weights = iterate_weights(ratings, i_h1_list) if len(hidden_layers) > 1: h1_h2_list = map(lambda i: i[0].h1_h2_weights, results) ann.h1_h2_weights = iterate_weights(ratings, h1_h2_list) h2_o_list = map(lambda i: i[0].h2_o_weights, results) ann.h2_o_weights = iterate_weights(ratings, h2_o_list) else: h1_o_list = map(lambda i: i[0].h1_o_weights, results) ann.h1_o_weights = iterate_weights(ratings, h1_o_list) return ann
def _init_network_from_file(loaded_file, activation, hidden_layer_size, sigma, mu): training_data, validation_data, test_data = loaded_file training_x, training_y = training_data label_num = np.max(training_y) + 1 input_size = np.shape(training_x)[1] return ANN([input_size, hidden_layer_size, label_num], activations=[activation, ActivationImpl.sigmoid], mu=mu, sigma=sigma)
class Animal: def __init__(self, pos=[0, 0], score=0, num_layers=3, bias=1, activation=0, layers=[20, 16, 4], symbol=u'\u0041'): self.pos = pos self.old_pos = pos self.score = score if not(num_layers == len(layers)) and len(layers) > 2: num_layers = len(layers) self.ann = ANN(num_layers, bias, activation, layers) self.symbol = symbol self.status = 0 # 0 for moving or 1 for paused (stunned) self.scored = False def move(self, inputs=[]): if self.status == 1: self.status = 0 return self.ann.run(inputs) # stunning disabled # return [0 for _ in range(self.ann.layers[self.ann.num_layers - 1])] # stunning enabled return self.ann.run(inputs)
def train_model(): dataset = np.genfromtxt('mnist.txt') input_layer_size = 28 * 28 #there are going to be as much input neurons as there are pixels in each image num_classes = 10 num_hidden_layers = 1 global hidden_layer_size #not considering bias units so a + 1 size in each layer should always be taken into consideration global num_epochs training_set_size = 900 test_set_size = 100 num_layers = num_hidden_layers + 1 global learning_rate labels, dataset = split_labels(dataset) neural_net = ANN(input_layer_size, num_classes, num_hidden_layers, hidden_layer_size, learning_rate, num_layers, dataset, labels) train(neural_net, dataset, num_epochs) neural_net.save_weights() plot_error_registry(neural_net.error_registry)
def train(data_x, data_y): N, D_in, H, D_out = len(data_x), len(data_x[1, :]), 100, 1 x = Variable(torch.FloatTensor(data_x)) y = Variable(torch.FloatTensor(data_y), requires_grad=False) model = ANN(D_in, H, D_out) criterion = torch.nn.MSELoss(size_average=False) optimizer = torch.optim.SGD(model.parameters(), lr=1e-4) for t in range(500): y_pred = model(x) loss = criterion(y_pred, y) optimizer.zero_grad() loss.backward() optimizer.step() torch.save(model, 'ANN.pt')
def MLP_256_Relu_Dropout_256_Relu(self, data, test_set=None): input_sizes, output_size, train_set, valid_set = data input_size = 1 for dim in input_sizes: input_size *= dim hidden_sizes = [256, 256] model = nn.Sequential(Flatten(), nn.Linear(input_size, hidden_sizes[0]), nn.ReLU(), nn.Dropout(p=0.5), nn.Linear(hidden_sizes[0], hidden_sizes[1]), nn.ReLU(), nn.Linear(hidden_sizes[1], output_size), nn.LogSoftmax(dim=1)).cuda() network = ANN("MLP_256_Relu_Dropout_256_Relu", model, cuda=True) network.train(train_set, epochs=40, batch_size=50, criterion=nn.NLLLoss(), optimizer=optim.Adam(model.parameters()), valid_set=valid_set) return network
def create_ann(self, name, alpha, layers, activation, loss): optimizer = Adam(lr=alpha) ## Create an ANN instance. ann = ANN(name, input_dim=self.ns, output_dim=self.na, activation=activation, layers=layers, loss=loss, optimizer=optimizer) return ann
class ANNWrapper: def __init__(self, in_l, hidden, ot_l, data_file): self.ANN = ANN(in_l, hidden, ot_l) self.get_data(open(data_file)) def get_data(self, data_file): parser = DataParser(data_file) sets = parser.get_training_sets(1) #one set, so take the first of the trainings sets #and take the first of that (i.e. not the test set) self.training_set = sets[0][0] self.test_set = parser.get_test_set() def train(self): self._test() for i in range(10): self._train_epoch() self._test() def _train_epoch(self): random.shuffle(self.training_set) for vector in self.training_set: self.ANN.train(vector) def _test(self): correct = 0 for vector in self.test_set: out = self.ANN.run(vector[0])[0] # print 'produced:', out, 'expected:', vector[1][0] #check which it's closest to for this instead if (out < 0 and vector[1][0] == -1) or (out > 0 and vector[1][0] == 1): correct += 1 print "Correct:", float(correct)/len(self.test_set) def classify(self): pass
class ANNWrapper: def __init__(self, in_l, hidden, ot_l, data_file): self.ANN = ANN(in_l, hidden, ot_l) self.get_data(open(data_file)) def get_data(self, data_file): parser = DataParser(data_file) sets = parser.get_training_sets(1) #one set, so take the first of the trainings sets #and take the first of that (i.e. not the test set) self.training_set = sets[0][0] self.test_set = parser.get_test_set() def train(self): self._test() for i in range(10): self._train_epoch() self._test() def _train_epoch(self): random.shuffle(self.training_set) for vector in self.training_set: self.ANN.train(vector) def _test(self): correct = 0 for vector in self.test_set: out = self.ANN.run(vector[0])[0] # print 'produced:', out, 'expected:', vector[1][0] #check which it's closest to for this instead if (out < 0 and vector[1][0] == -1) or (out > 0 and vector[1][0] == 1): correct += 1 print "Correct:", float(correct) / len(self.test_set) def classify(self): pass
def CNN_LSTM256_FC512(self, data, test_set=None): input_sizes, output_size, train_set, valid_set = data hidden_layer = 512 model = nn.Sequential( nn.Conv2d(in_channels=1, out_channels=4, kernel_size=5, stride=1, padding=2), #nn.BatchNorm2d(16), nn.ReLU(), nn.Conv2d(in_channels=4, out_channels=4, kernel_size=5, stride=1, padding=2), #nn.BatchNorm2d(16), nn.ReLU(), CNN_TO_LSTM(), nn.BatchNorm1d(input_sizes[1]), nn.LSTM(input_sizes[0] * 4, hidden_layer, batch_first=True), LSTM_Out(), nn.BatchNorm1d(hidden_layer), nn.Dropout(), nn.Linear(hidden_layer, 256), nn.ReLU(), nn.Dropout(), nn.Linear(256, output_size), #nn.BatchNorm1d(output_size), nn.LogSoftmax(dim=1)).cuda() network = ANN("CNN_LSTM256_FC512", model, cuda=True) network.train(train_set, epochs=60, batch_size=50, criterion=nn.NLLLoss(), optimizer=optim.Adam(model.parameters(), weight_decay=1e-6), valid_set=valid_set) return network
def ensemble_model_from_file(): input_layer_size = 28 * 28 #there are going to be as much input neurons as there are pixels in each image num_classes = 10 num_hidden_layers = 1 global hidden_layer_size #not considering bias units so a + 1 size in each layer should always be taken into account global num_epochs training_set_size = 900 test_set_size = 100 num_layers = num_hidden_layers + 1 global learning_rate neural_net = ANN(input_layer_size, num_classes, num_hidden_layers, hidden_layer_size, learning_rate, num_layers, [], []) neural_net.load_weights_from_memory() print('Current model architecture: \n') print('Epochs = ', num_epochs) print('Alpha_rate = ', neural_net.learning_rate) print('Hidden layers = ', neural_net.num_hidden_layers) print('Input layer size = ', neural_net.input_layer_size) print('Hidden layer size = ', neural_net.hidden_layer_size) print('Output layer size = ', neural_net.output_layer_size) return neural_net
def runGame(num_inputs, num_hidden_nodes, num_outputs, weights): ## num_inputs = config.nnet['n_inputs'] ## num_hidden_nodes = config.nnet['n_h_neurons'] ## num_outputs = config.nnet['n_outputs'] my_game = game.Game() for i in range(config.game['n_agents']): ann = ANN(num_inputs, num_hidden_nodes, num_outputs, weights) my_game.add_agent(ann) while True: my_game.game_loop(True) pygame.quit()
def run_trials(self, filename, model, iterations, inputs, outputs): parser = ANN() train_acc = [0] test_acc = [0] train_cm = [0, 0, 0, 0] test_cm = [0, 0, 0, 0] for i in range(iterations): x_train, x_test, y_train, y_test = parser.split_data( inputs, outputs, .25) model.train(x_train, y_train) train_a, train_conf = self.test_sample(model, x_train, y_train) test_a, test_conf = self.test_sample(model, x_test, y_test) train_cm, train_acc = self.update_metrics(train_cm, train_conf, train_acc, train_a) test_cm, test_acc = self.update_metrics(test_cm, test_conf, test_acc, test_a) test_metrics = self.get_result_metrics(filename, model, "test", test_acc, test_cm, iterations) train_metrics = self.get_result_metrics(filename, model, "train", train_acc, train_cm, iterations) self.export_results(filename, test_metrics, train_metrics) return
[0, 1], [1, 0] ] } } print("*****Building Model*****") #Create all of the layers each with a unique starting random state. rng = np.random.RandomState(np.random.randint(low=10, high=10000)) FeatureLayerIn = ANN(Xc=3 , Hc=6, rng=rng) rng = np.random.RandomState(np.random.randint(low=10, high=10000)) FeatureLayerOut = ANN(Xc=6 , Hc=3, rng=rng) rng = np.random.RandomState(np.random.randint(low=10, high=10000)) ContextIn = RNN(Xc=3 , Hc=6, rng=rng) rng = np.random.RandomState(np.random.randint(low=10, high=10000)) ContextOut = RNN(Xc=6 , Hc=2, rng=rng) rng = np.random.RandomState(np.random.randint(low=10, high=10000)) AnalysisLayer = ANN(Xc=5 , Hc=2, rng=rng) epoch = 0 #Iteration counter
testIndices = indices[500:1000] validData_new = validData[validIndices] validLabels_new = validLabels[validIndices] testData = validData[testIndices] testLabels = validLabels[testIndices] print "Split performed!" print "#Validation examples: " + str(len(validData_new)) print "#Testing examples: " + str(len(testData)) # Step 3: train both classifiers on the entire array of data concatData = np.concatenate([trainData, validData_new]) concatLabs = np.concatenate([trainLabels, validLabels_new]) ANN_cl = ANN(20600, 7) LSH_ANN_cl = LSH_ANN(80, 15, 5) start = t.time() # milliseconds since epoch ANN_cl.train(concatData, concatLabs) end = t.time() print "Took us " + str(end - start) + " milliseconds to train the ANN classifier." start = t.time() LSH_ANN_cl.train(concatData, concatLabs) end = t.time() print "Took us " + str(end - start) + " milliseconds to train the LSH_ANN classifier." # Step 4: test and compare classifiers on testing data. start = t.time()
def __init__(self, in_l, hidden, ot_l, data_file): self.ANN = ANN(in_l, hidden, ot_l) self.get_data(open(data_file))