Finetuning
Finetuning
Examples:
o Speech: wav2vec2-base
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
])
model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased",
num_labels=2)
model = models.resnet50(pretrained=True)
4. Modify the Output Layer
For CV (PyTorch):
import torch.nn as nn
NLP Example:
CV Example:
import torch.nn as nn
criterion = nn.CrossEntropyLoss()
import torch
model.to(device)
model.train()
optimizer.zero_grad()
outputs = model(**batch)
loss = outputs.loss
loss.backward()
optimizer.step()
7. Evaluate the Model
preds = model(**batch).logits.argmax(dim=-1)
print(f"Accuracy: {accuracy:.4f}")
Would you like code for a specific framework (PyTorch, TensorFlow) or a particular model (e.g., DistilBERT,
ResNet)?