Class: CooCoo::Recurrence::Frontend
- Defined in:
- lib/coo-coo/recurrence/frontend.rb
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #activation_function ⇒ Object
- #adjust_weights!(deltas) ⇒ Object
- #backend ⇒ Object
- #backend=(layer) ⇒ Object
- #backprop(input, outputs, errors, hidden_state) ⇒ Object
- #forward(inputs, hidden_state) ⇒ Object
-
#initialize(num_inputs, num_recurrent_outputs) ⇒ Frontend
constructor
A new instance of Frontend.
- #num_inputs ⇒ Object
- #recurrent_size ⇒ Object
- #size ⇒ Object
- #to_hash(network = nil) ⇒ Object
- #transfer_error(deltas) ⇒ Object
- #update_from_hash!(h) ⇒ Object
- #weight_deltas(inputs, deltas) ⇒ Object
Constructor Details
#initialize(num_inputs, num_recurrent_outputs) ⇒ Frontend
Returns a new instance of Frontend.
10 11 12 13 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 10 def initialize(num_inputs, num_recurrent_outputs) @num_inputs = num_inputs @num_recurrent_outputs = num_recurrent_outputs end |
Class Method Details
.from_hash(h, network = nil) ⇒ Object
91 92 93 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 91 def self.from_hash(h, network = nil) self.new(h.fetch(:inputs), h.fetch(:recurrent_outputs)).update_from_hash!(h) end |
Instance Method Details
#==(other) ⇒ Object
71 72 73 74 75 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 71 def ==(other) other.kind_of?(self.class) && num_inputs == other.num_inputs && recurrent_size == other.recurrent_size end |
#activation_function ⇒ Object
19 20 21 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 19 def activation_function nil end |
#adjust_weights!(deltas) ⇒ Object
67 68 69 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 67 def adjust_weights!(deltas) self end |
#backend ⇒ Object
31 32 33 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 31 def backend @backend ||= Backend.new(self, @num_inputs, recurrent_size) end |
#backend=(layer) ⇒ Object
35 36 37 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 35 def backend=(layer) @backend = layer end |
#backprop(input, outputs, errors, hidden_state) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 45 def backprop(input, outputs, errors, hidden_state) # split for real and recurrent errors norm_errors = errors[0, num_inputs] recurrent_errors = errors[num_inputs, recurrent_size] # buffer the recurrent output hidden_state ||= Hash.new hidden_state[self] ||= Array.new hidden_state[self].push(recurrent_errors) # return real errors return norm_errors, hidden_state end |
#forward(inputs, hidden_state) ⇒ Object
39 40 41 42 43 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 39 def forward(inputs, hidden_state) layer_state = hidden_state[@backend] recurrent_input = layer_state && layer_state.pop return inputs.append(recurrent_input || empty_input), hidden_state end |
#num_inputs ⇒ Object
15 16 17 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 15 def num_inputs @num_inputs end |
#recurrent_size ⇒ Object
27 28 29 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 27 def recurrent_size @num_recurrent_outputs end |
#size ⇒ Object
23 24 25 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 23 def size @num_inputs + recurrent_size end |
#to_hash(network = nil) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 77 def to_hash(network = nil) { type: self.class.name, inputs: @num_inputs, recurrent_outputs: @num_recurrent_outputs } end |
#transfer_error(deltas) ⇒ Object
59 60 61 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 59 def transfer_error(deltas) deltas end |
#update_from_hash!(h) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 84 def update_from_hash!(h) @num_inputs = h.fetch(:inputs) @num_recurrent_outputs = h.fetch(:recurrent_outputs) self end |
#weight_deltas(inputs, deltas) ⇒ Object
63 64 65 |
# File 'lib/coo-coo/recurrence/frontend.rb', line 63 def weight_deltas(inputs, deltas) inputs * deltas end |