Class: DNN::Layers::GRUDense
- Includes:
- LayerNode
- Defined in:
- lib/dnn/core/layers/rnn_layers.rb
Instance Attribute Summary collapse
-
#trainable ⇒ Object
Returns the value of attribute trainable.
Attributes inherited from Layer
Instance Method Summary collapse
- #backward_node(dh2) ⇒ Object
- #forward_node(x, h) ⇒ Object
-
#initialize(weight, recurrent_weight, bias) ⇒ GRUDense
constructor
A new instance of GRUDense.
Methods included from LayerNode
Methods inherited from Layer
#build, #built?, #call, call, #clean, #forward, from_hash, #load_hash, #output_shape, #to_hash
Constructor Details
#initialize(weight, recurrent_weight, bias) ⇒ GRUDense
Returns a new instance of GRUDense.
377 378 379 380 381 382 383 384 385 |
# File 'lib/dnn/core/layers/rnn_layers.rb', line 377 def initialize(weight, recurrent_weight, bias) @weight = weight @recurrent_weight = recurrent_weight @bias = bias @update_sigmoid = Layers::Sigmoid.new @reset_sigmoid = Layers::Sigmoid.new @tanh = Layers::Tanh.new @trainable = true end |
Instance Attribute Details
#trainable ⇒ Object
Returns the value of attribute trainable.
375 376 377 |
# File 'lib/dnn/core/layers/rnn_layers.rb', line 375 def trainable @trainable end |
Instance Method Details
#backward_node(dh2) ⇒ Object
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/dnn/core/layers/rnn_layers.rb', line 410 def backward_node(dh2) dtanh_h = @tanh.backward_node(dh2 * (1 - @update)) dh = dh2 * @update if @trainable dweight_h = @x.transpose.dot(dtanh_h) dweight2_h = (@h * @reset).transpose.dot(dtanh_h) dbias_h = dtanh_h.sum(0) if @bias end dx = dtanh_h.dot(@weight_h.transpose) dh += dtanh_h.dot(@weight2_h.transpose) * @reset dreset = @reset_sigmoid.backward_node(dtanh_h.dot(@weight2_h.transpose) * @h) dupdate = @update_sigmoid.backward_node(dh2 * @h - dh2 * @tanh_h) da = Xumo::SFloat.hstack([dupdate, dreset]) if @trainable dweight_a = @x.transpose.dot(da) dweight2_a = @h.transpose.dot(da) dbias_a = da.sum(0) if @bias end dx += da.dot(@weight_a.transpose) dh += da.dot(@weight2_a.transpose) if @trainable @weight.grad += Xumo::SFloat.hstack([dweight_a, dweight_h]) @recurrent_weight.grad += Xumo::SFloat.hstack([dweight2_a, dweight2_h]) @bias.grad += Xumo::SFloat.hstack([dbias_a, dbias_h]) if @bias end [dx, dh] end |
#forward_node(x, h) ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/dnn/core/layers/rnn_layers.rb', line 387 def forward_node(x, h) @x = x @h = h num_nodes = h.shape[1] @weight_a = @weight.data[true, 0...(num_nodes * 2)] @weight2_a = @recurrent_weight.data[true, 0...(num_nodes * 2)] a = x.dot(@weight_a) + h.dot(@weight2_a) a += @bias.data[0...(num_nodes * 2)] if @bias @update = @update_sigmoid.forward_node(a[true, 0...num_nodes]) @reset = @reset_sigmoid.forward_node(a[true, num_nodes..-1]) @weight_h = @weight.data[true, (num_nodes * 2)..-1] @weight2_h = @recurrent_weight.data[true, (num_nodes * 2)..-1] @tanh_h = if @bias bias_h = @bias.data[(num_nodes * 2)..-1] @tanh.forward_node(x.dot(@weight_h) + (h * @reset).dot(@weight2_h) + bias_h) else @tanh.forward_node(x.dot(@weight_h) + (h * @reset).dot(@weight2_h)) end h2 = (1 - @update) * @tanh_h + @update * h h2 end |