forked from macournoyer/nn.rb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxor.rb
56 lines (41 loc) · 1.08 KB
/
xor.rb
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
require_relative "nn"
# Make rand deterministic, for testing purposes
srand 0
## Create the network
inputs_count = 2
hiddens_count = 20
outputs_count = 1
network = Network.new(
Layer.new(inputs_count, hiddens_count),
Layer.new(hiddens_count, outputs_count)
)
## Training
examples = [
# _ XOR _ = _
[ [ 0, 0 ].to_v, [ 0 ].to_v ],
[ [ 0, 1 ].to_v, [ 1 ].to_v ],
[ [ 1, 0 ].to_v, [ 1 ].to_v ],
[ [ 1, 1 ].to_v, [ 0 ].to_v ]
]
(1..1000).each do |epoch|
if epoch % 100 == 0
puts "----------- epoch #{epoch} -----------"
end
examples.each do |inputs, expected_outputs|
# Forward pass (evaluate / predict)
actual_outputs = network.forward(inputs)
# Compute the errors. By how much did we miss?
errors = actual_outputs - expected_outputs
if epoch % 100 == 0
puts " %d XOR %d = %.2f error = %+.2f" % [
inputs[0], inputs[1],
actual_outputs[0],
errors[0]
]
end
# Back-propagate the errors
network.back_propagate errors
# Update the weights
network.update_weights
end
end