def LinearDecoder( n_out:int, # Number of output channels n_hid:int, # Number of features in encoder last layer output output_p:float=0.1, # Input dropout probability tie_encoder:torch.nn.modules.module.Module=None, # If module is supplied will tie decoder weight to `tie_encoder.weight` bias:bool=True, # If `False` the layer will not learn additive bias):
To go on top of a RNNCore module and create a Language Model.
def get_language_model( arch, # Function or class that can generate a language model architecture vocab_sz:int, # Size of the vocabulary config:dict=None, # Model configuration dictionary drop_mult:float=1.0, # Multiplicative factor to scale all dropout probabilities in `config`)->__main__.SequentialRNN: # Language model with `arch` encoder and linear decoder
Create a language model from arch and its config.
The default config used can be found in _model_meta[arch]['config_lm']. drop_mult is applied to all the probabilities of dropout in that config.
def SentenceEncoder( bptt:int, # Backpropagation through time module:torch.nn.modules.module.Module, # A module that can process up to [`bs`, `bptt`] tokens pad_idx:int=1, # Padding token id max_len:int=None, # Maximal output length):
Create an encoder over module that can process a full sentence.
Warning
This module expects the inputs padded with most of the padding first, with the sequence beginning at a round multiple of bptt (and the rest of the padding at the end). Use pad_input_chunk to get your data in a suitable format.
def masked_concat_pool( output:torch.Tensor, # Output of sentence encoder mask:torch.Tensor, # Boolean mask as returned by sentence encoder bptt:int, # Backpropagation through time)->torch.Tensor: # Concatenation of [last_hidden, max_pool, avg_pool]
Pool MultiBatchEncoder outputs into one vector [last_hidden, max_pool, avg_pool]
#Test the result is independent of padding by replacing the padded part by some random contentout1 = torch.randn(2,4,5)out1[0,2:] = out[0,2:].clone()out1[1,:3] = out[1,:3].clone()x1 = masked_concat_pool(out1, mask, 2)test_eq(x, x1)
def PoolingLinearClassifier( dims:list, # List of hidden sizes for MLP as `int`s ps:list, # List of dropout probabilities as `float`s bptt:int, # Backpropagation through time y_range:tuple=None, # Tuple of (low, high) output value bounds):
def get_text_classifier( arch:Callable, # Function or class that can generate a language model architecture vocab_sz:int, # Size of the vocabulary n_class:int, # Number of classes seq_len:int=72, # Backpropagation through time config:dict=None, # Encoder configuration dictionary drop_mult:float=1.0, # Multiplicative factor to scale all dropout probabilities in `config` lin_ftrs:list=None, # List of hidden sizes for classifier head as `int`s ps:list=None, # List of dropout probabilities for classifier head as `float`s pad_idx:int=1, # Padding token id max_len:int=1440, # Maximal output length for `SentenceEncoder` y_range:tuple=None, # Tuple of (low, high) output value bounds):
Create a text classifier from arch and its config, maybe pretrained