<jupyter_start><jupyter_text><jupyter_code>import torch 
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
# device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device
# hyper parameters
num_epochs = 20
batch_size = 64
learning_rate = 0.001
# load dataset
transform = transforms.Compose(
    [
     transforms.ToTensor(), 
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ]
)

train_dataset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                             download=True, transform=transform)
test_dataset = torchvision.datasets.CIFAR10(root='./data', train=False,
                                             download=True, transform=transform)

train_loader = torch.utils.data.DataLoader(train_dataset, 
                                           batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset, 
                                           batch_size=batch_size, shuffle=False)

classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck') 
# convolutional neural network
class ConvNet(nn.Module):
  def __init__(self):
    super(ConvNet, self).__init__()

    # 5x5 conv kernel/filter
    self.conv1 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=5) 
    
    # 2x2 maxpooling
    self.pool =  nn.MaxPool2d(kernel_size=2, stride=2) 
    
    # 5x5 conv kernel/filter
    self.conv2 = nn.Conv2d(6, 16, 5)
    
    # 3x32x32 --conv1--> 6x28x28 --maxpool--> 6x14x14
    # 6x14x14 --conv2--> 16x10x10 --maxpool--> 16x5x5
    self.fc1 = nn.Linear(16*5*5, 120) 
    self.fc2 = nn.Linear(120, 84) 
    self.fc3 = nn.Linear(84, 10)
    self.relu = nn.ReLU()

  def forward(self, input):
    # conv1 -> relu -> maxpool
    output = self.pool(self.relu(self.conv1(input))) 
    # conv2 -> relu -> maxpool
    output = self.pool(self.relu(self.conv2(output)))

    # flatten
    output = output.reshape(-1, 16*5*5)
    # -> fc1 -> relu
    output = self.relu(self.fc1(output))
    # -> fc2 -> relu
    output = self.relu(self.fc2(output))
    # fc3 (no activation at end)
    output = self.fc3(output)

    return output
model = ConvNet().to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
for epoch in range(num_epochs):
  avg_batch_loss = 0
  for step, (images, labels) in enumerate(train_loader):
    images = images.to(device)
    labels = labels.to(device)

    # forward pass
    outputs = model(images)
    loss = criterion(outputs, labels)

    # backward pass
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    # accumulate batch loss
    avg_batch_loss+=loss

  avg_batch_loss /= len(train_loader)

  print(f'epoch {epoch+1}/{num_epochs}, loss = {avg_batch_loss:.4f}')
PATH = './cnn.pth'
torch.save(model.state_dict(), PATH)

with torch.no_grad():
  n_correct = 0
  n_samples = 0
  for images, labels in test_loader:
      images = images.to(device)
      labels = labels.to(device)
      outputs = model(images)
      # max returns (value ,index)
      _, predicted = torch.max(outputs, 1)
      n_samples += labels.size(0)
      n_correct += (predicted == labels).sum().item()

  acc = 100.0 * n_correct / n_samples
  print(f'Accuracy of the network: {acc}%')<jupyter_output>Accuracy of the network: 62.47%
{'fraction_non_alphanumeric': 0.13393375944218477, 'fraction_numerical': 0.03631609529343405, 'mean_word_length': 2.8512304250559284, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '9990', 'license': 'MIT', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1176, 'n_words': 247, 'path': 'cnn_cifar10.ipynb', 'repo_name': 'ferdouszislam/pytorch-practice', 'size': '3442'}