<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=True)
for data in trainset:
  print(data)
  break
x, y = data[0][0], data[1][0]
print(y)
print(int(y))
print(x)
import matplotlib.pyplot as plt
plt.imshow(data[0][0].view(28,28)) 
type(trainset)
total = 0
counter_dict = {0:0, 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0}

for data in trainset:
  xs, ys = data
  for y in ys:
    counter_dict[int(y)] += 1
    total+=1

print(counter_dict)
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(28*28, 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)

        # pytorch allows to manipulate and build neural network as our wish i.e.
        
        # if weather = sunny:
        #   x = F.relu(self.fc1(x))
        #   x = F.relu(self.fc2(x))
        #   x = F.relu(self.fc3(x))
        #   x = F.relu(self.fc4(x))
        #   return F.log_softmax(x, dim=1)
        
        # else:
        #   ...

net = Net()
print(net)
x = torch.rand(28,28)
output = net(x.view(1,28*28))
output
import torch.optim as optim
optimizer = optim.Adam(net.parameters(), lr=0.001) # decaying learning rate
Epochs = 3
for epoch in range(Epochs):
  for data in trainset:
    x, y = data
    net.zero_grad()
    output = net(x.view(10,28*28)) # batch = 10
    loss = F.nll_loss(output,y) 
    loss.backward()
    optimizer.step()
  print(loss)
correct = 0
total = 0

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

print("Accuracy: ", round(correct/total, 3))

y
plt.imshow(x[0].view(28,28))
print("Symbol result: ")
print(torch.argmax(net(x[0].view(1,28*28))))
import os
import cv2
import numpy as np
from tqdm import tqdm
import zipfile
!wget --no-check-certificate \
    "https://download.microsoft.com/download/3/E/1/3E1C3F21-ECDB-4869-8368-6DEBA77B919F/kagglecatsanddogs_3367a.zip" \
    -O "/tmp/cats-and-dogs.zip"

local_zip = '/tmp/cats-and-dogs.zip'
zip_ref   = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/tmp')
zip_ref.close()
REBUILD_DATA = True

class DogsVSCats():
  IMG_SIZE = 50
  CATS = "/tmp/PetImages/Cat"
  DOGS = "/tmp/PetImages/Dog"
  LABELS = {CATS:0, DOGS:1}
  training_data = []
  catcount = 0
  dogcount = 0

  def make_training_data(self):
    for label in self.LABELS:
      print(label)
      for f in tqdm(os.listdir(label)):
        try:
          path = os.path.join(label, f)
          img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
          img = cv2.resize(img, (self.IMG_SIZE, self.IMG_SIZE))
          self.training_data.append([np.array(img), np.eye(2)[self.LABELS[label]]])

          if label == self.CATS:
            self.catcount += 1
          elif label == self.DOGS:
            self.dogcount += 1

        except Exception as e:
          pass

    np.random.shuffle(self.training_data)
    np.save("training_data.npy", self.training_data)
    print("Cats: ",self.catcount)
    print("Dogs: ",self.dogcount)


if REBUILD_DATA:
  dogsvcats = DogsVSCats()
  dogsvcats.make_training_data()


training_data = np.load("training_data.npy")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1510864072764022, 'fraction_numerical': 0.037897928246589184, 'mean_word_length': 2.927579365079365, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '541514', 'license': 'MIT', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1529, 'n_words': 313, 'path': 'EECS_598.ipynb', 'repo_name': 'ruixuanz/EECS-598', 'size': '3958'}