-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuron.rb
51 lines (41 loc) · 1.05 KB
/
neuron.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
class Neuron
attr_reader :last_output, :weights
attr_accessor :delta
def initialize(number_of_inputs)
create_weights(number_of_inputs)
end
def fire(input)
@last_output = activation_function(input)
end
def update_weight(inputs, training_rate)
inputs << -1 # Add the bias
@weights.each_index do |i|
@weights[i] += training_rate * delta * inputs[i]
end
end
def inspect
@weights
end
private
def activation_function(input)
sum = 0
input.each_with_index do |n, index|
# puts "index:#{index} weight: #{@weights[index]} input: #{n} input_size: #{input.size}"
sum += @weights[index] * n
end
sum += @weights.last * -1 #bias node
sigmoid_function(sum)
end
# g(h) = 1 / (1+exp(-B*h(j)))
def sigmoid_function(x)
1 / (1+Math.exp(-1 * (x)))
end
def create_weights(number_of_inputs)
# Create random weights between 0 & 1
# Plus another one for the bias node
@weights = []
(number_of_inputs + 1).times do
@weights << (rand > 0.5 ? -rand : rand)
end
end
end