class DNN::Layers::LSTM

Attributes

cell[R]

Public Class Methods

new(num_units, stateful: false, return_sequences: true, weight_initializer: Initializers::RandomNormal.new, recurrent_weight_initializer: Initializers::RandomNormal.new, bias_initializer: Initializers::Zeros.new, weight_regularizer: nil, recurrent_weight_regularizer: nil, bias_regularizer: nil, use_bias: true) click to toggle source
Calls superclass method DNN::Layers::RNN::new
# File lib/dnn/core/layers/rnn_layers.rb, line 291
def initialize(num_units,
               stateful: false,
               return_sequences: true,
               weight_initializer: Initializers::RandomNormal.new,
               recurrent_weight_initializer: Initializers::RandomNormal.new,
               bias_initializer: Initializers::Zeros.new,
               weight_regularizer: nil,
               recurrent_weight_regularizer: nil,
               bias_regularizer: nil,
               use_bias: true)
  super
  @cell = Param.new
end

Public Instance Methods

backward_node(dh2s) click to toggle source
# File lib/dnn/core/layers/rnn_layers.rb, line 341
def backward_node(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
  (dh2s.shape[1] - 1).downto(0) do |t|
    dh2 = dh2s[true, t, false]
    dx, dh, dc = @hidden_layers[t].backward(dh2 + dh, dc)
    dxs[true, t, false] = dx
  end
  dxs
end
build(input_shape) click to toggle source
Calls superclass method DNN::Layers::RNN#build
# File lib/dnn/core/layers/rnn_layers.rb, line 305
def build(input_shape)
  super
  num_prev_units = input_shape[1]
  @weight.data = Xumo::SFloat.new(num_prev_units, @num_units * 4)
  @recurrent_weight.data = Xumo::SFloat.new(@num_units, @num_units * 4)
  @bias.data = Xumo::SFloat.new(@num_units * 4) if @bias
  init_weight_and_bias
end
create_hidden_layer() click to toggle source
# File lib/dnn/core/layers/rnn_layers.rb, line 314
def create_hidden_layer
  @hidden_layers = Array.new(@time_length) { LSTMDense.new(@weight, @recurrent_weight, @bias) }
end
forward_node(xs) click to toggle source
# File lib/dnn/core/layers/rnn_layers.rb, line 318
def forward_node(xs)
  create_hidden_layer
  @xs_shape = xs.shape
  hs = Xumo::SFloat.zeros(xs.shape[0], @time_length, @num_units)
  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_units)
  c ||= Xumo::SFloat.zeros(xs.shape[0], @num_units)
  xs.shape[1].times do |t|
    x = xs[true, t, false]
    @hidden_layers[t].trainable = @trainable
    h, c = @hidden_layers[t].forward(x, h, c)
    hs[true, t, false] = h
  end
  @hidden.data = h
  @cell.data = c
  @return_sequences ? hs : h
end
get_params() click to toggle source
# File lib/dnn/core/layers/rnn_layers.rb, line 363
def get_params
  { weight: @weight, recurrent_weight: @recurrent_weight, bias: @bias, hidden: @hidden, cell: @cell }
end
reset_state() click to toggle source
Calls superclass method DNN::Layers::RNN#reset_state
# File lib/dnn/core/layers/rnn_layers.rb, line 358
def reset_state
  super()
  @cell.data = @cell.data.fill(0) if @cell.data
end