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(rnn) ⇒ SimpleRNN_Dense

Returns a new instance of SimpleRNN_Dense.



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

def initialize(rnn)
  @rnn = rnn
  @activation = rnn.activation.clone
end

Instance Method Details

#backward(dh2) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/dnn/core/rnn_layers.rb', line 138

def backward(dh2)
  dh2 = @activation.backward(dh2)
  @rnn.weight.grad += @x.transpose.dot(dh2)
  @rnn.weight2.grad += @h.transpose.dot(dh2)
  if @rnn.l1_lambda > 0
    @rnn.weight.grad += dlasso
    @rnn.weight2.grad += dlasso2
  elsif @rnn.l2_lambda > 0
    @rnn.weight.grad += dridge
    @rnn.weight2.grad += dridge2
  end
  @rnn.bias.grad += dh2.sum(0)
  dx = dh2.dot(@rnn.weight.data.transpose)
  dh = dh2.dot(@rnn.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(@rnn.weight.data) + h.dot(@rnn.weight2.data) + @rnn.bias.data
  @activation.forward(h2)
end