Class: DNN::Layers::SimpleRNNDense

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SimpleRNNDense.



142
143
144
145
146
147
148
# File 'lib/dnn/core/layers/rnn_layers.rb', line 142

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

Instance Attribute Details

#trainableObject

Returns the value of attribute trainable.



140
141
142
# File 'lib/dnn/core/layers/rnn_layers.rb', line 140

def trainable
  @trainable
end

Instance Method Details

#backward(dh2) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/dnn/core/layers/rnn_layers.rb', line 158

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



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

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