class CooCoo::TemporalNetwork
Attributes
backprop_limit[RW]
network[R]
Public Class Methods
from_hash(h)
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 125 def self.from_hash(h) net = CooCoo::Network.from_hash(h) self.new(network: net) end
new(opts = Hash.new)
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 13 def initialize(opts = Hash.new) @network = opts.fetch(:network) { CooCoo::Network.new } @backprop_limit = opts[:backprop_limit] end
Public Instance Methods
adjust_weights!(deltas)
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 107 def adjust_weights!(deltas) @network.adjust_weights!(deltas) self end
backprop(inputs, outputs, errors, hidden_state = nil)
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 85 def backprop(inputs, outputs, errors, hidden_state = nil) errors = Sequence.new(outputs.size) { errors / outputs.size.to_f } unless errors.kind_of?(Sequence) o = outputs.zip(inputs, errors).reverse.collect do |output, input, err| output, hidden_state = @network.backprop(input, output, err, hidden_state) output end.reverse return Sequence[o], hidden_state end
final_output(outputs)
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 47 def final_output(outputs) CooCoo::Sequence[outputs.collect { |o| @network.final_output(o) }] end
forward(input, hidden_state = nil, flattened = false)
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 51 def forward(input, hidden_state = nil, flattened = false) if input.kind_of?(Enumerable) o = input.collect do |i| output, hidden_state = @network.forward(i, hidden_state, flattened) output end return CooCoo::Sequence[o], hidden_state else @network.forward(input, hidden_state, flattened) end end
layer(*args)
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 18 def layer(*args) @network.layer(*args) self end
layers()
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 23 def layers @network.layers end
learn(input, expecting, rate, cost_function = CostFunctions::MeanSquare, hidden_state = nil)
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 77 def learn(input, expecting, rate, cost_function = CostFunctions::MeanSquare, hidden_state = nil) expecting.zip(input).each do |target, input| n, hidden_state = @network.learn(input, target, rate, cost_function, hidden_state) end return self, hidden_state end
predict(input, hidden_state = nil, flattened = false)
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 64 def predict(input, hidden_state = nil, flattened = false) if input.kind_of?(Enumerable) o = input.collect do |i| outputs, hidden_state = @network.predict(i, hidden_state, flattened) outputs end return o, hidden_state else @network.predict(input, hidden_state, flattened) end end
prep_input(input)
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 27 def prep_input(input) if input.kind_of?(Enumerable) CooCoo::Sequence[input.collect do |i| @network.prep_input(i) end] else @network.prep_input(input) end end
prep_output_target(target)
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 37 def prep_output_target(target) if target.kind_of?(Enumerable) CooCoo::Sequence[target.collect do |t| @network.prep_output_target(t) end] else @network.prep_output_target(target) end end
to_hash()
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 116 def to_hash @network.to_hash.merge({ type: self.class.name }) end
update_from_hash!(h)
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 120 def update_from_hash!(h) @network.update_from_hash!(h) self end
update_weights!(inputs, outputs, deltas)
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 112 def update_weights!(inputs, outputs, deltas) adjust_weights!(weight_deltas(inputs, outputs, deltas)) end
weight_deltas(inputs, outputs, deltas)
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 96 def weight_deltas(inputs, outputs, deltas) e = inputs.zip(outputs, deltas) e = e.last(@backprop_limit) if @backprop_limit deltas = e.collect do |input, output, delta| @network.weight_deltas(input, output, delta) end accumulate_deltas(deltas) end
Private Instance Methods
accumulate_deltas(deltas)
click to toggle source
# File lib/coo-coo/temporal_network.rb, line 131 def accumulate_deltas(deltas) weight = 1.0 / deltas.size.to_f acc = deltas[0] deltas[1, deltas.size].each do |step| step.each_with_index do |layer, i| acc[i] += layer * weight end end acc end