Class: DNN::Layers::SimpleRNN

Inherits:
RNN show all
Defined in:
lib/dnn/core/layers/rnn_layers.rb

Instance Attribute Summary collapse

Attributes inherited from RNN

#hidden, #num_nodes, #recurrent_weight, #recurrent_weight_initializer, #recurrent_weight_regularizer, #return_sequences, #stateful

Attributes inherited from Connection

#bias, #bias_initializer, #bias_regularizer, #weight, #weight_initializer, #weight_regularizer

Attributes inherited from TrainableLayer

#trainable

Attributes inherited from Layer

#input_shape

Instance Method Summary collapse

Methods inherited from RNN

#backward, #forward, #get_params, #output_shape, #regularizers, #reset_state

Methods inherited from Connection

#get_params, #regularizers, #use_bias

Methods inherited from TrainableLayer

#clean, #get_params

Methods inherited from Layer

#backward, #built?, #call, call, #clean, #forward, from_hash, #output_shape

Constructor Details

#initialize(num_nodes, stateful: false, return_sequences: true, activation: Layers::Tanh.new, 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) ⇒ SimpleRNN

Returns a new instance of SimpleRNN.

Parameters:

  • activation (DNN::Layers::Layer) (defaults to: Layers::Tanh.new)

    Activation function to use in a recurrent network.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/dnn/core/layers/rnn_layers.rb', line 173

def initialize(num_nodes,
               stateful: false,
               return_sequences: true,
               activation: Layers::Tanh.new,
               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(num_nodes,
        stateful: stateful,
        return_sequences: return_sequences,
        weight_initializer: weight_initializer,
        recurrent_weight_initializer: recurrent_weight_initializer,
        bias_initializer: bias_initializer,
        weight_regularizer: weight_regularizer,
        recurrent_weight_regularizer: recurrent_weight_regularizer,
        bias_regularizer: bias_regularizer,
        use_bias: use_bias)
  @activation = activation
end

Instance Attribute Details

#activationObject (readonly)

Returns the value of attribute activation.



170
171
172
# File 'lib/dnn/core/layers/rnn_layers.rb', line 170

def activation
  @activation
end

Instance Method Details

#build(input_shape) ⇒ Object



197
198
199
200
201
202
203
204
# File 'lib/dnn/core/layers/rnn_layers.rb', line 197

def build(input_shape)
  super
  num_prev_nodes = input_shape[1]
  @weight.data = Xumo::SFloat.new(num_prev_nodes, @num_nodes)
  @recurrent_weight.data = Xumo::SFloat.new(@num_nodes, @num_nodes)
  @bias.data = Xumo::SFloat.new(@num_nodes) if @bias
  init_weight_and_bias
end

#create_hidden_layerObject



206
207
208
# File 'lib/dnn/core/layers/rnn_layers.rb', line 206

def create_hidden_layer
  @hidden_layers = Array.new(@time_length) { SimpleRNNDense.new(@weight, @recurrent_weight, @bias, @activation) }
end

#load_hash(hash) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/dnn/core/layers/rnn_layers.rb', line 214

def load_hash(hash)
  initialize(hash[:num_nodes],
             stateful: hash[:stateful],
             return_sequences: hash[:return_sequences],
             activation: Layers::Layer.from_hash(hash[:activation]),
             weight_initializer: Initializers::Initializer.from_hash(hash[:weight_initializer]),
             recurrent_weight_initializer: Initializers::Initializer.from_hash(hash[:recurrent_weight_initializer]),
             bias_initializer: Initializers::Initializer.from_hash(hash[:bias_initializer]),
             weight_regularizer: Regularizers::Regularizer.from_hash(hash[:weight_regularizer]),
             recurrent_weight_regularizer: Regularizers::Regularizer.from_hash(hash[:recurrent_weight_regularizer]),
             bias_regularizer: Regularizers::Regularizer.from_hash(hash[:bias_regularizer]),
             use_bias: hash[:use_bias])
end

#to_hashObject



210
211
212
# File 'lib/dnn/core/layers/rnn_layers.rb', line 210

def to_hash
  super(activation: @activation.to_hash)
end