-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperceptron.rb
More file actions
178 lines (144 loc) · 4.56 KB
/
perceptron.rb
File metadata and controls
178 lines (144 loc) · 4.56 KB
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
# When we define a perceptron class, we need to have the number of layers, and the number of neurons in each layer
#
def sigmoid(x)
1 / (1 + Math::E**(-x))
end
class Perceptron
attr_accessor :weights,:neurons, :deltas
learning_rate = 0.005
def initialize(num_neurons, num_inputs)
#now, we initialize all the weights randomly
@weights = Array.new(num_neurons)
@neurons = Array.new(num_neurons)
@deltas = Array.new(num_neurons)
for i in 0...num_neurons
layer_length = num_neurons - i
@neurons[i] = Array.new(layer_length)
@deltas[i] = Array.new(layer_length)
for j in 0...(layer_length)
@neurons[i][j] = 0
@deltas[i][j] = 0
end
end
#now we can randomly initialize the weights
prev = num_inputs
current = num_neurons
for i in 0...num_neurons
@weights[i] = Array.new(prev)
for j in 0...prev
@weights[i][j] = Array.new(current)
for k in 0...current
@weights[i][j][k] = rand * 2 - 1
end
end
prev = current
current -= 1
end
end
def train(input, output)
feedforward(input)
backpropagate(input, output)
end
##this function will take in an input and calculate the output of the perceptron
def feedforward(input)
#calculate first layer
for i in 0...weights[0][0].length
sigmoid_input = 0
for j in 0...input.length
sigmoid_input += input[j] * weights[0][j][i]
end
#puts "neuron value " + sigmoid(sigmoid_input).to_s
@neurons[0][i] = sigmoid(sigmoid_input)
end
#then all the other layers
for i in 1...weights.length
sigmoid_input = 0
for j in 0...weights[i][0].length
for k in 0...neurons[i - 1].length
sigmoid_input += neurons[i - 1][k] * weights[i][k][j]
end
@neurons[i][j] = sigmoid(sigmoid_input)
end
end
#return the output, which is the last neuron's value
@neurons[@neurons.length - 1][0]
end
## One question worth asking is, why do we multiply the difference by the derivative?
def backpropagate(input, correct_val)
#let's calculate all the deltas first
compute_delta(correct_val)
#now we have the deltas, we can go ahead and update the weights
updateWeights(input)
end
def activation_derivative(output_val)
output_val * (1 - output_val)
end
def compute_delta(correct_val)
output_val = @neurons[@neurons.length - 1][0]
@deltas[@deltas.length - 1][0] = (correct_val - output_val) * activation_derivative(output_val)
#now go through each layer, calculating the deltas
(@deltas.length - 2).downto(0) do |i|
0.upto(@deltas[i].length - 1) do |j|
#now we need to multiply the weights associated with the errors in the next layer that came from this node
error_sum = 0
0.upto(@weights[i + 1][j].length - 1) do |k|
error_sum += @weights[i + 1][j][k] * @deltas[i + 1][k]
end
output_val = @neurons[i][j]
@deltas[i][j] = error_sum * activation_derivative(output_val)
end
end
end
def updateWeights(input)
learning_rate = 0.005
0.upto(@weights.length - 1) do |i|
0.upto(@weights[i].length - 1) do |j|
0.upto(@weights[i][j].length - 1) do |k|
if (i == 0)
@weights[i][j][k] += input[j] * @deltas[i][k] * learning_rate
else
@weights[i][j][k] += @neurons[i - 1][j] * @deltas[i][k] * learning_rate
end
end
end
end
end
def calculateRMSE(data)
error_sum = 0
data.each do |x|
error_sum += calculateSE(x[0], x[1])
end
Math.sqrt(error_sum / (data.length))
end
def calculateSE(input, output)
(feedforward(input) - output)**2
end
end
sample = Perceptron.new(4, 2)
puts sample.weights[0].length * sample.weights[0][0].length
data = []
counter = 0
File.open("ex2data1.txt", "r") do |infile|
while (line = infile.gets)
data << []
data_point = line.split(",")
data[counter] << []
# the 0th index is the input
data[counter][0] << data_point[0].to_f
data[counter][0] << data_point[1].to_f
#the 1th index is the output
data[counter] << data_point[2].to_f
counter += 1
end
end
puts sample.calculateRMSE(data)
#puts sample.calculateSE(data[0][0], data[0][1])
##Now we can train on the data
0.upto(1000) do |i|
# sample.train(data[0][0], data[0][1])
data.each do |x|
sample.train(x[0], x[1])
end
puts sample.calculateRMSE(data)
#puts sample.calculateSE(data[0][0], data[0][1])
end