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.



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

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

Instance Method Details

#backward(dh2) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/dnn/core/rnn_layers.rb', line 138

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

#forward(x, h) ⇒ Object



131
132
133
134
135
136
# File 'lib/dnn/core/rnn_layers.rb', line 131

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