-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.rb
56 lines (47 loc) · 1.39 KB
/
layer.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 './neuron.rb'
require_relative './connection'
class Layer
attr_accessor :neurons
attr_accessor :income_connections
attr_accessor :outcome_connections
def initialize(number_of_neurons)
@income_connections = []
@outcome_connections = []
@neurons = (1..number_of_neurons).map do
Neuron.new
end
end
def create_connections_with(previous_layer)
previous_layer.neurons.map do |previous_layer_neuron|
neurons.each do |current_layer_neuron|
@income_connections.push(
build_connection(previous_layer_neuron, current_layer_neuron)
)
end
end
previous_layer.outcome_connections = @income_connections
end
def train
neurons.each do |neuron|
neuron.input_connections.each(&:calculate_gradient)
neuron.input_connections.each(&:calculate_delta_change)
neuron.input_connections.each(&:update_weight)
end
end
def calculate_delta_for_neurons
neurons.each(&:calculate_delta)
end
def process_all_neurons
@neurons.each(&:process)
end
def connections
return if @income_connections.nil? || @outcome_connections.nil?
@income_connections + @outcome_connections
end
private
def build_connection(input, output)
conn = Connection.new(start_neuron: input, end_neuron: output, layer: self)
input.output_connections.push(conn)
output.input_connections.push(conn)
end
end