Class: DNN::Layers::GRU_Dense
- Inherits:
-
Object
- Object
- DNN::Layers::GRU_Dense
- Defined in:
- lib/dnn/core/rnn_layers.rb
Instance Method Summary collapse
- #backward(dh2) ⇒ Object
- #forward(x, h) ⇒ Object
-
#initialize(weight, weight2, bias) ⇒ GRU_Dense
constructor
A new instance of GRU_Dense.
Constructor Details
#initialize(weight, weight2, bias) ⇒ GRU_Dense
Returns a new instance of GRU_Dense.
349 350 351 352 353 354 355 356 |
# File 'lib/dnn/core/rnn_layers.rb', line 349 def initialize(weight, weight2, bias) @weight = weight @weight2 = weight2 @bias = bias @update_sigmoid = Sigmoid.new @reset_sigmoid = Sigmoid.new @tanh = Tanh.new end |
Instance Method Details
#backward(dh2) ⇒ Object
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/dnn/core/rnn_layers.rb', line 377 def backward(dh2) dtanh_h = @tanh.backward(dh2 * @update) dh = dh2 * (1 - @update) dweight_h = @x.transpose.dot(dtanh_h) dx = dtanh_h.dot(@weight_h.transpose) dweight2_h = (@h * @reset).transpose.dot(dtanh_h) dh += dtanh_h.dot(@weight2_h.transpose) * @reset dbias_h = dtanh_h.sum(0) dreset = @reset_sigmoid.backward(dtanh_h.dot(@weight2_h.transpose) * @h) dupdate = @update_sigmoid.backward(dh2 * @tanh_h - dh2 * @h) da = Xumo::SFloat.hstack([dupdate, dreset]) dweight_a = @x.transpose.dot(da) dx += da.dot(@weight_a.transpose) dweight2_a = @h.transpose.dot(da) dh += da.dot(@weight2_a.transpose) dbias_a = da.sum(0) @weight.grad += Xumo::SFloat.hstack([dweight_a, dweight_h]) @weight2.grad += Xumo::SFloat.hstack([dweight2_a, dweight2_h]) @bias.grad += Xumo::SFloat.hstack([dbias_a, dbias_h]) [dx, dh] end |
#forward(x, h) ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/dnn/core/rnn_layers.rb', line 358 def forward(x, h) @x = x @h = h num_nodes = h.shape[1] @weight_a = @weight.data[true, 0...(num_nodes * 2)] @weight2_a = @weight2.data[true, 0...(num_nodes * 2)] bias_a = @bias.data[0...(num_nodes * 2)] a = x.dot(@weight_a) + h.dot(@weight2_a) + bias_a @update = @update_sigmoid.forward(a[true, 0...num_nodes]) @reset = @reset_sigmoid.forward(a[true, num_nodes..-1]) @weight_h = @weight.data[true, (num_nodes * 2)..-1] @weight2_h = @weight2.data[true, (num_nodes * 2)..-1] bias_h = @bias.data[(num_nodes * 2)..-1] @tanh_h = @tanh.forward(x.dot(@weight_h) + (h * @reset).dot(@weight2_h) + bias_h) h2 = (1 - @update) * h + @update * @tanh_h h2 end |