numpyGPT provides a clear and hands-on approach to understanding GPT architecture by using only NumPy and Python's standard library. By implementing each layer from scratch without any frameworks, this project emphasizes manual computation of gradients and operations, making it ideal for educational purposes and deep learning enthusiasts.
numpyGPT is an implementation of a Generative Pre-trained Transformer (GPT) from scratch, utilizing only NumPy and Python’s standard library. This project emphasizes a deep understanding of the core components of neural networks, including modules, tokenizers, optimizers, and backpropagation, without relying on any automatic differentiation frameworks or tensor abstractions. The result is a clear, transparent implementation where every computation, including gradient calculations, is explicit.
The package comprises several functionalities, including:
Linear, Embedding, LayerNorm, Softmax, ReLU, MultiHeadAttention, and FeedForward.Adam optimizer for efficient training.GPT model based on the transformer decoder.Here’s a sample implementation of a linear layer to illustrate the code style and structure:
class Linear:
def __init__(self, in_features, out_features):
self.W = np.random.randn(in_features, out_features) * 0.02
self.b = np.zeros(out_features)
def forward(self, X):
self.X = X # cache for backward
return X @ self.W + self.b
def backward(self, dY):
self.dW = self.X.T @ dY # gradient w.r.t weights
self.db = np.sum(dY, axis=0) # gradient w.r.t bias
dX = dY @ self.W.T # gradient w.r.t input
return dX
The repository includes various educational documents that explain:
The project also conducts experiments to evaluate the efficacy of different tokenization strategies (Character-level, Word-level, BPE) on training transformer models using Shakespeare's text. With detailed hyperparameters and evaluation metrics, insights into model performance, output quality, and parameter efficiency are highlighted.
In summary, numpyGPT serves as an educational resource for those looking to gain a deeper understanding of neural networks and the underlying mechanics of modern language models. The hands-on implementation provides a fundamental foundation that is crucial for advancing in the field of machine learning.
No comments yet.
Sign in to be the first to comment.