Class: DNN::Layers::Embedding
- Inherits:
-
TrainableLayer
- Object
- Layer
- TrainableLayer
- DNN::Layers::Embedding
- Includes:
- LayerNode
- Defined in:
- lib/dnn/core/layers/embedding.rb
Instance Attribute Summary collapse
-
#input_length ⇒ Object
readonly
Returns the value of attribute input_length.
-
#mask_zero ⇒ Object
readonly
Returns the value of attribute mask_zero.
-
#weight ⇒ Object
readonly
Returns the value of attribute weight.
-
#weight_initializer ⇒ Object
readonly
Returns the value of attribute weight_initializer.
-
#weight_regularizer ⇒ Object
readonly
Returns the value of attribute weight_regularizer.
Attributes inherited from TrainableLayer
Attributes inherited from Layer
Instance Method Summary collapse
- #backward_node(dy) ⇒ Object
- #build(input_shape) ⇒ Object
- #forward_node(x) ⇒ Object
- #get_params ⇒ Object
-
#initialize(input_dim_or_shape, input_length, weight_initializer: Initializers::RandomUniform.new, weight_regularizer: nil, mask_zero: false) ⇒ Embedding
constructor
A new instance of Embedding.
- #load_hash(hash) ⇒ Object
- #regularizers ⇒ Object
- #to_hash ⇒ Object
Methods included from LayerNode
Methods inherited from TrainableLayer
Methods inherited from Layer
#<<, #built?, #call, call, #clean, #compute_output_shape, #forward, from_hash
Constructor Details
#initialize(input_dim_or_shape, input_length, weight_initializer: Initializers::RandomUniform.new, weight_regularizer: nil, mask_zero: false) ⇒ Embedding
Returns a new instance of Embedding.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/dnn/core/layers/embedding.rb', line 17 def initialize(input_dim_or_shape, input_length, weight_initializer: Initializers::RandomUniform.new, weight_regularizer: nil, mask_zero: false) 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 @weight_regularizer = weight_regularizer @weight = Param.new(nil, Xumo::SFloat[0]) @mask_zero = mask_zero end |
Instance Attribute Details
#input_length ⇒ Object (readonly)
Returns the value of attribute input_length.
7 8 9 |
# File 'lib/dnn/core/layers/embedding.rb', line 7 def input_length @input_length end |
#mask_zero ⇒ Object (readonly)
Returns the value of attribute mask_zero.
11 12 13 |
# File 'lib/dnn/core/layers/embedding.rb', line 11 def mask_zero @mask_zero end |
#weight ⇒ Object (readonly)
Returns the value of attribute weight.
8 9 10 |
# File 'lib/dnn/core/layers/embedding.rb', line 8 def weight @weight end |
#weight_initializer ⇒ Object (readonly)
Returns the value of attribute weight_initializer.
9 10 11 |
# File 'lib/dnn/core/layers/embedding.rb', line 9 def weight_initializer @weight_initializer end |
#weight_regularizer ⇒ Object (readonly)
Returns the value of attribute weight_regularizer.
10 11 12 |
# File 'lib/dnn/core/layers/embedding.rb', line 10 def weight_regularizer @weight_regularizer end |
Instance Method Details
#backward_node(dy) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/dnn/core/layers/embedding.rb', line 53 def backward_node(dy) @weight.grad += Xumo::SFloat.zeros(*@weight.data.shape) @x.shape[0].times do |i| @x.shape[1].times do |j| index = @x[i, j] if @mask_zero @weight.grad[index] += dy[i, j] unless index == 0 else @weight.grad[index] += dy[i, j] end end end nil end |
#build(input_shape) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/dnn/core/layers/embedding.rb', line 30 def build(input_shape) super(@input_shape) @weight.data = Xumo::SFloat.new(@input_length) @weight_initializer.init_param(self, @weight) @weight_regularizer.param = @weight if @weight_regularizer end |
#forward_node(x) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/dnn/core/layers/embedding.rb', line 37 def forward_node(x) @x = x y = Xumo::SFloat.zeros(*x.shape) x.shape[0].times do |i| if @mask_zero x.shape[1].times do |j| index = x[i, j] y[i, j] = index == 0 ? 0 : @weight.data[index] end else y[i, false] = @weight.data[x[i, false]] end end y end |
#get_params ⇒ Object
85 86 87 |
# File 'lib/dnn/core/layers/embedding.rb', line 85 def get_params { weight: @weight } end |
#load_hash(hash) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/dnn/core/layers/embedding.rb', line 78 def load_hash(hash) initialize(hash[:input_shape], hash[:input_length], weight_initializer: Initializers::Initializer.from_hash(hash[:weight_initializer]), weight_regularizer: Regularizers::Regularizer.from_hash(hash[:weight_regularizer]), mask_zero: hash[:mask_zero]) end |
#regularizers ⇒ Object
68 69 70 |
# File 'lib/dnn/core/layers/embedding.rb', line 68 def regularizers @weight_regularizer ? [@weight_regularizer] : [] end |
#to_hash ⇒ Object
72 73 74 75 76 |
# File 'lib/dnn/core/layers/embedding.rb', line 72 def to_hash super(input_shape: @input_shape, input_length: @input_length, weight_initializer: @weight_initializer.to_hash, weight_regularizer: @weight_regularizer&.to_hash, mask_zero: @mask_zero) end |