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
#bias, #num_nodes, #stateful, #weight, #weight2
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: Initializers::RandomNormal.new, bias_initializer: Initializers::Zeros.new, l1_lambda: 0, l2_lambda: 0) ⇒ LSTM
constructor
A new instance of LSTM.
- #reset_state ⇒ Object
Methods inherited from RNN
#dlasso, #dridge, #lasso, #ridge, #shape, #to_hash
Methods inherited from Connection
#dlasso, #dridge, #lasso, #ridge, #to_hash
Methods inherited from HasParamLayer
Methods inherited from Layer
#build, #built?, #prev_layer, #shape, #to_hash
Constructor Details
#initialize(num_nodes, stateful: false, return_sequences: true, weight_initializer: Initializers::RandomNormal.new, bias_initializer: Initializers::Zeros.new, l1_lambda: 0, l2_lambda: 0) ⇒ LSTM
Returns a new instance of LSTM.
269 270 271 272 273 274 275 276 277 278 |
# File 'lib/dnn/core/rnn_layers.rb', line 269 def initialize(num_nodes, stateful: false, return_sequences: true, weight_initializer: Initializers::RandomNormal.new, bias_initializer: Initializers::Zeros.new, l1_lambda: 0, l2_lambda: 0) super @params[:c] = nil end |
Class Method Details
.load_hash(hash) ⇒ Object
258 259 260 261 262 263 264 265 266 267 |
# File 'lib/dnn/core/rnn_layers.rb', line 258 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 end |
Instance Method Details
#backward(dh2s) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/dnn/core/rnn_layers.rb', line 301 def backward(dh2s) @weight.grad = Xumo::SFloat.zeros(*@weight.data.shape) @weight2.grad = Xumo::SFloat.zeros(*@weight2.data.shape) @bias.grad = Xumo::SFloat.zeros(*@bias.data.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
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/dnn/core/rnn_layers.rb', line 280 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 = @params[:h] if @params[:h] c = @params[:c] if @params[: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 @params[:h] = h @params[:c] = c @return_sequences ? hs : h end |
#reset_state ⇒ Object
321 322 323 324 |
# File 'lib/dnn/core/rnn_layers.rb', line 321 def reset_state super() @params[:c] = @params[:c].fill(0) if @params[:c] end |