Class: DNN::Layers::Dense
- Inherits:
-
HasParamLayer
- Object
- Layer
- HasParamLayer
- DNN::Layers::Dense
- Includes:
- Initializers
- Defined in:
- lib/dnn/core/layers.rb
Instance Attribute Summary collapse
-
#num_nodes ⇒ Object
readonly
Returns the value of attribute num_nodes.
-
#weight_decay ⇒ Object
readonly
Returns the value of attribute weight_decay.
Attributes inherited from HasParamLayer
Instance Method Summary collapse
- #backward(dout) ⇒ Object
- #forward(x) ⇒ Object
-
#initialize(num_nodes, weight_initializer: nil, bias_initializer: nil, weight_decay: 0) ⇒ Dense
constructor
A new instance of Dense.
- #shape ⇒ Object
Methods inherited from HasParamLayer
Methods inherited from Layer
Constructor Details
#initialize(num_nodes, weight_initializer: nil, bias_initializer: nil, weight_decay: 0) ⇒ Dense
Returns a new instance of Dense.
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/dnn/core/layers.rb', line 80 def initialize(num_nodes, weight_initializer: nil, bias_initializer: nil, weight_decay: 0) super() @num_nodes = num_nodes @weight_initializer = (weight_initializer || RandomNormal.new) @bias_initializer = (bias_initializer || Zeros.new) @weight_decay = weight_decay end |
Instance Attribute Details
#num_nodes ⇒ Object (readonly)
Returns the value of attribute num_nodes.
77 78 79 |
# File 'lib/dnn/core/layers.rb', line 77 def num_nodes @num_nodes end |
#weight_decay ⇒ Object (readonly)
Returns the value of attribute weight_decay.
78 79 80 |
# File 'lib/dnn/core/layers.rb', line 78 def weight_decay @weight_decay end |
Instance Method Details
#backward(dout) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/dnn/core/layers.rb', line 96 def backward(dout) @grads[:weight] = @x.transpose.dot(dout) if @weight_decay > 0 dridge = @weight_decay * @params[:weight] @grads[:weight] += dridge end @grads[:bias] = dout.sum(0) dout.dot(@params[:weight].transpose) end |
#forward(x) ⇒ Object
91 92 93 94 |
# File 'lib/dnn/core/layers.rb', line 91 def forward(x) @x = x @x.dot(@params[:weight]) + @params[:bias] end |
#shape ⇒ Object
106 107 108 |
# File 'lib/dnn/core/layers.rb', line 106 def shape [@num_nodes] end |