Class: DNN::Layers::Dense

Inherits:
HasParamLayer show all
Includes:
Initializers
Defined in:
lib/dnn/core/layers.rb

Instance Attribute Summary collapse

Attributes inherited from HasParamLayer

#grads, #params

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HasParamLayer

#build, #update

Methods inherited from Layer

#build, #built?, #prev_layer

Constructor Details

#initialize(num_nodes, weight_initializer: nil, bias_initializer: nil, weight_decay: 0) ⇒ Dense

Returns a new instance of Dense.



113
114
115
116
117
118
119
120
121
122
# File 'lib/dnn/core/layers.rb', line 113

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_nodesObject (readonly)

Returns the value of attribute num_nodes.



103
104
105
# File 'lib/dnn/core/layers.rb', line 103

def num_nodes
  @num_nodes
end

#weight_decayObject (readonly)

Returns the value of attribute weight_decay.



104
105
106
# File 'lib/dnn/core/layers.rb', line 104

def weight_decay
  @weight_decay
end

Class Method Details

.load_hash(hash) ⇒ Object



106
107
108
109
110
111
# File 'lib/dnn/core/layers.rb', line 106

def self.load_hash(hash)
  self.new(hash[:num_nodes],
           weight_initializer: Util.load_hash(hash[:weight_initializer]),
           bias_initializer: Util.load_hash(hash[:bias_initializer]),
           weight_decay: hash[:weight_decay])
end

Instance Method Details

#backward(dout) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/dnn/core/layers.rb', line 129

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



124
125
126
127
# File 'lib/dnn/core/layers.rb', line 124

def forward(x)
  @x = x
  @x.dot(@params[:weight]) + @params[:bias]
end

#shapeObject



139
140
141
# File 'lib/dnn/core/layers.rb', line 139

def shape
  [@num_nodes]
end

#to_hashObject



143
144
145
146
147
148
# File 'lib/dnn/core/layers.rb', line 143

def to_hash
  super({num_nodes: @num_nodes,
         weight_initializer: @weight_initializer.to_hash,
         bias_initializer: @bias_initializer.to_hash,
         weight_decay: @weight_decay})
end