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.



65
66
67
68
69
# File 'lib/dnn/core/rnn_layers.rb', line 65

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

Instance Method Details

#backward(dh2) ⇒ Object



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

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



71
72
73
74
75
76
# File 'lib/dnn/core/rnn_layers.rb', line 71

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