-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.py
66 lines (54 loc) · 2.67 KB
/
main2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from neural_network import NeuralNetwork
from formulae import calculate_average_error, seed_random_number_generator
from video import generate_writer, annotate_frame, take_still
#import main
from configurationNetwork import examples, new_situation, training_iterations, neurons_in_layers, show_iterations, video_file_name
import configurationNetwork
#class TrainingExample():
#def __init__(self, inputs, output):
#self.inputs = inputs
#self.output = output
# Seed the random number generator
seed_random_number_generator()
# Assemble a neural network, with 3 neurons (by default) in the first layer or INPUT LAYER
# 4 neurons in the second layer or HIDDEN LAYER and 1 neuron in the third layer or OUTPUT LAYER
network = NeuralNetwork(configurationNetwork.neurons_in_layers)
# Training set with inputs [a,b,c] and output
#examples = [TrainingExample([0, 0, 1], 0),
#TrainingExample([0, 1, 1], 1),
#TrainingExample([1, 0, 1], 1),
#TrainingExample([1, 1, 1], 1)]
# Create a video and image writer
fig, writer = generate_writer()
# Generate an image of the neural network before training
print ("Generating an image of the neural network before")
network.do_not_think()
network.draw()
#take_still("neural_network_before.png")
# Generate a video of the neural network learning
print ("Generating a video of the neural network learning.")
print ("There will be " + str(len(configurationNetwork.examples) * len(configurationNetwork.show_iterations)) + " frames.")
print ("This may take a long time. Please wait...")
with writer.saving(fig, configurationNetwork.video_file_name, 100):
for i in range(1, configurationNetwork.training_iterations + 1):
cumulative_error = 0
for e, example in enumerate(configurationNetwork.examples):
cumulative_error += network.train(example)
if i in configurationNetwork.show_iterations:
network.draw()
annotate_frame(i, e, average_error, example)
writer.grab_frame()
average_error = calculate_average_error(cumulative_error, len(configurationNetwork.examples))
print ("Success! Open the file " + configurationNetwork.video_file_name + " to view the video.")
# Generate an image of the neural network after training
print ("Generating an image of the neural network after")
network.do_not_think()
network.draw()
#take_still("neural_network_after.png")
# Consider a new situation
new_situation = [1, 0, 1]
print ("Considering a new situation " + str(configurationNetwork.new_situation) + "?")
print("output estimated is: ")
print (network.think(configurationNetwork.new_situation))
network.draw()
#take_still("neural_network_new_situation.png")