Class: DNN::Layers::SimpleRNN

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

Instance Attribute Summary

Attributes inherited from RNN

#h, #num_nodes, #stateful, #weight_decay

Attributes inherited from HasParamLayer

#grads, #params

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RNN

#ridge, #shape

Methods inherited from HasParamLayer

#build, #update

Methods inherited from Layer

#build, #built?, #prev_layer, #shape

Constructor Details

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

Returns a new instance of SimpleRNN.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dnn/core/rnn_layers.rb', line 101

def initialize(num_nodes,
               stateful: false,
               return_sequences: true,
               activation: nil,
               weight_initializer: nil,
               bias_initializer: nil,
               weight_decay: 0)
  super(num_nodes,
        stateful: stateful,
        return_sequences: return_sequences,
        weight_initializer: weight_initializer,
        bias_initializer: bias_initializer,
        weight_decay: weight_decay)
  @activation = (activation || Tanh.new)
end

Class Method Details

.load_hash(hash) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/dnn/core/rnn_layers.rb', line 91

def self.load_hash(hash)
  self.new(hash[:num_nodes],
           stateful: hash[:stateful],
           return_sequences: hash[:return_sequences],
           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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dnn/core/rnn_layers.rb', line 130

def backward(dh2s)
  @grads[:weight] = Xumo::SFloat.zeros(*@params[:weight].shape)
  @grads[:weight2] = Xumo::SFloat.zeros(*@params[:weight2].shape)
  @grads[:bias] = Xumo::SFloat.zeros(*@params[:bias].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
  (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



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dnn/core/rnn_layers.rb', line 117

def forward(xs)
  @xs_shape = xs.shape
  hs = Xumo::SFloat.zeros(xs.shape[0], @time_length, @num_nodes)
  h = (@stateful && @h) ? @h : Xumo::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
  @return_sequences ? hs : h
end

#to_hash ⇒ Object



149
150
151
# File 'lib/dnn/core/rnn_layers.rb', line 149

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