Class: Torch::NN::EmbeddingBag

Inherits:
Module
  • Object
show all
Defined in:
lib/torch/nn/embedding_bag.rb

Instance Method Summary collapse

Methods inherited from Module

#_apply, #add_module, #apply, #buffers, #call, #children, #cpu, #cuda, #double, #eval, #float, #half, #inspect, #load_state_dict, #method_missing, #modules, #named_buffers, #named_children, #named_modules, #named_parameters, #parameters, #register_buffer, #register_parameter, #requires_grad!, #respond_to?, #share_memory, #state_dict, #to, #train, #type, #zero_grad

Methods included from Utils

#_ntuple, #_pair, #_quadrupal, #_single, #_triple

Constructor Details

#initialize(num_embeddings, embedding_dim, max_norm: nil, norm_type: 2.0, scale_grad_by_freq: false, mode: "mean", sparse: false, _weight: nil) ⇒ EmbeddingBag

Returns a new instance of EmbeddingBag.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/torch/nn/embedding_bag.rb', line 5

def initialize(num_embeddings, embedding_dim, max_norm: nil, norm_type: 2.0,
  scale_grad_by_freq: false, mode: "mean", sparse: false, _weight: nil)

  super()
  @num_embeddings = num_embeddings
  @embedding_dim = embedding_dim
  @max_norm = max_norm
  @norm_type = norm_type
  @scale_grad_by_freq = scale_grad_by_freq
  if _weight.nil?
    @weight = Parameter.new(Tensor.new(num_embeddings, embedding_dim))
    reset_parameters
  else
    raise ArgumentError, "Shape of weight does not match num_embeddings and embedding_dim" unless _weight.shape == [num_embeddings, embedding_dim]
    @weight = Parameter.new(_weight)
  end
  @mode = mode
  @sparse = sparse
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Torch::NN::Module

Instance Method Details

#forward(input, offsets: nil, per_sample_weights: nil) ⇒ Object



29
30
31
# File 'lib/torch/nn/embedding_bag.rb', line 29

def forward(input, offsets: nil, per_sample_weights: nil)
  F.embedding_bag(input, @weight, offsets: offsets, max_norm: @max_norm, norm_type: @norm_type, scale_grad_by_freq: @scale_grad_by_freq, mode: @mode, sparse: @sparse, per_sample_weights: per_sample_weights)
end

#reset_parametersObject



25
26
27
# File 'lib/torch/nn/embedding_bag.rb', line 25

def reset_parameters
  Init.normal!(@weight)
end