class DNN::Layers::Dense
Attributes
num_units[R]
Public Class Methods
new(num_units, weight_initializer: Initializers::RandomNormal.new, bias_initializer: Initializers::Zeros.new, weight_regularizer: nil, bias_regularizer: nil, use_bias: true)
click to toggle source
@param [Integer] num_units
Number of nodes.
Calls superclass method
DNN::Layers::Connection::new
# File lib/dnn/core/layers/basic_layers.rb, line 240 def initialize(num_units, weight_initializer: Initializers::RandomNormal.new, bias_initializer: Initializers::Zeros.new, weight_regularizer: nil, bias_regularizer: nil, use_bias: true) super(weight_initializer: weight_initializer, bias_initializer: bias_initializer, weight_regularizer: weight_regularizer, bias_regularizer: bias_regularizer, use_bias: use_bias) @num_units = num_units end
Public Instance Methods
backward_node(dy)
click to toggle source
# File lib/dnn/core/layers/basic_layers.rb, line 269 def backward_node(dy) if @trainable @weight.grad += @x.transpose.dot(dy) @bias.grad += dy.sum(0) if @bias end dy.dot(@weight.data.transpose) end
build(input_shape)
click to toggle source
Calls superclass method
DNN::Layers::Layer#build
# File lib/dnn/core/layers/basic_layers.rb, line 251 def build(input_shape) unless input_shape.length == 1 raise DNNShapeError, "Input shape is #{input_shape}. But input shape must be 1 dimensional." end super num_prev_units = input_shape[0] @weight.data = Xumo::SFloat.new(num_prev_units, @num_units) @bias.data = Xumo::SFloat.new(@num_units) if @bias init_weight_and_bias end
compute_output_shape()
click to toggle source
# File lib/dnn/core/layers/basic_layers.rb, line 277 def compute_output_shape [@num_units] end
forward_node(x)
click to toggle source
# File lib/dnn/core/layers/basic_layers.rb, line 262 def forward_node(x) @x = x y = x.dot(@weight.data) y += @bias.data if @bias y end
load_hash(hash)
click to toggle source
# File lib/dnn/core/layers/basic_layers.rb, line 285 def load_hash(hash) initialize(hash[:num_units], weight_initializer: Initializers::Initializer.from_hash(hash[:weight_initializer]), bias_initializer: Initializers::Initializer.from_hash(hash[:bias_initializer]), weight_regularizer: Regularizers::Regularizer.from_hash(hash[:weight_regularizer]), bias_regularizer: Regularizers::Regularizer.from_hash(hash[:bias_regularizer]), use_bias: hash[:use_bias]) end
to_hash()
click to toggle source
Calls superclass method
DNN::Layers::Connection#to_hash
# File lib/dnn/core/layers/basic_layers.rb, line 281 def to_hash super(num_units: @num_units) end