<jupyter_start><jupyter_text><jupyter_code>import torch
from torch.utils.data import DataLoader
from torch import nn
from torchvision import datasets, transforms
import os
training_data = datasets.FashionMNIST(
    root="data",
    train=True,
    download=True,
    transform=transforms.ToTensor()
)

test_data = datasets.FashionMNIST(
    root="data",
    train=False,
    download=True,
    transform=transforms.ToTensor()
)

train_dataloader = DataLoader(training_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)
device = 'cuda' if torch.cuda.is_available() else  'cpu'
print(f'Using {device} device ')
class NueralNetwork(nn.Module):
    def __init__(self):
        super(NueralNetwork, self).__init__()
        self.flatten = nn.Flatten()
        self.linear_relu_stack = nn.Sequential(nn.Linear(28 * 28, 512),
                                               nn.ReLU(), nn.Linear(512, 512),
                                               nn.ReLU(), nn.Linear(512, 10))

    
    def forward(self, x):
        x = self.flatten(x)
        logits = self.linear_relu_stack(x)
        return logits
model = NueralNetwork().to(device)
print(model)
x = torch.rand((1, 28, 28), device=device)
logits = model(x)
pred_prob = nn.Softmax(dim=1)(logits)
y_pred = pred_prob.argmax(1)
print(f'Predicted class {y_pred}')
learning_rate = 1e-3
batch_size = 64
epochs = 10
def train_loop(dataloader, model, loss_fn, optimizer):
    size = len(dataloader.dataset)
    for batch, (X, y) in enumerate(dataloader):

        #Forward Pass
        pred = model(X)
        loss = loss_fn(pred, y)

        #Backward Pass
        optimizer.zero_grad() #Reset optimizer's computed grads to 0
        loss.backward() #Compute all gradients
        optimizer.step() #Update weights with each of their newly computed gradients

        if batch % 100 == 0:
            loss, current = loss.item(), batch * len(X)
            print(f"loss: {loss:>7f}  [{current:>5d}/{size:>5d}]")
def test_loop(dataloader, model, loss_fn):
    size = len(dataloader.dataset)
    test_loss, correct = 0, 0

    with torch.no_grad():
        for X, y in dataloader:
            pred = model(X)
            test_loss += loss_fn(pred, y).item()
            correct += (pred.argmax(1) == y).type(torch.float).sum().item()
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

for t in range(epochs):
    print(f"Epoch {t+1}\n-------------------------------")
    train_loop(train_dataloader, model, loss_fn, optimizer)
    test_loop(test_dataloader, model, loss_fn)
print("Done!")<jupyter_output>Epoch 1
-------------------------------
loss: 2.306278  [    0/60000]
loss: 2.296587  [ 6400/60000]
loss: 2.274753  [12800/60000]
loss: 2.264477  [19200/60000]
loss: 2.243316  [25600/60000]
loss: 2.224376  [32000/60000]
loss: 2.222136  [38400/60000]
loss: 2.195870  [44800/60000]
loss: 2.195055  [51200/60000]
loss: 2.156963  [57600/60000]
Epoch 2
-------------------------------
loss: 2.164903  [    0/60000]
loss: 2.156428  [ 6400/60000]
loss: 2.094105  [12800/60000]
loss: 2.107533  [19200/60000]
loss: 2.043602  [25600/60000]
loss: 1.997468  [32000/60000]
loss: 2.018254  [38400/60000]
loss: 1.943678  [44800/60000]
loss: 1.954789  [51200/60000]
loss: 1.875761  [57600/60000]
Epoch 3
-------------------------------
loss: 1.907375  [    0/60000]
loss: 1.879389  [ 6400/60000]
loss: 1.756700  [12800/60000]
loss: 1.800603  [19200/60000]
loss: 1.679145  [25600/60000]
loss: 1.642569  [32000/60000]
loss: 1.660160  [38400/60000]
loss: 1.567812  [44800/60000]
loss: 1.596884  [51200/60000]
loss: 1.49[...]
{'fraction_non_alphanumeric': 0.1663448275862069, 'fraction_numerical': 0.14648275862068966, 'mean_word_length': 2.769230769230769, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '97191', 'license': 'CC0-1.0', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1345, 'n_words': 287, 'path': 'Pytorch_MNIST.ipynb', 'repo_name': 'dude123studios/TransformerLearning', 'size': '3625'}