Transformer XL

Model from Keita Kurita.

Not useable https://github.com/keitakurita/Practical_NLP_in_PyTorch/blob/master/deep_dives/transformer_xl_from_scratch.ipynb

class flood_forecast.transformer_xl.transformer_xl.MultiHeadAttention(d_input: int, d_inner: int, n_heads: int = 4, dropout: float = 0.1, dropouta: float = 0.0)[source]
__init__(d_input: int, d_inner: int, n_heads: int = 4, dropout: float = 0.1, dropouta: float = 0.0)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(input_: FloatTensor, pos_embs: FloatTensor, memory: FloatTensor, u: FloatTensor, v: FloatTensor, mask: FloatTensor | None = None)[source]
pos_embs: we pass the positional embeddings in separately

because we need to handle relative positions

input shape: (seq, bs, self.d_input) pos_embs shape: (seq + prev_seq, bs, self.d_input) output shape: (seq, bs, self.d_input)

class flood_forecast.transformer_xl.transformer_xl.PositionwiseFF(d_input, d_inner, dropout)[source]
__init__(d_input, d_inner, dropout)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(input_: FloatTensor) FloatTensor[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class flood_forecast.transformer_xl.transformer_xl.DecoderBlock(n_heads, d_input, d_head_inner, d_ff_inner, dropout, dropouta=0.0)[source]
__init__(n_heads, d_input, d_head_inner, d_ff_inner, dropout, dropouta=0.0)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(input_: FloatTensor, pos_embs: FloatTensor, u: FloatTensor, v: FloatTensor, mask=None, mems=None)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class flood_forecast.transformer_xl.transformer_xl.PositionalEmbedding(d)[source]
__init__(d)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(positions: LongTensor)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class flood_forecast.transformer_xl.transformer_xl.StandardWordEmbedding(num_embeddings, embedding_dim, div_val=1, sample_softmax=False)[source]
__init__(num_embeddings, embedding_dim, div_val=1, sample_softmax=False)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(input_: LongTensor)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class flood_forecast.transformer_xl.transformer_xl.TransformerXL(num_embeddings, n_layers, n_heads, d_model, d_head_inner, d_ff_inner, dropout=0.1, dropouta=0.0, seq_len: int = 0, mem_len: int = 0)[source]
__init__(num_embeddings, n_layers, n_heads, d_model, d_head_inner, d_ff_inner, dropout=0.1, dropouta=0.0, seq_len: int = 0, mem_len: int = 0)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

init_memory(device=device(type='cpu')) FloatTensor[source]
update_memory(previous_memory: List[FloatTensor], hidden_states: List[FloatTensor])[source]
reset_length(seq_len, ext_len, mem_len)[source]
forward(idxs: LongTensor, target: LongTensor, memory: List[FloatTensor] | None = None) Dict[str, Tensor][source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.