赞
踩
好的,这是一个使用 transformer 模型进行图像分类的简单代码示例:
``` import torch import torch.nn as nn import torchvision.transforms as transforms import torchvision.datasets as datasets
train_dataset = datasets.CIFAR10(root='./data', train=True, transform=transforms.ToTensor(), download=True) test_dataset = datasets.CIFAR10(root='./data', train=False, transform=transforms.ToTensor(), download=True)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=64, shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=64, shuffle=False)
class Transformer(nn.Module): def init(self): super(Transformer, self).init() self.layer1 = nn.Linear(33232, 128) self.layer2 = nn.Linear(128, 64) self.layer3 = nn.Linear(64, 10)
- def forward(self, x):
- x = x.reshape(x.size(0), -1)
- x = torch.relu(self.layer1(x))
- x = torch.relu(self.layer2(x))
- x = self.layer3(x)
- return x
model = Transformer()
criterion = nn.CrossEntropyLoss() optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
for epoch in range(10): for i, (images, labels) in enumerate(train_loader): outputs = model(images) loss = criterion(outputs, labels)
- optimizer.zero_grad()
- loss.backward()
- optimizer.step()if (i+1) % 100 == 0:
- print (f'Epoch [{epoch+1}/10], Step [{i+1}/{len(train_loader)}], Loss: {loss.item()}')
with torch.no_grad(): correct = 0 total = 0 for images, labels in test_loader: outputs = model(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item()
print(f'Accuracy of the model on the test
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。