Class: DNN::Layers::Embedding
Instance Attribute Summary collapse
#params, #trainable
Attributes inherited from Layer
#input_shape, #learning_phase
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Layer
#built?, #output_shape
Constructor Details
#initialize(input_dim_or_shape, input_length, weight_initializer: Initializers::RandomUniform.new) ⇒ Embedding
Returns a new instance of Embedding.
17
18
19
20
21
22
|
# File 'lib/dnn/core/embedding.rb', line 17
def initialize(input_dim_or_shape, input_length, weight_initializer: Initializers::RandomUniform.new)
super()
@input_shape = input_dim_or_shape.is_a?(Array) ? input_dim_or_shape : [input_dim_or_shape]
@input_length = input_length
@weight_initializer = weight_initializer
end
|
Instance Attribute Details
6
7
8
|
# File 'lib/dnn/core/embedding.rb', line 6
def input_length
@input_length
end
|
Return the weight initializer.
8
9
10
|
# File 'lib/dnn/core/embedding.rb', line 8
def weight_initializer
@weight_initializer
end
|
Class Method Details
.from_hash(hash) ⇒ Object
10
11
12
13
|
# File 'lib/dnn/core/embedding.rb', line 10
def self.from_hash(hash)
self.new(hash[:input_shape], hash[:input_length],
weight_initializer: DNN::Utils.from_hash(hash[:weight_initializer]))
end
|
Instance Method Details
#backward(dy) ⇒ Object
40
41
42
43
44
45
46
47
48
|
# File 'lib/dnn/core/embedding.rb', line 40
def backward(dy)
@weight.grad += Xumo::SFloat.zeros(*@weight.data.shape)
@x.shape[0].times do |i|
@x.shape[1].times do |j|
@weight.grad[@x[i, j]] += dy[i, j]
end
end
nil
end
|
#build ⇒ Object
24
25
26
27
28
29
|
# File 'lib/dnn/core/embedding.rb', line 24
def build
@built = true
@params[:weight] = @weight = Param.new(Xumo::SFloat.new(@input_length), 0)
@weight_initializer.init_param(self, @weight)
@input_shape
end
|
#forward(x) ⇒ Object
31
32
33
34
35
36
37
38
|
# File 'lib/dnn/core/embedding.rb', line 31
def forward(x)
@x = x
y = Xumo::SFloat.zeros(*x.shape)
x.shape[0].times do |i|
y[i, false] = @weight.data[x[i, false]]
end
y
end
|
#to_hash ⇒ Object
50
51
52
|
# File 'lib/dnn/core/embedding.rb', line 50
def to_hash
super(input_shape: @input_shape, input_length: @input_length, weight_initializer: @weight_initializer.to_hash)
end
|