Code
Hub
Workspaces
Following
Trending
Connect
MCP
copy
Create free account
hub
/
github.com/alan-cooney/transformer-from-scratch
/ functions
Functions
66 in github.com/alan-cooney/transformer-from-scratch
⨍
Functions
66
◇
Types & classes
26
↓ 4 callers
Function
cross_entropy_loss
Language Model Cross Entropy Loss Loss is calculated as the average negative log probs of the correct tokens. https://arxiv.org/pdf/1706.037
transformer_from_scratch/components/cross_entropy_loss.py:18
↓ 3 callers
Method
attention
Attention Calculation. The attention calculation does two things for each destination token - it both moves information from source t
transformer_from_scratch/components/attention.py:97
↓ 3 callers
Function
evaluate
Evaluate the model on a test dataloader Args: model (Transformer): Transformer model test_dataloader (DataLoader): Test dataloade
transformer_from_scratch/train.py:38
↓ 2 callers
Function
get_default_device
Get the default device to use. Returns: torch.device: Device to use.
transformer_from_scratch/train.py:23
↓ 2 callers
Method
mask
Mask the attention pattern. Each attention pattern is of shape (dest, src), and each element represents the attention that a destinat
transformer_from_scratch/components/attention.py:78
↓ 2 callers
Function
train_loop
Train loop Args: model: The Transformer model to train. train_dataloader: Dataloader for training data. test_dataloader:
transformer_from_scratch/train.py:67
↓ 1 callers
Method
__init__
Initialize the Embed layer.
transformer_from_scratch/components/embed_unembed.py:42
↓ 1 callers
Method
__init__
(self, inputs: BatchTokenIndices)
transformer_from_scratch/tests/test_train.py:42
↓ 1 callers
Function
get_datasets
()
transformer_from_scratch/examples/gpt2_code_comparison.py:36
↓ 1 callers
Function
get_tokenized_datasets
Get the tokenized datasets.
transformer_from_scratch/examples/gpt2_code_comparison.py:67
↓ 1 callers
Function
train
Train.
transformer_from_scratch/examples/gpt2_code_comparison.py:86
Method
__getitem__
(self, idx: int)
transformer_from_scratch/components/tests/test_embed_unembed.py:75
Method
__getitem__
(self, index: int)
transformer_from_scratch/components/tests/test_mlp.py:44
Method
__getitem__
(self, index)
transformer_from_scratch/components/tests/test_mlp.py:55
Method
__getitem__
(self, index)
transformer_from_scratch/components/tests/test_mlp.py:67
Method
__getitem__
(self, index)
transformer_from_scratch/components/tests/test_mlp.py:83
Method
__getitem__
(self, index)
transformer_from_scratch/components/tests/test_attention.py:133
Method
__getitem__
(self, index)
transformer_from_scratch/tests/test_train.py:46
Method
__init__
Initialise the Transformer.
transformer_from_scratch/transformer.py:20
Method
__init__
Initialize the positional encoding matrix.
transformer_from_scratch/components/positional_encoding.py:128
Method
__init__
Initialize the MultiHeadAttention module.
transformer_from_scratch/components/attention.py:37
Method
__init__
MLP Sub-Layer Initialisation.
transformer_from_scratch/components/mlp.py:33
Method
__init__
Initialise the full layer.
transformer_from_scratch/components/layer.py:22
Method
__init__
Initialize the Unembed Layer.
transformer_from_scratch/components/embed_unembed.py:86
Method
__init__
Initialise the dataset. Args: num_samples (int): Number of samples d_vocab (int): Vocab size (e.g. if 3 then it'll ge
transformer_from_scratch/components/tests/test_embed_unembed.py:50
Method
__init__
(self, d_vocab: int, d_model: int)
transformer_from_scratch/components/tests/test_embed_unembed.py:85
Method
__init__
Initialize the dataset. Args: num_samples (int): Number of samples sequence_length (int): Sequence length
transformer_from_scratch/components/tests/test_mlp.py:24
Method
__init__
(self, sequence_length: int, d_model: int, num_samples: int)
transformer_from_scratch/components/tests/test_attention.py:114
Method
__init__
(self, output_indices: int, vocab_size: int)
transformer_from_scratch/tests/test_train.py:19
Method
__len__
(self)
transformer_from_scratch/components/tests/test_embed_unembed.py:72
Method
__len__
Get the number of samples in the dataset. Returns: int: Number of samples in the dataset
transformer_from_scratch/components/tests/test_mlp.py:36
Method
__len__
(self)
transformer_from_scratch/components/tests/test_attention.py:130
Method
__len__
(self)
transformer_from_scratch/tests/test_train.py:49
Method
forward
Forward pass. Args: tokens (BatchTokenIndices): Input tokens (indices rather than one-hot) Returns: BatchLog
transformer_from_scratch/transformer.py:39
Method
forward
Apply the positional encoding to the given input embedding. Args: embedding (ResidualStream): The input embedding with shape (bat
transformer_from_scratch/components/positional_encoding.py:148
Method
forward
Attention layer forward pass. https://arxiv.org/pdf/1706.03762.pdf (p5)
transformer_from_scratch/components/attention.py:142
Method
forward
Forward Pass through the MLP Sub-Layer. Args: residual_stream (ResidualStream): MLP input Returns: ResidualS
transformer_from_scratch/components/mlp.py:57
Method
forward
Forward pass. Args: residual_stream (ResidualStream): Residual stream Returns: ResidualStream: Updated resid
transformer_from_scratch/components/layer.py:32
Method
forward
Forward Pass through the Embedding Layer. The original paper multiples the embedding by sqrt(d_model) during the forward pass, presum
transformer_from_scratch/components/embed_unembed.py:57
Method
forward
Forward Pass through the Unembedding Layer. Args: residual_stream (ResidualStream): Residual stream Returns:
transformer_from_scratch/components/embed_unembed.py:96
Method
forward
Forward Pass.
transformer_from_scratch/components/tests/test_embed_unembed.py:91
Method
forward
Forward pass Args: x (BatchTokenIndices): Inputs Returns: BatchLogits: 100% log probabilities of the specifi
transformer_from_scratch/tests/test_train.py:24
Method
test_attend_flagged_token
Test that the model can attend to a specific (flagged) token.
transformer_from_scratch/components/tests/test_attention.py:141
Method
test_attention_simple
Test a simple attention calculation
transformer_from_scratch/components/tests/test_attention.py:53
Method
test_enum_elements_are_unique
Verify that we don't have any repeated enum elements.
transformer_from_scratch/tests/test_types.py:8
Function
test_feed_forward_learns_on_dataset
Test the MLP network learns to solve the regression tasks. Args: dataset_class (RegressionTaskDataset): Dataset to test.
transformer_from_scratch/components/tests/test_mlp.py:97
Method
test_fully_accurate_model
Test that a model that always outputs the correct token index, gets 100% accuracy.
transformer_from_scratch/tests/test_train.py:67
Method
test_fully_inaccurate_model
Test that a model that always outputs the incorrect token index, gets 100% accuracy.
transformer_from_scratch/tests/test_train.py:78
Method
test_get_default_device
Test that a valid PyTorch device is returned.
transformer_from_scratch/tests/test_train.py:56
Function
test_layer_adds_attention_and_feed_forward_output
Test if Layer correctly adds attention and feed forward outputs to the residual stream.
transformer_from_scratch/components/tests/test_layer.py:15
Function
test_learn_order_integers
Test that a model with just the embedding and unembedding can learn bigram statistics.
transformer_from_scratch/components/tests/test_embed_unembed.py:97
Function
test_linear_function_for_relative_positions
Test that relative positions can be obtained with just matrix multiplication. Check that the positional encoding of a token at position `initial_
transformer_from_scratch/components/tests/test_positional_encoding.py:42
Function
test_loss_with_random_logits
Test loss with random logits.
transformer_from_scratch/components/tests/test_cross_entropy_loss.py:27
Function
test_loss_with_uniform_logits
Test loss with uniform logits.
transformer_from_scratch/components/tests/test_cross_entropy_loss.py:41
Method
test_mask
Test that it masks correctly
transformer_from_scratch/components/tests/test_attention.py:33
Method
test_model_parameters_change
Test that model parameters change after training.
transformer_from_scratch/tests/test_train.py:111
Function
test_numerically_stable
Test that the encoding is numerically stable. Check our Positional Encoding doesn't create any NaN values.
transformer_from_scratch/components/tests/test_positional_encoding.py:107
Function
test_positional_encoding_against_specific_position_and_dimension_scalar_calculation
Check the positional encoding is as expected for a specific position and dimension. This uses a scalar calculation (as opposed to the vectorized
transformer_from_scratch/components/tests/test_positional_encoding.py:84
Function
test_positional_encoding_each_token_unique
Test that each token will have a unique positional encoding vector.
transformer_from_scratch/components/tests/test_positional_encoding.py:13
Function
test_positional_encoding_same_across_batch_items
Test that the positional encoding is the same for each batch item.
transformer_from_scratch/components/tests/test_positional_encoding.py:122
Method
test_train_loop_executes
Test that the train loop runs without error.
transformer_from_scratch/tests/test_train.py:93
Function
test_transformer_forward
Check that a forward pass can be run.
transformer_from_scratch/tests/test_transformer.py:55
Function
test_transformer_init_correctly
Check that the Transformer is initialised correctly.
transformer_from_scratch/tests/test_transformer.py:20
Function
test_transformer_memorize_dataset
Check the transformer can learn to memorize a simple dataset.
transformer_from_scratch/tests/test_transformer.py:80
Function
test_where_logits_are_fully_accurate
Test loss where the logits are fully accurate (so it should be 0).
transformer_from_scratch/components/tests/test_cross_entropy_loss.py:9
Function
tokenize
Tokenize code.
transformer_from_scratch/examples/gpt2_code_comparison.py:51