<jupyter_start><jupyter_text>Use a pretrained ResNet model, reset the final layer to number of classes for our dataset.<jupyter_code>model = models.resnet18(pretrained=True)
model.fc = nn.Linear(model.fc.in_features, len(classes))
model = model.to(device)

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters())<jupyter_output>Downloading: "https://download.pytorch.org/models/resnet18-5c106cde.pth" to /root/.cache/torch/hub/checkpoints/resnet18-5c106cde.pth<jupyter_text>Tune weights of the model.<jupyter_code># best_model = copy.deepcopy(model.state_dict())
epochs = 10
for epoch in range(epochs):
  running_loss = 0.0
  for i, data in enumerate(train_loader, 0):
    inputs, labels = data[0].to(device), data[1].to(device)
    optimizer.zero_grad()
    outputs = model(inputs)
    loss = criterion(outputs, labels)
    loss.backward()
    optimizer.step()
    running_loss += loss.item()
    if i % 100 == 99:  # print every 100 mini-batches
      print(f'{datetime.now().time().replace(microsecond=0)} --- '
            f'Epoch: {epoch + 1}\t'
            f'Batch: {i + 1}\t'
            f'Loss: {running_loss / 100:.3f}')
      running_loss = 0.0
  correct = 0
  total = 0

  with torch.no_grad():
    for data in validate_loader:
      images, labels = data[0].to(device), data[1].to(device)
      outputs = model(images)
      _, predicted = torch.max(outputs.data, 1)
      total += labels.size(0)
      correct += (predicted == labels).sum().item()
  print(f'{correct} correct predictions out of {total}. Accuracy: {100 * correct / total}')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14535246412975672, 'fraction_numerical': 0.029320024953212728, 'mean_word_length': 3.7596439169139466, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, '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': '22332', 'license': 'MIT', 'n_tokens_mistral': 589, 'n_tokens_neox': 554, 'n_words': 117, 'path': '04-transfer_learning.ipynb', 'repo_name': 'btian/deep-learning', 'size': '1603'}