Class: DNN::Layers::SimpleRNN_Dense

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(weight, recurrent_weight, bias, activation) ⇒ SimpleRNN_Dense

Returns a new instance of SimpleRNN_Dense.



116
117
118
119
120
121
122
# File 'lib/dnn/core/rnn_layers.rb', line 116

def initialize(weight, recurrent_weight, bias, activation)
  @weight = weight
  @recurrent_weight = recurrent_weight
  @bias = bias
  @activation = activation.clone
  @trainable = true
end

Instance Attribute Details

#trainable ⇒ Object

Returns the value of attribute trainable.



114
115
116
# File 'lib/dnn/core/rnn_layers.rb', line 114

def trainable
  @trainable
end

Instance Method Details

#backward(dh2) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/dnn/core/rnn_layers.rb', line 132

def backward(dh2)
  dh2 = @activation.backward(dh2)
  if @trainable
    @weight.grad += @x.transpose.dot(dh2)
    @recurrent_weight.grad += @h.transpose.dot(dh2)
    @bias.grad += dh2.sum(0) if @bias
  end
  dx = dh2.dot(@weight.data.transpose)
  dh = dh2.dot(@recurrent_weight.data.transpose)
  [dx, dh]
end

#forward(x, h) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/dnn/core/rnn_layers.rb', line 124

def forward(x, h)
  @x = x
  @h = h
  h2 = x.dot(@weight.data) + h.dot(@recurrent_weight.data)
  h2 += @bias.data if @bias
  @activation.forward(h2)
end