class Secryst::TransformerEncoder
Public Class Methods
new(encoder_layers, norm=nil, d_model, vocab_size, dropout)
click to toggle source
TransformerEncoder
is a stack of N encoder layers Args:
encoder_layers: an array of instances of the TransformerEncoderLayer class (required). norm: the layer normalization component (optional). d_model: the number of expected features in the encoder/decoder inputs. vocab_size: size of vocabulary (number of different possible tokens).
- Examples
-
>>> encoder_layers = 6.times.map {|i|
TransformerEncoderLayer.new
(512, 8) } >>> transformer_encoder = nn.TransformerEncoder(encoder_layers, nil, 512, 72, 0.1) >>> src = Torch.rand(10, 32, 512) >>> out = transformer_encoder.call(src)
Calls superclass method
# File lib/secryst/transformer.rb, line 131 def initialize(encoder_layers, norm=nil, d_model, vocab_size, dropout) super() @d_model = d_model encoder_layers.each.with_index do |l, i| instance_variable_set("@layer#{i}", l) end @layers = encoder_layers.length.times.map {|i| instance_variable_get("@layer#{i}") } @num_layers = encoder_layers.length @embedding = Torch::NN::Embedding.new(vocab_size, d_model) @pos_encoder = PositionalEncoding.new(d_model, dropout: dropout) @norm = norm end
Public Instance Methods
forward(src, mask: nil, src_key_padding_mask: nil)
click to toggle source
Pass the input through the encoder layers in turn. Args:
src: the sequence to the encoder (required). mask: the mask for the src sequence (optional). src_key_padding_mask: the mask for the src keys per batch (optional).
Shape:
see the docs in Transformer class.
# File lib/secryst/transformer.rb, line 151 def forward(src, mask: nil, src_key_padding_mask: nil) output = @embedding.call(src) * Math.sqrt(@d_model) output = @pos_encoder.call(output) @layers.each { |mod| output = mod.call(output, src_mask: mask, src_key_padding_mask: src_key_padding_mask) } if @norm output = @norm.call(output) end return output end