mirror of
https://github.com/rasbt/LLMs-from-scratch.git
synced 2025-08-09 17:23:06 +00:00
45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
# Copyright (c) Sebastian Raschka under Apache License 2.0 (see LICENSE.txt).
|
|
# Source for "Build a Large Language Model From Scratch"
|
|
# - https://www.manning.com/books/build-a-large-language-model-from-scratch
|
|
# Code: https://github.com/rasbt/LLMs-from-scratch
|
|
|
|
import torch
|
|
from torch.utils.data import Dataset
|
|
|
|
|
|
class NeuralNetwork(torch.nn.Module):
|
|
def __init__(self, num_inputs, num_outputs):
|
|
super().__init__()
|
|
|
|
self.layers = torch.nn.Sequential(
|
|
|
|
# 1st hidden layer
|
|
torch.nn.Linear(num_inputs, 30),
|
|
torch.nn.ReLU(),
|
|
|
|
# 2nd hidden layer
|
|
torch.nn.Linear(30, 20),
|
|
torch.nn.ReLU(),
|
|
|
|
# output layer
|
|
torch.nn.Linear(20, num_outputs),
|
|
)
|
|
|
|
def forward(self, x):
|
|
logits = self.layers(x)
|
|
return logits
|
|
|
|
|
|
class ToyDataset(Dataset):
|
|
def __init__(self, X, y):
|
|
self.features = X
|
|
self.labels = y
|
|
|
|
def __getitem__(self, index):
|
|
one_x = self.features[index]
|
|
one_y = self.labels[index]
|
|
return one_x, one_y
|
|
|
|
def __len__(self):
|
|
return self.labels.shape[0]
|