Class: DNN::Layers::LSTM
- Inherits:
-
RNN
- Object
- Layer
- HasParamLayer
- Connection
- RNN
- DNN::Layers::LSTM
- Defined in:
- lib/dnn/core/rnn_layers.rb
Instance Attribute Summary collapse
-
#c ⇒ Object
Returns the value of attribute c.
Attributes inherited from RNN
Attributes inherited from Connection
Attributes inherited from HasParamLayer
Class Method Summary collapse
Instance Method Summary collapse
- #backward(dh2s) ⇒ Object
- #forward(xs) ⇒ Object
-
#initialize(num_nodes, stateful: false, return_sequences: true, weight_initializer: nil, bias_initializer: nil, l1_lambda: 0, l2_lambda: 0) ⇒ LSTM
constructor
A new instance of LSTM.
- #reset_state ⇒ Object
- #to_hash ⇒ Object
Methods inherited from RNN
#dlasso, #dlasso2, #dridge, #dridge2, #lasso, #ridge, #shape
Methods inherited from Connection
#dlasso, #dridge, #lasso, #ridge
Methods inherited from HasParamLayer
Methods inherited from Layer
#build, #built?, #prev_layer, #shape
Constructor Details
#initialize(num_nodes, stateful: false, return_sequences: true, weight_initializer: nil, bias_initializer: nil, l1_lambda: 0, l2_lambda: 0) ⇒ LSTM
Returns a new instance of LSTM.
282 283 284 285 286 287 288 289 290 291 |
# File 'lib/dnn/core/rnn_layers.rb', line 282 def initialize(num_nodes, stateful: false, return_sequences: true, weight_initializer: nil, bias_initializer: nil, l1_lambda: 0, l2_lambda: 0) super @c = nil end |
Instance Attribute Details
#c ⇒ Object
Returns the value of attribute c.
267 268 269 |
# File 'lib/dnn/core/rnn_layers.rb', line 267 def c @c end |
Class Method Details
.load_hash(hash) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/dnn/core/rnn_layers.rb', line 269 def self.load_hash(hash) lstm = self.new(hash[:num_nodes], stateful: hash[:stateful], return_sequences: hash[:return_sequences], weight_initializer: Util.load_hash(hash[:weight_initializer]), bias_initializer: Util.load_hash(hash[:bias_initializer]), l1_lambda: hash[:l1_lambda], l2_lambda: hash[:l2_lambda]) lstm.h = Xumo::SFloat.cast(hash[:h]) lstm.c = Xumo::SFloat.cast(hash[:c]) lstm end |
Instance Method Details
#backward(dh2s) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/dnn/core/rnn_layers.rb', line 314 def backward(dh2s) @grads[:weight] = Xumo::SFloat.zeros(*@params[:weight].shape) @grads[:weight2] = Xumo::SFloat.zeros(*@params[:weight2].shape) @grads[:bias] = Xumo::SFloat.zeros(*@params[:bias].shape) unless @return_sequences dh = dh2s dh2s = Xumo::SFloat.zeros(dh.shape[0], @time_length, dh.shape[1]) dh2s[true, -1, false] = dh end dxs = Xumo::SFloat.zeros(@xs_shape) dh = 0 dc = 0 (0...dh2s.shape[1]).to_a.reverse.each do |t| dh2 = dh2s[true, t, false] dx, dh, dc = @layers[t].backward(dh2 + dh, dc) dxs[true, t, false] = dx end dxs end |
#forward(xs) ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/dnn/core/rnn_layers.rb', line 293 def forward(xs) @xs_shape = xs.shape hs = Xumo::SFloat.zeros(xs.shape[0], @time_length, @num_nodes) h = nil c = nil if @stateful h = @h if @h c = @c if @c end h ||= Xumo::SFloat.zeros(xs.shape[0], @num_nodes) c ||= Xumo::SFloat.zeros(xs.shape[0], @num_nodes) xs.shape[1].times do |t| x = xs[true, t, false] h, c = @layers[t].forward(x, h, c) hs[true, t, false] = h end @h = h @c = c @return_sequences ? hs : h end |
#reset_state ⇒ Object
334 335 336 337 |
# File 'lib/dnn/core/rnn_layers.rb', line 334 def reset_state super() @c = @c.fill(0) if @c end |
#to_hash ⇒ Object
339 340 341 |
# File 'lib/dnn/core/rnn_layers.rb', line 339 def to_hash super({c: @c.to_a}) end |