Transformers做cifar-10图像分类任务pytorch版本效果好且画出loss变化曲线以及准确率变化曲线的代码以及测试代码
时间: 2024-01-17 18:04:37 浏览: 201
以下是使用PyTorch实现Transformer模型进行CIFAR-10图像分类任务的代码,同时画出了loss变化曲线和准确率变化曲线:
```python
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# 定义超参数
num_epochs = 10
batch_size = 128
learning_rate = 0.001
# 加载CIFAR-10数据集并做数据增强
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
train_dataset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_dataset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
# 定义Transformer模型
class TransformerModel(nn.Module):
def __init__(self, input_dim, hidden_dim, num_classes, num_layers, num_heads, dropout):
super().__init__()
self.embedding = nn.Linear(input_dim, hidden_dim)
self.pos_encoding = PositionalEncoding(hidden_dim, dropout)
self.transformer_layers = nn.ModuleList([
TransformerLayer(hidden_dim, num_heads, dropout) for _ in range(num_layers)
])
self.fc = nn.Linear(hidden_dim, num_classes)
def forward(self, x):
x = self.embedding(x)
x = self.pos_encoding(x)
for layer in self.transformer_layers:
x = layer(x)
x = torch.mean(x, dim=1)
x = self.fc(x)
return x
class PositionalEncoding(nn.Module):
def __init__(self, hidden_dim, dropout, max_len=5000):
super().__init__()
self.dropout = nn.Dropout(p=dropout)
pe = torch.zeros(max_len, hidden_dim)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(torch.arange(0, hidden_dim, 2).float() * (-math.log(10000.0) / hidden_dim))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0).transpose(0, 1)
self.register_buffer('pe', pe)
def forward(self, x):
x = x + self.pe[:x.size(0), :]
return self.dropout(x)
class TransformerLayer(nn.Module):
def __init__(self, hidden_dim, num_heads, dropout):
super().__init__()
self.self_attn = nn.MultiheadAttention(hidden_dim, num_heads, dropout=dropout)
self.dropout1 = nn.Dropout(p=dropout)
self.norm1 = nn.LayerNorm(hidden_dim)
self.fc = nn.Sequential(
nn.Linear(hidden_dim, 4 * hidden_dim),
nn.GELU(),
nn.Linear(4 * hidden_dim, hidden_dim),
nn.Dropout(p=dropout)
)
self.dropout2 = nn.Dropout(p=dropout)
self.norm2 = nn.LayerNorm(hidden_dim)
def forward(self, x):
attn_output, _ = self.self_attn(x, x, x)
x = x + self.dropout1(attn_output)
x = self.norm1(x)
fc_output = self.fc(x)
x = x + self.dropout2(fc_output)
x = self.norm2(x)
return x
# 实例化模型
model = TransformerModel(input_dim=32*32*3, hidden_dim=512, num_classes=10, num_layers=6, num_heads=8, dropout=0.1).to(device)
# 定义损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# 训练模型
train_loss_list = []
train_acc_list = []
test_loss_list = []
test_acc_list = []
total_step = len(train_loader)
for epoch in range(num_epochs):
running_loss = 0.0
running_corrects = 0
for i, (images, labels) in enumerate(train_loader):
images = images.reshape(-1, 32*32*3).to(device)
labels = labels.to(device)
outputs = model(images)
loss = criterion(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
_, predicted = torch.max(outputs.data, 1)
running_loss += loss.item() * images.size(0)
running_corrects += torch.sum(predicted == labels.data)
if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
epoch_loss = running_loss / len(train_dataset)
epoch_acc = running_corrects.double() / len(train_dataset)
train_loss_list.append(epoch_loss)
train_acc_list.append(epoch_acc)
print('Epoch [{}/{}], Train Loss: {:.4f}, Train Accuracy: {:.4f}'
.format(epoch+1, num_epochs, epoch_loss, epoch_acc))
# 在测试集上测试模型
with torch.no_grad():
running_loss = 0.0
running_corrects = 0
for images, labels in test_loader:
images = images.reshape(-1, 32*32*3).to(device)
labels = labels.to(device)
outputs = model(images)
loss = criterion(outputs, labels)
_, predicted = torch.max(outputs.data, 1)
running_loss += loss.item() * images.size(0)
running_corrects += torch.sum(predicted == labels.data)
epoch_loss = running_loss / len(test_dataset)
epoch_acc = running_corrects.double() / len(test_dataset)
test_loss_list.append(epoch_loss)
test_acc_list.append(epoch_acc)
print('Epoch [{}/{}], Test Loss: {:.4f}, Test Accuracy: {:.4f}'
.format(epoch+1, num_epochs, epoch_loss, epoch_acc))
# 画出loss变化曲线和准确率变化曲线
plt.subplot(2, 1, 1)
plt.plot(train_loss_list, label='Train')
plt.plot(test_loss_list, label='Test')
plt.title('Loss')
plt.legend()
plt.subplot(2, 1, 2)
plt.plot(train_acc_list, label='Train')
plt.plot(test_acc_list, label='Test')
plt.title('Accuracy')
plt.legend()
plt.show()
# 在测试集上测试模型
model.eval()
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images = images.reshape(-1, 32*32*3).to(device)
labels = labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Test Accuracy: {:.4f}'.format(correct / total))
```
在上面的代码中,我们使用了PyTorch内置的`nn.MultiheadAttention`和`nn.LayerNorm`来实现Transformer模型中的自注意力机制和层归一化,同时也实现了Transformer中的位置编码和前馈网络。在训练过程中,我们记录了每个epoch的训练集loss和准确率、测试集loss和准确率,并画出了loss变化曲线和准确率变化曲线。最后,我们在测试集上测试了模型的准确率。
阅读全文