给我写一个resnet50的pytorch程序
时间: 2023-11-21 10:04:09 浏览: 74
自己实现的resnet18,使用pytorch 在数据集cifar10,miniimagenet跑
5星 · 资源好评率100%
当然可以!以下是一个简单的ResNet50 PyTorch程序示例:
```python
import torch.nn as nn
import torch.optim as optim
from torchvision import models
resnet50 = models.resnet50(pretrained=True)
for param in resnet50.parameters():
param.requires_grad = False
num_ftrs = resnet50.fc.in_features
resnet50.fc = nn.Linear(num_ftrs, 10) # 假设输出10个类别
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(resnet50.fc.parameters(), lr=0.001, momentum=0.9)
# 接下来是训练循环,可以根据具体数据集进行修改
for epoch in range(num_epochs):
for inputs, labels in dataloader:
optimizer.zero_grad()
outputs = resnet50(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
```
希望这可以帮到您!
阅读全文