class DNN::Layers::SimpleRNNDense

Attributes

trainable[RW]

Public Class Methods

new(weight, recurrent_weight, bias, activation) click to toggle source
# 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

Public Instance Methods

backward(dh2) click to toggle source
# 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) click to toggle source
# 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