Class: CooCoo::TemporalNetwork

Inherits:
Object
  • Object
show all
Defined in:
lib/coo-coo/temporal_network.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = Hash.new) ⇒ TemporalNetwork

Returns a new instance of TemporalNetwork.



13
14
15
16
# 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

Instance Attribute Details

#backprop_limitObject

Returns the value of attribute backprop_limit.



6
7
8
# File 'lib/coo-coo/temporal_network.rb', line 6

def backprop_limit
  @backprop_limit
end

#networkObject (readonly)

Returns the value of attribute network.



5
6
7
# File 'lib/coo-coo/temporal_network.rb', line 5

def network
  @network
end

Class Method Details

.from_hash(h) ⇒ Object



125
126
127
128
# 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

Instance Method Details

#adjust_weights!(deltas) ⇒ Object



107
108
109
110
# 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) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Object



47
48
49
# 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) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object



18
19
20
21
# File 'lib/coo-coo/temporal_network.rb', line 18

def layer(*args)
  @network.layer(*args)
  self
end

#layersObject



23
24
25
# 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) ⇒ Object



77
78
79
80
81
82
83
# 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) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# 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) ⇒ Object



27
28
29
30
31
32
33
34
35
# 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) ⇒ Object



37
38
39
40
41
42
43
44
45
# 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_hashObject



116
117
118
# 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) ⇒ Object



120
121
122
123
# 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) ⇒ Object



112
113
114
# 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) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# 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