appendix_a.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright (c) Sebastian Raschka under Apache License 2.0 (see LICENSE.txt).
  2. # Source for "Build a Large Language Model From Scratch"
  3. # - https://www.manning.com/books/build-a-large-language-model-from-scratch
  4. # Code: https://github.com/rasbt/LLMs-from-scratch
  5. import torch
  6. from torch.utils.data import Dataset
  7. class NeuralNetwork(torch.nn.Module):
  8. def __init__(self, num_inputs, num_outputs):
  9. super().__init__()
  10. self.layers = torch.nn.Sequential(
  11. # 1st hidden layer
  12. torch.nn.Linear(num_inputs, 30),
  13. torch.nn.ReLU(),
  14. # 2nd hidden layer
  15. torch.nn.Linear(30, 20),
  16. torch.nn.ReLU(),
  17. # output layer
  18. torch.nn.Linear(20, num_outputs),
  19. )
  20. def forward(self, x):
  21. logits = self.layers(x)
  22. return logits
  23. class ToyDataset(Dataset):
  24. def __init__(self, X, y):
  25. self.features = X
  26. self.labels = y
  27. def __getitem__(self, index):
  28. one_x = self.features[index]
  29. one_y = self.labels[index]
  30. return one_x, one_y
  31. def __len__(self):
  32. return self.labels.shape[0]