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
Attributes inherited from RNN
#num_nodes, #recurrent_weight_initializer, #recurrent_weight_regularizer, #return_sequences, #stateful
Attributes inherited from Connection
#bias_initializer, #bias_regularizer, #weight_initializer, #weight_regularizer
Attributes inherited from HasParamLayer
Attributes inherited from Layer
Class Method Summary collapse
Instance Method Summary collapse
- #backward(dh2s) ⇒ Object
- #build(input_shape) ⇒ Object
- #forward(xs) ⇒ Object
-
#initialize(num_nodes, stateful: false, return_sequences: true, weight_initializer: RandomNormal.new, recurrent_weight_initializer: RandomNormal.new, bias_initializer: Zeros.new, weight_regularizer: nil, recurrent_weight_regularizer: nil, bias_regularizer: nil, use_bias: true) ⇒ LSTM
constructor
A new instance of LSTM.
- #reset_state ⇒ Object
Methods inherited from RNN
#output_shape, #regularizers, #to_hash
Methods inherited from Connection
#regularizers, #to_hash, #use_bias
Methods inherited from Layer
#built?, #output_shape, #to_hash
Constructor Details
#initialize(num_nodes, stateful: false, return_sequences: true, weight_initializer: RandomNormal.new, recurrent_weight_initializer: RandomNormal.new, bias_initializer: Zeros.new, weight_regularizer: nil, recurrent_weight_regularizer: nil, bias_regularizer: nil, use_bias: true) ⇒ LSTM
Returns a new instance of LSTM.
281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/dnn/core/rnn_layers.rb', line 281 def initialize(num_nodes, stateful: false, return_sequences: true, weight_initializer: RandomNormal.new, recurrent_weight_initializer: RandomNormal.new, bias_initializer: Zeros.new, weight_regularizer: nil, recurrent_weight_regularizer: nil, bias_regularizer: nil, use_bias: true) super @cell = @params[:cell] = Param.new end |
Class Method Details
.from_hash(hash) ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/dnn/core/rnn_layers.rb', line 267 def self.from_hash(hash) lstm = self.new(hash[:num_nodes], stateful: hash[:stateful], return_sequences: hash[:return_sequences], weight_initializer: Utils.from_hash(hash[:weight_initializer]), recurrent_weight_initializer: Utils.from_hash(hash[:recurrent_weight_initializer]), bias_initializer: Utils.from_hash(hash[:bias_initializer]), weight_regularizer: Utils.from_hash(hash[:weight_regularizer]), recurrent_weight_regularizer: Utils.from_hash(hash[:recurrent_weight_regularizer]), bias_regularizer: Utils.from_hash(hash[:bias_regularizer]), use_bias: hash[:use_bias]) lstm end |
Instance Method Details
#backward(dh2s) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/dnn/core/rnn_layers.rb', line 329 def backward(dh2s) 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 |
#build(input_shape) ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/dnn/core/rnn_layers.rb', line 295 def build(input_shape) super num_prev_nodes = input_shape[1] @weight.data = Xumo::SFloat.new(num_prev_nodes, @num_nodes * 4) @recurrent_weight.data = Xumo::SFloat.new(@num_nodes, @num_nodes * 4) @bias.data = Xumo::SFloat.new(@num_nodes * 4) if @bias init_weight_and_bias @time_length.times do |t| @layers << LSTM_Dense.new(@weight, @recurrent_weight, @bias) end end |
#forward(xs) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/dnn/core/rnn_layers.rb', line 307 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 = @hidden.data if @hidden.data c = @cell.data if @cell.data 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] @layers[t].trainable = @trainable h, c = @layers[t].forward(x, h, c) hs[true, t, false] = h end @hidden.data = h @cell.data = c @return_sequences ? hs : h end |
#reset_state ⇒ Object
346 347 348 349 |
# File 'lib/dnn/core/rnn_layers.rb', line 346 def reset_state super() @cell.data = @cell.data.fill(0) if @cell.data end |