<jupyter_start><jupyter_text><jupyter_code>import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import tqdm
import matplotlib.pyplot as plt
transform = transforms.Compose([ transforms.ToTensor(),transforms.Normalize((0.5,0.5),(0.5,0.5))])
train_set = torchvision.datasets.FashionMNIST(root = './data',train = True,download=True,transform=transform)
test_set = torchvision.datasets.FashionMNIST(root = './data',train = False,download=True,transform=transform)
train_set.data.shape
train_set.targets.shape
fig = plt.figure(figsize=(5,5))
row = 7
col = 4
for i in range(1,col*row+1):
  ima = train_set.data[i]
  fig.add_subplot(row,col,i)
  plt.imshow(ima)
plt.show()
plt.imshow(train_set.data[0])
train_set.data.shape
class CNNetwork(nn.Module):
  def __init__(self,im_channel = 1,output_channel = 6,hidden_units = 64):
    super(CNNetwork,self).__init__()
    self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5)
    self.conv2 = nn.Conv2d(in_channels=6, out_channels=12, kernel_size=5)

    self.fc1 = nn.Linear(in_features=12*4*4, out_features=120)
    self.fc2 = nn.Linear(in_features=120, out_features=60)
    self.out = nn.Linear(in_features=60, out_features=10)

  def forward(self, t):
    t = self.conv1(t)
    t = F.relu(t)
    t = F.max_pool2d(t, kernel_size=2, stride=2)
    t = self.conv2(t)
    t = F.relu(t)
    t = F.max_pool2d(t, kernel_size=2, stride=2)
    t = t.reshape(-1, 12*4*4)
    t = self.fc1(t)
    t = F.relu(t)
    t = self.fc2(t)
    t = F.relu(t)
    t = self.out(t)
    return t<jupyter_output><empty_output><jupyter_text><jupyter_code>epochs = 20
loss = torch.nn.NLLLoss()
model = CNNetwork().to('cuda')
optimizer = torch.optim.Adam(model.parameters(),lr = 0.01)
print(model)
batch_size = 64

i = 1
start = 1
while i < epochs:
  for im in range(batch_size):
    end = batch_size*i
    y_pred = model((train_set.data[start:end]).to('cuda'))
    optimizer.zero_grad()
    loss = loss_fn(y_pred,label.to('cuda'))
    print(loss)
    loss.backward()
    optimizer.step()
  start = end
  i += 1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15300805305542398, 'fraction_numerical': 0.03458076740881099, 'mean_word_length': 4.557894736842106, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '193852', 'license': 'MIT', 'n_tokens_mistral': 889, 'n_tokens_neox': 851, 'n_words': 143, 'path': 'pytorch5.ipynb', 'repo_name': 'vijayshankarrealdeal/intro_to_pytorch', 'size': '2111'}