-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn.rb
284 lines (230 loc) · 6.14 KB
/
nn.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# ==== Activation Function Strategy
#
#
# ====
module ActivationFunctions
class Tanh
def self.calc(val)
Math.tanh(val)
end
def self.calc_derivative(val)
1 - (Math.tanh(val) * Math.tanh(val))
end
end
class Sigmoid
def self.calc(val)
1 / (1 + Math.exp(-val))
end
def self.calc_derivative(val)
self.calc(val) * (1 - self.calc(val))
end
end
end
# ==== Layer of Neurons within a Network (1 x N)
#
#
# ====
class Layer
attr_accessor :neurons
def initialize
@neurons = []
end
def add(neuron)
@neurons.push(neuron)
end
def get(idx)
@neurons[idx]
end
def size
@neurons.length
end
end
# ==== Neuron within a dense Network.
#
#
# ====
class Neuron
attr_accessor :output
attr_accessor :gradient
attr_accessor :output_weights
attr_accessor :idx
def initialize(outputs, idx, activation, eta)
@output_weights = []
@idx = idx
@output = 0.0 # output of the Neuron
@gradient = 0.0 # for Stochastic Gradient Descent
@activation = activation # Strategy
@eta = eta # learning rate
outputs.times {
# initialize with random weight.
@output_weights.push(rand)
}
end
def sum_derivatives_of_weights(next_layer)
sum = 0.0
(0...(next_layer.size - 1)).each { |i|
sum += @output_weights[i] * next_layer.get(i).gradient
}
sum
end
def grad_output_layer(val)
delta = val - @output
@gradient = delta * @activation.calc_derivative(@output)
end
def grad_hidden_layer(next_layer)
dow = sum_derivatives_of_weights(next_layer)
@gradient = dow * @activation.calc_derivative(@output)
end
def update_input_weights(prev_layer)
(0...prev_layer.size).each { |i|
# Calculate weight delta based on gradient. Learning rate `eta` is applied here as well.
weight_delta = @eta * prev_layer.get(i).output * @gradient
prev_layer.get(i).output_weights[@idx] += weight_delta
}
end
def forward(prev_layer)
sum = 0.0
# Sum the previous output values with weight multiplied.
prev_layer.neurons.each do |neuron|
sum += (neuron.output * neuron.output_weights[@idx])
end
@output = @activation.calc(sum)
end
end
# ==== Dense Neural Network.
#
#
# ====
class Network
attr_accessor :epochs
def initialize(shape, activation, eta)
@layers = []
@epochs = 0
shape.each_with_index do |neurons_in_layer, layer_idx|
layer = Layer.new
num_outputs = neurons_in_layer == shape.last ? 0 : shape[layer_idx + 1]
# Populate Layer with Neuron(s).
# Add extra bias Layer (+1).
(neurons_in_layer + 1).times do |neuron_idx|
layer.add(Neuron.new(num_outputs, neuron_idx, activation, eta))
end
@layers.push(layer)
end
end
def forward(_X)
# Input shape must match shape of input layer.
if _X.length != @layers[0].size - 1
puts 'Error! Input shape mismatch.'
return
end
# Set output of input Neuron to input value provided.
(0..._X.length).each { |i|
@layers[0].neurons[i].output = _X[i]
}
# Feed forward starting with first hidden layer.
([email protected]).each { |i|
(0...(@layers[i].size - 1)).each { |j|
@layers[i].get(j).forward(@layers[i - 1])
}
}
end
def backprop(_y)
# Calculate gradient for output layer.
(0...(@layers.last.size - 1)).each { |i|
@layers.last.get(i).grad_output_layer(_y[i])
}
# Calculate gradient for hidden layers.
i = @layers.length - 2
while i > 0
(0...@layers[i].size).each { |j|
@layers[i].get(j).grad_hidden_layer(@layers[i + 1])
}
i = i.pred
end
# Update input weights using calculated gradients.
i = @layers.length - 1
while i > 0
(0...(@layers[i].size - 1)).each { |j|
@layers[i].get(j).update_input_weights(@layers[i - 1])
}
i = i.pred
end
end
def fit(_X, _y)
(0..@epochs).each {
(0..._X.length).each { |j|
# Optimize using SGD.
forward(_X[j])
backprop(_y[j])
}
}
end
def predict(_X)
forward(_X)
results
end
def results
res = []
(0...(@layers.last.size - 1)).each { |i|
res.push(@layers.last.get(i).output)
}
res
end
end
class NetworkTester
def main
# Goal 1: train Network to function as logical gates and observe performance.
# Goal 2: compare performance based on Strategy (ActivationFunctions) and shape of Network.
# Network setup
shape1 = [2, 4, 4, 1] # 2 input, 2 hidden, 1 output
shape2 = [2, 10, 1] # 2 input, 1 hidden, 1 output
net1 = Network.new(shape1, ActivationFunctions::Tanh, 0.15) # Tanh ActivationFunctions Strategy
net2 = Network.new(shape2, ActivationFunctions::Sigmoid, 0.15) # Sigmoid ActivationFunctions Strategy
net1.epochs = 5000
net2.epochs = 10000
# Inputs
_X = [[0, 1],
[0, 0],
[1, 0],
[1, 1]]
# **** Test 1: OR Gate ****
_y = [1, 0, 1, 1] # training data
puts "Training Net 1..."
net1.fit(_X, _y)
puts "Training Net 2..."
net2.fit(_X, _y)
puts "OR Gate Results:"
puts "Net 1 RMS Error: #{calc_rms_error(net1, _X, _y)}"
puts "Net 2 RMS Error: #{calc_rms_error(net2, _X, _y)}"
puts "-----------"
# **** Test 2: XOR Gate ****
_y = [1, 0, 1, 0] # training data
puts "Training Net 1..."
net1.fit(_X, _y)
puts "Training Net 2..."
net2.fit(_X, _y)
puts "XOR Gate Results:"
puts "Net 1 RMS Error: #{calc_rms_error(net1, _X, _y)}"
puts "Net 2 RMS Error: #{calc_rms_error(net2, _X, _y)}"
puts "-----------"
# **** Test 3: AND Gate ****
_y = [0, 0, 0, 1] # training data
puts "Training Net 1..."
net1.fit(_X, _y)
puts "Training Net 2..."
net2.fit(_X, _y)
puts "AND Gate Results:"
puts "Net 1 RMS Error: #{calc_rms_error(net1, _X, _y)}"
puts "Net 2 RMS Error: #{calc_rms_error(net2, _X, _y)}"
end
def calc_rms_error(net, input, target)
error1 = 0.0
(0...(target.length)).each { |i|
delta1 = target[i] - net.predict(input[i])[0]
error1 += delta1 * delta1
}
error1 /= target.length
Math.sqrt(error1)
end
end
NetworkTester.new.main