Ejemplo n.º 1
0
def test_or_nn(verbose=0):
    x, y = create_or_nn_data()
    nn = NeuralNetwork(x, y, 4, 1)
    nn.feedforward()
    if verbose > 0:
        print("OR 1 " + str(nn.loss()))
        print("NN output " + str(nn._output))
        print(nn.accuracy_precision())
    assert nn.loss() < .04
Ejemplo n.º 2
0
def test_ttt_nn(verbose=0):
    x, y = load_tictactoe_csv("tic-tac-toe-1.csv")
    nn = NeuralNetwork(x, y, 4, .1)
    nn.load_4_layer_ttt_network()
    nn.feedforward()
    if verbose > 0:
        print("NN 1 " + str(nn.loss()))
        print("NN output " + str(nn._output))
        print(nn.accuracy_precision())
    assert nn.loss() < .02
Ejemplo n.º 3
0
class NeuralNetworkEvaluator(Evaluator):
    def __init__(self, weights=None):
        self.weights = weights
        super().__init__(weights)
        self.network = NeuralNetwork(input_shape=(1, 22))
        self.genome_len = self.network.num_nodes
        if self.weights is not None:
            self.network.weights = self.network.weights_to_ndarray(self.weights)
        else:
            self.weights = self.network.weights_to_list()
            self.genome = self.weights

    def eval(self, engine):
        self.engine = engine
        state_eval = 0
        if self.engine.game_state is not None:
            if self.engine.game_state == 0:
                return 0
            else:
                return 10000 if self.engine.game_state == 1 else -10000
        state_eval = self.gene_eval()
        return state_eval

    def gene_eval(self):
        feature_vec = []
        for player in [1, 2]:
            player_vec = []
            if self.engine.game_state is None:
                self.player = player
            for feature in self.features:
                player_vec.append(feature())
            feature_vec.extend(player_vec)
        return self.network.feedforward(feature_vec)
Ejemplo n.º 4
0
def run_network(learning_rate, hidden_nodes):
    brain = NeuralNetwork(784, hidden_nodes, 10)
    brain.set_learning_rate(learning_rate)
    counter = 0

    for image, label in zip(training_images, training_labels):
        counter += 1
        print(
            f"Backward propogating the {counter} image, which was a {label} with array: {labels_dict[label]}."
        )
        brain.train(image.ravel(), labels_dict[label])
        #if counter > 20_000: break
        #print(brain.weights_ho[2:3, 2:4])

    correct = 0
    for i in range(len(testing_images)):
        output = brain.feedforward(testing_images[i].ravel())
        output = output.ravel()
        #print(testing_images[i].ravel())
        print(output)
        print(
            f"The letter was thought to be {output.argmax()} by the Neural network, but was actually {testing_labels[i]}."
        )
        if output.argmax() == testing_labels[i]:
            correct += 1
            print("IT WAS CORRECT!")
    print(correct / len(testing_images))
    return correct / len(testing_images)
Ejemplo n.º 5
0
def test(x):

    correct_pred = 0
    for i in range(x):

        brain = NeuralNetwork(2, 4, 1)
        for i in range(100000):
            j = randint(0, 3)
            brain.train(inputs[j], targets[j])

        t1 = output(brain.feedforward([0, 0])[0][0])
        t2 = output(brain.feedforward([1, 1])[0][0])
        t3 = output(brain.feedforward([0, 1])[0][0])
        t4 = output(brain.feedforward([1, 0])[0][0])

        print(t1, t2, t3, t4)

        if targets[0][0] == t1 and targets[1][0] == t2 and targets[2][
                0] == t3 and targets[3][0] == t4:
            correct_pred += 1

    classification_rate = (correct_pred / x) * 100
    message = "Classification rate: " + str(classification_rate) + "%"
    return message
Ejemplo n.º 6
0
]

targets = [
    [0],
    [1],
    [1],
    [0],
]


for _ in range(10000):
    i = randint(0, len(data) - 1)
    nn.train(data[i], targets[i], 0.1)


print(nn.feedforward(data[0]).rows)
print(nn.feedforward(data[1]).rows)
print(nn.feedforward(data[2]).rows)
print(nn.feedforward(data[3]).rows)
print("----")

# NOT example

nn = NeuralNetwork([1, 1, 1])

data = [[0], [1]]
targets = [[1], [0]]

for _ in range(0):
    i = randint(0, len(data) - 1)
    nn.train(data[i], targets[i], 0.1)
Ejemplo n.º 7
0
    return True

  if output == [0, 1]:
    return False

nn = NeuralNetwork(2, 1, 2)

# start training for AND operation
for i in range(1000):
  nn.train([1, 1], [1, 0])
  nn.train([1, 0], [0, 1])
  nn.train([0, 0], [0, 1])
  nn.train([0, 1], [0, 1])
# end training
print('AND OPERATION')
print([1,0], ' => ', pretty_print(nn.feedforward([1,0])))
print([1,1], ' => ', pretty_print(nn.feedforward([1,1])))
print([0,1], ' => ', pretty_print(nn.feedforward([0,1])))
print([0,0], ' => ', pretty_print(nn.feedforward([0,0])))


#start training for XOR operation
nn = NeuralNetwork(2, 2, 1)
training_data = [
  {'i': [1, 1], 'o': [0]},
  {'i': [1, 0], 'o': [1]},
  {'i': [0, 0], 'o': [0]},
  {'i': [0, 1], 'o': [1]},
]
for i in range(50000):
  data = training_data[random.randint(0, 3)]