Class: DNN::Layers::SimpleRNN

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

Constant Summary

Constants included from Activations

Activations::Layer

Instance Attribute Summary collapse

Attributes inherited from HasParamLayer

#grads, #params

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HasParamLayer

#build, #update

Methods inherited from Layer

#build, #built?, #prev_layer

Constructor Details

#initialize(num_nodes, stateful: false, activation: nil, weight_initializer: nil, bias_initializer: nil, weight_decay: 0) ⇒ SimpleRNN

Returns a new instance of SimpleRNN.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dnn/core/rnn_layers.rb', line 47

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

Instance Attribute Details

#num_nodes ⇒ Object (readonly)

Returns the value of attribute num_nodes.



34
35
36
# File 'lib/dnn/core/rnn_layers.rb', line 34

def num_nodes
  @num_nodes
end

#stateful ⇒ Object (readonly)

Returns the value of attribute stateful.



35
36
37
# File 'lib/dnn/core/rnn_layers.rb', line 35

def stateful
  @stateful
end

#weight_decay ⇒ Object (readonly)

Returns the value of attribute weight_decay.



36
37
38
# File 'lib/dnn/core/rnn_layers.rb', line 36

def weight_decay
  @weight_decay
end

Class Method Details

.load_hash(hash) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/dnn/core/rnn_layers.rb', line 38

def self.load_hash(hash)
  self.new(hash[:num_nodes],
           stateful: hash[:stateful],
           activation: Util.load_hash(hash[:activation]),
           weight_initializer: Util.load_hash(hash[:weight_initializer]),
           bias_initializer: Util.load_hash(hash[:bias_initializer]),
           weight_decay: hash[:weight_decay])
end

Instance Method Details

#backward(dh2s) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dnn/core/rnn_layers.rb', line 77

def backward(dh2s)
  @grads[:weight] = SFloat.zeros(*@params[:weight].shape)
  @grads[:weight2] = SFloat.zeros(*@params[:weight2].shape)
  @grads[:bias] = SFloat.zeros(*@params[:bias].shape)
  dxs = SFloat.zeros(@xs_shape)
  dh = 0
  (0...dh2s.shape[1]).to_a.reverse.each do |t|
    dh2 = dh2s[true, t, false]
    dx, dh = @layers[t].backward(dh2 + dh)
    dxs[true, t, false] = dx
  end
  dxs
end

#forward(xs) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dnn/core/rnn_layers.rb', line 64

def forward(xs)
  @xs_shape = xs.shape
  hs = SFloat.zeros(xs.shape[0], *shape)
  h = (@stateful && @h) ? @h : SFloat.zeros(xs.shape[0], @num_nodes)
  xs.shape[1].times do |t|
    x = xs[true, t, false]
    h = @layers[t].forward(x, h)
    hs[true, t, false] = h
  end
  @h = h
  hs
end

#ridge ⇒ Object



95
96
97
98
99
100
101
# File 'lib/dnn/core/rnn_layers.rb', line 95

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

#shape ⇒ Object



91
92
93
# File 'lib/dnn/core/rnn_layers.rb', line 91

def shape
  [@time_length, @num_nodes]
end

#to_hash ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/dnn/core/rnn_layers.rb', line 103

def to_hash
  super({num_nodes: @num_nodes,
         stateful: @stateful,
         activation: @activation.to_hash,
         weight_initializer: @weight_initializer.to_hash,
         bias_initializer: @bias_initializer.to_hash,
         weight_decay: @weight_decay})
end