Class: CooCoo::Recurrence::Backend

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recurrence_layer, outputs, recurrent_outputs) ⇒ Backend

Returns a new instance of Backend.



11
12
13
14
15
# File 'lib/coo-coo/recurrence/backend.rb', line 11

def initialize(recurrence_layer, outputs, recurrent_outputs)
  @recurrence_layer = recurrence_layer
  @outputs = outputs
  @recurrent_size = recurrent_outputs
end

Instance Attribute Details

#recurrence_layerObject (readonly)

Returns the value of attribute recurrence_layer.



9
10
11
# File 'lib/coo-coo/recurrence/backend.rb', line 9

def recurrence_layer
  @recurrence_layer
end

Class Method Details

.from_hash(h, network) ⇒ Object

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/coo-coo/recurrence/backend.rb', line 84

def self.from_hash(h, network)
  frontend = network.layers[h.fetch(:recurrence_layer)]
  raise ArgumentError.new("Frontend not found") unless frontend
  
  layer = self.new(frontend,
                   h.fetch(:outputs),
                   h.fetch(:recurrent_size)).
    update_from_hash!(h)

  frontend.backend = layer
  
  layer
end

Instance Method Details

#==(other) ⇒ Object



71
72
73
74
75
76
# File 'lib/coo-coo/recurrence/backend.rb', line 71

def ==(other)
  other.kind_of?(self.class) &&
    size = other.size &&
    recurrence_layer == other.recurrence_layer &&
    recurrent_size == other.recurrent_size
end

#activation_functionObject



29
30
31
# File 'lib/coo-coo/recurrence/backend.rb', line 29

def activation_function
  nil
end

#adjust_weights!(deltas) ⇒ Object



55
56
57
# File 'lib/coo-coo/recurrence/backend.rb', line 55

def adjust_weights!(deltas)
  self
end

#backprop(input, output, errors, hidden_state) ⇒ Object



41
42
43
44
45
# File 'lib/coo-coo/recurrence/backend.rb', line 41

def backprop(input, output, errors, hidden_state)
  layer_state = hidden_state[@recurrence_layer]
  rec_errors = (layer_state && layer_state.pop) || CooCoo::Vector.zeros(recurrent_size)
  return errors.append(rec_errors), hidden_state
end

#forward(inputs, hidden_state) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/coo-coo/recurrence/backend.rb', line 33

def forward(inputs, hidden_state)
  hidden_state ||= Hash.new
  hidden_state[self] ||= Array.new
  hidden_state[self].push(inputs[size, recurrent_size])

  return inputs[0, size], hidden_state
end

#num_inputsObject



17
18
19
# File 'lib/coo-coo/recurrence/backend.rb', line 17

def num_inputs
  size + recurrent_size
end

#recurrent_sizeObject



25
26
27
# File 'lib/coo-coo/recurrence/backend.rb', line 25

def recurrent_size
  @recurrent_size
end

#sizeObject



21
22
23
# File 'lib/coo-coo/recurrence/backend.rb', line 21

def size
  @outputs
end

#to_hash(network = nil) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/coo-coo/recurrence/backend.rb', line 63

def to_hash(network = nil)
  { type: self.class.name,
    outputs: @outputs,
    recurrent_size: @recurrent_size,
    recurrence_layer: network && network.layer_index(@recurrence_layer)
  }
end

#transfer_error(deltas) ⇒ Object



47
48
49
# File 'lib/coo-coo/recurrence/backend.rb', line 47

def transfer_error(deltas)
  deltas
end

#update_from_hash!(h) ⇒ Object



78
79
80
81
82
# File 'lib/coo-coo/recurrence/backend.rb', line 78

def update_from_hash!(h)
  @outputs = h.fetch(:outputs)
  @recurrent_size = h.fetch(:recurrent_size)
  self
end

#update_weights!(inputs, deltas) ⇒ Object



51
52
53
# File 'lib/coo-coo/recurrence/backend.rb', line 51

def update_weights!(inputs, deltas)
  self
end

#weight_deltas(inputs, deltas) ⇒ Object



59
60
61
# File 'lib/coo-coo/recurrence/backend.rb', line 59

def weight_deltas(inputs, deltas)
  inputs * deltas
end