def test1(): X=np.array([[1,0,1,0],[1,0,1,1],[0,1,0,1]]) y=np.array([[1],[1],[0]]) nn = NeuralNet() nn.train(X, y, epochs=5000) pred = nn.predict(X) print(pred)
def train( net: NeuralNet, train_inputs: np.ndarray, train_labels: np.ndarray, input_converter: Callable, label_converter: Callable, epoch_count: int = 5000, batch_size: int = 32, learning_rate: int = 0.1): batch_iterator = BatchIterator(train_inputs, train_labels, batch_size) pbar = tqdm(total=epoch_count) for epoch in range(epoch_count): epoch_loss = 0 batch = next(batch_iterator) for input, label in batch: vector_input = input_converter(input) vector_label = label_converter(label) output = net.predict(vector_input) epoch_loss += net.loss.loss_func(output, vector_label) grad = net.loss.grad_func(output, vector_label) net.backward(grad) net.gradient_step(learning_rate / batch_size) pbar.update() pbar.set_description(desc=f"Training model. Current epoch loss: {round(epoch_loss, 2)}")
def test_predict(self): neural_net = NeuralNet( input_size=3, hidden_size=3, output_size=1, ) result = neural_net.predict([1, 1, 1]) self.assertEquals(result, 6)
def test2(): X1 = np.array([[0,0], [0,1], [1,0], [1,1]]) y1 = np.array([0,1,1,0]) nn = NeuralNet(input_layer=2, hidden_layer=4, output_layer=1) nn.train(X1, y1) pred = nn.predict(X1) print(pred)
def test(net: NeuralNet, inputs: np.ndarray, labels: np.ndarray, confusion_matrix: DataFrame, input_converter: Callable, output_converter: Callable, label_converter: Callable, title='') -> ModelEvaluator: evaluator = ModelEvaluator(confusion_matrix, title=title) pbar = tqdm(total=len(labels)) for input, label in zip(inputs, labels): output = net.predict(input_converter(input)) evaluator.receive(output_converter(output), label, net.loss.loss_func(output, label_converter(label))) pbar.update() pbar.set_description(desc=f"Testing model") return evaluator
dataset = zip(data['data'].tolist(), actual) shuffle(dataset) train = dataset[:101] test = dataset[101:] nn = NeuralNet() nn.set_layers([ Layer('input', 4), Layer('hidden', 10, "LReLU"), Layer('output', 3, "LReLU") ]) score = 0.0 for i in test: p = nn.predict(i) if p.index(max(p)) == i[1].index(1): score += 1 # Expect a value around 0.333, since there is a # 1 in 3 chance to randomly guess correctly print("Before: {}\n".format(score / len(test))) nn.train(train, epochs=1000, learning_rate=0.01) score = 0.0 for i in test: p = nn.predict(i) if p.index(max(p)) == i[1].index(1): score += 1
# Train the network using resilient backpropagation resilient_backpropagation( network, training_data, # specify the training set test_data, # specify the test set cost_function, # specify the cost function to calculate error ERROR_LIMIT = 1e-3, # define an acceptable error limit #max_iterations = (), # continues until the error limit is reach if this argument is skipped # optional parameters weight_step_max = 50., weight_step_min = 0., start_step = 0.5, learn_max = 1.2, learn_min = 0.5, save_trained_network = False # Whether to write the trained weights to disk ) # Print a network test print_test( network, training_data, cost_function ) """ Prediction Example """ prediction_set = [ Instance([0,1]), Instance([1,0]) ] prediction_set = preprocessor( prediction_set ) print network.predict( prediction_set ) # produce the output signal
ds = SemEvalData(blank=True) for j in range(num_folds): if j != i: ds = ds + datasets[j] input, tags, order = NeuralNet.format_dataset(ds, embedder) # Train the network. network = NeuralNet(hidden=128, layers=2, input=len(input[0]), output=2) # Train on non-test datasets. loss, accuracy = network.train(input, tags, iterations=100) accuracies += [accuracy] # Test on test dataset. test, _, order = NeuralNet.format_dataset(datasets[i], embedder) network_predictions.update(network.predict(test, order)) # Evaluate neural network. neural_cm = Evaluator.confidence_compare(dataset.tags, network_predictions) print("Neural Network Accuracy: " + str(round( Evaluator.accuracy(neural_cm)*100, 2)) + "%") # Save similarity measures to csv. with open(args.csv_file(), 'w') as writefile: writefile.write(embedder.to_csv(embed_predictions)) # Save neural network to csv. with open('output.csv', 'w') as writefile: writefile.write(NeuralNet.pred_to_csv(network_predictions))
searching = True for event in pygame.event.get(): if pygame.mouse.get_pressed()[0]: k = 0 # VNN.clicked(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) # et.evalGeneration() if pygame.key.get_pressed()[pygame.K_SPACE]: searching = False myVSG.snakegame.reset() VNN.nn.weights = bestnnweights VNN.nn.bias = bestnnbias if pygame.key.get_pressed()[pygame.K_LEFT]: print("Human: LEFT, AI: " + str(myVSG.snakegame.predict(myNN.predict(state.T)))) myVSG.snakegame.update(Direction.LEFT) if pygame.key.get_pressed()[pygame.K_RIGHT]: print("Human: RIGHT, AI: " + str(myVSG.snakegame.predict(myNN.predict(state.T)))) myVSG.snakegame.update(Direction.RIGHT) if pygame.key.get_pressed()[pygame.K_UP]: print("Human: UP, AI: " + str(myVSG.snakegame.predict(myNN.predict(state.T)))) myVSG.snakegame.update(Direction.UP) if pygame.key.get_pressed()[pygame.K_DOWN]: print("Human: DOWN, AI: " + str(myVSG.snakegame.predict(myNN.predict(state.T)))) myVSG.snakegame.update(Direction.DOWN) if event.type == pygame.QUIT: running = False