Class: Torch::NN::TransformerDecoder

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

Instance Attribute Summary

Attributes inherited from Module

#training

Instance Method Summary collapse

Methods inherited from Module

#_apply, #add_module, #apply, #buffers, #call, #children, #cpu, #cuda, #deep_dup, #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

#_activation_fn, #_clones, #_ntuple, #_pair, #_quadrupal, #_single, #_triple

Constructor Details

#initialize(decoder_layer, num_layers, norm: nil) ⇒ TransformerDecoder

Returns a new instance of TransformerDecoder.



4
5
6
7
8
9
10
# File 'lib/torch/nn/transformer_decoder.rb', line 4

def initialize(decoder_layer, num_layers, norm: nil)
  super()

  @layers = _clones(decoder_layer, num_layers)
  @num_layers = num_layers
  @norm = norm
end

Dynamic Method Handling

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

Instance Method Details

#forward(tgt, memory, tgt_mask: nil, memory_mask: nil, tgt_key_padding_mask: nil, memory_key_padding_mask: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/torch/nn/transformer_decoder.rb', line 12

def forward(tgt, memory, tgt_mask: nil, memory_mask: nil, tgt_key_padding_mask: nil, memory_key_padding_mask: nil)
  output = tgt

  @layers.each do |mod|
    output = mod.call(output, memory, tgt_mask: tgt_mask, memory_mask: memory_mask, tgt_key_padding_mask: tgt_key_padding_mask, memory_key_padding_mask: memory_key_padding_mask)
  end

  output = @norm.call(output) if @norm

  output
end