Class: DNN::Layers::SimpleRNNCell

Inherits:
RNNCell
  • Object
show all
Defined in:
lib/dnn/core/layers/rnn_layers.rb

Instance Attribute Summary

Attributes inherited from RNNCell

#trainable

Instance Method Summary collapse

Constructor Details

#initialize(weight, recurrent_weight, bias, activation) ⇒ SimpleRNNCell

Returns a new instance of SimpleRNNCell.



151
152
153
154
# File 'lib/dnn/core/layers/rnn_layers.rb', line 151

def initialize(weight, recurrent_weight, bias, activation)
  super(weight, recurrent_weight, bias)
  @activation = activation.clone
end

Instance Method Details

#backward(dh2) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/dnn/core/layers/rnn_layers.rb', line 164

def backward(dh2)
  dh2 = @activation.backward_node(dh2)
  if @trainable
    @weight.grad += @x.transpose.dot(dh2)
    @recurrent_weight.grad += @h.transpose.dot(dh2)
    @bias.grad += dh2.sum(0) if @bias
  end
  dx = dh2.dot(@weight.data.transpose)
  dh = dh2.dot(@recurrent_weight.data.transpose)
  [dx, dh]
end

#forward(x, h) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/dnn/core/layers/rnn_layers.rb', line 156

def forward(x, h)
  @x = x
  @h = h
  h2 = x.dot(@weight.data) + h.dot(@recurrent_weight.data)
  h2 += @bias.data if @bias
  @activation.forward_node(h2)
end