Class: DNN::Layers::SimpleRNN_Dense

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

Instance Method Summary collapse

Constructor Details

#initialize(weight, weight2, bias, activation) ⇒ SimpleRNN_Dense

Returns a new instance of SimpleRNN_Dense.



108
109
110
111
112
113
# File 'lib/dnn/core/rnn_layers.rb', line 108

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

Instance Method Details

#backward(dh2) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/dnn/core/rnn_layers.rb', line 123

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

#forward(x, h) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/dnn/core/rnn_layers.rb', line 115

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