<jupyter_start><jupyter_text><jupyter_code>import torch
import torchvision
from torchvision import transforms, datasets
train = datasets.MNIST('', train=True, download= True,
                       transform = transforms.Compose([
                            transforms.ToTensor()
                       ]))

test = datasets.MNIST('', train=False, download= True,
                       transform = transforms.Compose([
                            transforms.ToTensor()
                       ]))
trainset = torch.utils.data.DataLoader(train, batch_size=10, shuffle = True)
testset = torch.utils.data.DataLoader(test, batch_size=10, shuffle = False)
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):

  def __init__(self):
    super().__init__()

    self.fc1 = nn.Linear(784, 64)
    self.fc2 = nn.Linear(64, 64)
    self.fc3 = nn.Linear(64, 64)
    self.fc4 = nn.Linear(64, 10)
  
  def forward(self, x):

    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = F.relu(self.fc3(x))
    x = self.fc4(x)

    return F.log_softmax(x, dim =1)
  
net = Net()
print(net)


X = torch.rand((28,28))
X = X.view(-1,28*28)

output = net(X)
print(output)
import torch.optim as optim
from tqdm.notebook import tqdm

optimizer = optim.Adam(net.parameters(),lr = 0.001)

EPOCHS = 3

for epoch in tqdm(range(EPOCHS)):
  for data in trainset:

    X,y = data
    net.zero_grad()
    output = net(X.view(-1, 28*28))
    loss = F.nll_loss(output,y)
    loss.backward()
    optimizer.step()
  print(loss)

correct = 0
total = 0

with torch.no_grad():
  for data in testset:
    X, y = data
    output = net(X.view(-1,784))
    for idx, i in enumerate(output):
      if torch.argmax(i) == y[idx]:
        correct +=1
      total +=1

print('Accuracy: ', round(correct/total,3))
import matplotlib.pyplot as plt
from random import randint


n = 4
plt.imshow(X[n].view(28,28))
plt.show()
prediction = torch.argmax(net(X[n].view(-1,784))[0])
print(prediction)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.144, 'fraction_numerical': 0.034, 'mean_word_length': 2.75422138836773, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '61626', 'license': 'MIT', 'n_tokens_mistral': 780, 'n_tokens_neox': 719, 'n_words': 161, 'path': 'PyTorch - MNIST.ipynb', 'repo_name': 'xBrymer/PyTorch-Basics', 'size': '2000'}