-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.rb
114 lines (95 loc) · 2.67 KB
/
net.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require_relative './layer'
require_relative './input_layer'
require_relative './output_layer'
class Net
attr_accessor :layers
def initialize(layers_representation, batch_size, classes)
@input_layer = InputLayer.new(batch_size)
@global_error = 1
@output_layer = OutputLayer.new(classes.size)
@classes = classes
@layers = [@input_layer]
layers_representation.map do |l|
new_layer = Layer.new(l)
new_layer.create_connections_with(@layers.last)
@layers.push(new_layer)
end
@output_layer.create_connections_with(@layers.last)
layers.push(@output_layer)
end
def process(input)
put_data_into_net(input)
hidden_layers_and_output_layer.each(&:process_all_neurons)
output = {}
results = @output_layer.result
@classes.each_with_index do |c, index|
output[c] = results[index]
end
output
end
def train_network(inputs, expected_output)
inputs.each_with_index do |input, index|
process(input)
@global_error = calculate_global_error(expected_output[index])
p "GLOBAL ERROR IS #{@global_error}" if (index % 100).zero?
back_propogation(expected_output[index])
end
end
def save_network(name)
f = File.open(name, 'w')
f.write(Marshal.dump(self))
end
def self.load_network(name)
f = File.read(name)
Marshal.load(f)
end
def marshal_dump
[@layers, @input_layer, @output_layer, @classes, @global_error]
end
def marshal_load(net)
@layers, @input_layer, @output_layer, @classes, @global_error = net
end
private
def back_propogation(expected_results)
@output_layer.neurons.each_with_index do |neuron, index|
error = neuron.result - expected_results[index]
neuron.delta = -error * neuron.result
end
@output_layer.train
hidden_layers_and_input_layer.reverse_each(&:calculate_delta_for_neurons)
hidden_layers_and_input_layer.reverse_each(&:train)
end
def calculate_global_error(expected_result)
sum = 0
@output_layer.result.each_with_index do |result, index|
sum += (expected_result[index] - result)**2.0
end
sum / expected_result.length
end
def put_data_into_net(array_of_digits)
input_connections.each_with_index do |neuron, index|
neuron.result = array_of_digits[index]
end
end
def all_neurons
@layers.map(&:neurons).flatten
end
def connections
@layers.map(&:connections).compact.flatten
end
def network_result
@output_layer.result
end
def input_connections
@input_layer.neurons
end
def hidden_layers
@layers[1..-2]
end
def hidden_layers_and_input_layer
@layers[0..-1]
end
def hidden_layers_and_output_layer
@layers[1..-1]
end
end