Class: DNN::Layers::SimpleRNN_Dense
- Inherits:
-
Object
- Object
- DNN::Layers::SimpleRNN_Dense
- Defined in:
- lib/dnn/core/rnn_layers.rb
Instance Method Summary collapse
- #backward(dh2) ⇒ Object
- #forward(x, h) ⇒ Object
-
#initialize(rnn) ⇒ SimpleRNN_Dense
constructor
A new instance of SimpleRNN_Dense.
Constructor Details
#initialize(rnn) ⇒ SimpleRNN_Dense
Returns a new instance of SimpleRNN_Dense.
124 125 126 127 |
# File 'lib/dnn/core/rnn_layers.rb', line 124 def initialize(rnn) @rnn = rnn @activation = rnn.activation.clone end |
Instance Method Details
#backward(dh2) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/dnn/core/rnn_layers.rb', line 136 def backward(dh2) dh2 = @activation.backward(dh2) @rnn.grads[:weight] += @x.transpose.dot(dh2) @rnn.grads[:weight2] += @h.transpose.dot(dh2) if @rnn.l1_lambda > 0 @rnn.grads[:weight] += dlasso @rnn.grads[:weight2] += dlasso2 elsif @rnn.l2_lambda > 0 @rnn.grads[:weight] += dridge @grads[:weight2] += dridge2 end @rnn.grads[:bias] += dh2.sum(0) dx = dh2.dot(@rnn.params[:weight].transpose) dh = dh2.dot(@rnn.params[:weight2].transpose) [dx, dh] end |
#forward(x, h) ⇒ Object
129 130 131 132 133 134 |
# File 'lib/dnn/core/rnn_layers.rb', line 129 def forward(x, h) @x = x @h = h h2 = x.dot(@rnn.params[:weight]) + h.dot(@rnn.params[:weight2]) + @rnn.params[:bias] @activation.forward(h2) end |