Class: DNN::Layers::SimpleRNN_Dense

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

Instance Method Summary collapse

Constructor Details

#initialize(params, grads, activation) ⇒ SimpleRNN_Dense

Returns a new instance of SimpleRNN_Dense.



5
6
7
8
9
# File 'lib/dnn/core/rnn_layers.rb', line 5

def initialize(params, grads, activation)
  @params = params
  @grads = grads
  @activation = activation
end

Instance Method Details

#backward(dh2) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/dnn/core/rnn_layers.rb', line 18

def backward(dh2)
  dh2 = @activation.backward(dh2)
  @grads[:weight] += @x.transpose.dot(dh2)
  @grads[:weight2] += @h.transpose.dot(dh2)
  @grads[:bias] += dh2.sum(0)
  dx = dh2.dot(@params[:weight].transpose)
  dh = dh2.dot(@params[:weight2].transpose)
  [dx, dh]
end

#forward(x, h) ⇒ Object



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

def forward(x, h)
  @x = x
  @h = h
  h2 = x.dot(@params[:weight]) + h.dot(@params[:weight2]) + @params[:bias]
  @activation.forward(h2)
end