<jupyter_start><jupyter_text>Setting a device<jupyter_code>device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)<jupyter_output>cuda<jupyter_text>Initializing the Hyperparameters<jupyter_code>num_classes = 10
learning_rate = 1e-3
batch_size = 1024
num_epochs = 5<jupyter_output><empty_output><jupyter_text>Load Pretrain Model<jupyter_code>class Identity(nn.Module):
  def __init__(self):
    super(Identity, self).__init__()

  def forward(self, x):
    return x
model = torchvision.models.vgg16(pretrained=True)

# Freezing the parameters with no_grad
for param in model.parameters():
  param.requires_grad = False
model.avgpool = Identity() # avgpool[1] = Identity()

# Now we are changing the FC layer ... so for this, requires_grad=True
model.classifier = nn.Sequential(
    nn.Linear(512, 100), nn.ReLU(), nn.Dropout(p=0.5), nn.Linear(100, num_classes)
) # since the no of kernels/filters in vgg is 512

model.to(device)<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>train_dataset = datasets.CIFAR10(
    root="dataset/", train=True, transform=transforms.ToTensor(), download=True
)
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)

test_dataset = datasets.CIFAR10(
    root="dataset/", train=False, transform=transforms.ToTensor(), download=True
)
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=True)<jupyter_output>Files already downloaded and verified
Files already downloaded and verified<jupyter_text>Loss & Optimizer<jupyter_code>criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)<jupyter_output><empty_output><jupyter_text>Training the Model<jupyter_code>for epoch in tqdm(range(num_epochs)):
  losses = []

  for ind, (data, target) in enumerate(train_loader):
    data = data.to(device=device)
    target = target.to(device=device)
    
    # forward
    scores = model(data)
    loss = criterion(scores, target)
    losses.append(loss)

    # backward
    optimizer.zero_grad() # set all the gradients to 0 for each batch
    loss.backward()

    # gradient descent or adam step - updates the parameters
    optimizer.step()

  mean_loss = sum(losses)/len(losses)

  print(f"\nLoss at Epoch {epoch+1} is {mean_loss}")<jupyter_output>20%|██        | 1/5 [00:22<01:28, 22.09s/it]<jupyter_text>Testing the Model<jupyter_code>def check_accuracy(loader, model):
  num_correct = 0
  num_samples = 0
  model.eval() # Let the model know that this is evaluation mode

  with torch.no_grad():
    for x, y in loader:
      x = x.to(device=device)
      y = y.to(device=device)

      scores = model(x)
      
      # Which class has the max value
      _, pred = scores.max(dim=1)
      num_correct += (pred == y).sum()
      num_samples += pred.size(0) # 64

    print(f"Got {num_correct} / {num_samples} with accuracy {(float(num_correct)/float(num_samples)*100):.2f}%")
check_accuracy(test_loader, model)<jupyter_output>Got 6021 / 10000 with accuracy 60.21%
{'fraction_non_alphanumeric': 0.12091179385530228, 'fraction_numerical': 0.02345556656755864, 'mean_word_length': 5.056, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '667365', 'license': 'MIT', 'n_tokens_mistral': 1112, 'n_tokens_neox': 1030, 'n_words': 223, 'path': '1/7-transfer-learning-and-finetuning-v-1/7_Transfer_Learning_and_Finetuning.ipynb', 'repo_name': 'HussamCheema/pytorch', 'size': '3027'}