Class: DNN::Layers::RNN

Inherits:
HasParamLayer show all
Includes:
Activations, Initializers
Defined in:
lib/dnn/core/rnn_layers.rb

Overview

Super class of all RNN classes.

Direct Known Subclasses

LSTM, SimpleRNN

Instance Attribute Summary collapse

Attributes inherited from HasParamLayer

#grads, #params

Instance Method Summary collapse

Methods inherited from HasParamLayer

#build, #update

Methods inherited from Layer

#backward, #build, #built?, #forward, #prev_layer

Constructor Details

#initialize(num_nodes, stateful: false, return_sequences: true, weight_initializer: nil, bias_initializer: nil, weight_decay: 0) ⇒ RNN

Returns a new instance of RNN.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dnn/core/rnn_layers.rb', line 14

def initialize(num_nodes,
               stateful: false,
               return_sequences: true,
               weight_initializer: nil,
               bias_initializer: nil,
               weight_decay: 0)
super()
@num_nodes = num_nodes
@stateful = stateful
@return_sequences = return_sequences
@weight_initializer = (weight_initializer || RandomNormal.new)
@bias_initializer = (bias_initializer || Zeros.new)
@weight_decay = weight_decay
@layers = []
@h = nil
end

Instance Attribute Details

#h ⇒ Object

Returns the value of attribute h.



9
10
11
# File 'lib/dnn/core/rnn_layers.rb', line 9

def h
  @h
end

#num_nodes ⇒ Object (readonly)

Returns the value of attribute num_nodes.



10
11
12
# File 'lib/dnn/core/rnn_layers.rb', line 10

def num_nodes
  @num_nodes
end

#stateful ⇒ Object (readonly)

Returns the value of attribute stateful.



11
12
13
# File 'lib/dnn/core/rnn_layers.rb', line 11

def stateful
  @stateful
end

#weight_decay ⇒ Object (readonly)

Returns the value of attribute weight_decay.



12
13
14
# File 'lib/dnn/core/rnn_layers.rb', line 12

def weight_decay
  @weight_decay
end

Instance Method Details

#init_params ⇒ Object



58
59
60
# File 'lib/dnn/core/rnn_layers.rb', line 58

def init_params
  @time_length = prev_layer.shape[0]
end

#ridge ⇒ Object



50
51
52
53
54
55
56
# File 'lib/dnn/core/rnn_layers.rb', line 50

def ridge
  if @weight_decay > 0
    0.5 * (@weight_decay * ((@params[:weight]**2).sum + (@params[:weight2]**2).sum))
  else
    0
  end
end

#shape ⇒ Object



46
47
48
# File 'lib/dnn/core/rnn_layers.rb', line 46

def shape
  @return_sequences ? [@time_length, @num_nodes] : [@num_nodes]
end

#to_hash(merge_hash = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dnn/core/rnn_layers.rb', line 31

def to_hash(merge_hash = nil)
  hash = {
    class: self.class.name,
    num_nodes: @num_nodes,
    stateful: @stateful,
    return_sequences: @return_sequences,
    activation: @activation.to_hash,
    weight_initializer: @weight_initializer.to_hash,
    bias_initializer: @bias_initializer.to_hash,
    weight_decay: @weight_decay,
  }
  hash.merge!(merge_hash) if merge_hash
  hash
end