pytorch画loss曲线的实例
时间: 2023-11-06 09:56:02 浏览: 125
很高兴为您解答。以下是 PyTorch 画 loss 曲线的实例代码:
```python
import torch
import matplotlib.pyplot as plt
# 假设 loss 存储在一个列表中
losses = [0.1, 0.08, 0.06, 0.04, 0.02, 0.01]
# 画出 loss 曲线
plt.plot(losses)
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Loss Curve')
plt.show()
```
上述代码可以让您快速画出 loss 曲线,以便更直观地了解训练的效果。希望对您有所帮助。
相关问题
Transformers做cifar-10图像分类任务pytorch版本效果好且画出loss变化曲线以及准确率变化曲线的代码以及测试代码
以下是使用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变化曲线和准确率变化曲线。最后,我们在测试集上测试了模型的准确率。
pytorch训练cifar10并输出 ROC曲线的python代码
以下是使用PyTorch训练cifar10并输出ROC曲线的Python代码示例:
```python
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
# 定义数据增强和预处理
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))
])
# 加载cifar10数据集
trainset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=True, num_workers=2)
testset = datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=128, shuffle=False, num_workers=2)
# 构建卷积神经网络模型
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 3, padding=1)
self.bn1 = nn.BatchNorm2d(32)
self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.conv3 = nn.Conv2d(64, 128, 3, padding=1)
self.bn3 = nn.BatchNorm2d(128)
self.fc1 = nn.Linear(4 * 4 * 128, 512)
self.bn4 = nn.BatchNorm1d(512)
self.fc2 = nn.Linear(512, 10)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = nn.functional.relu(x)
x = nn.functional.max_pool2d(x, 2)
x = self.conv2(x)
x = self.bn2(x)
x = nn.functional.relu(x)
x = nn.functional.max_pool2d(x, 2)
x = self.conv3(x)
x = self.bn3(x)
x = nn.functional.relu(x)
x = x.view(-1, 4 * 4 * 128)
x = self.fc1(x)
x = self.bn4(x)
x = nn.functional.relu(x)
x = self.fc2(x)
return x
# 实例化模型、损失函数和优化器
net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.1, momentum=0.9, weight_decay=5e-4)
# 训练模型
for epoch in range(10):
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
inputs, labels = data
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print('[Epoch %d] loss: %.3f' % (epoch + 1, running_loss / len(trainloader)))
# 使用测试集评估模型
correct = 0
total = 0
pred_prob = []
true_labels = []
with torch.no_grad():
for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
pred_prob.append(outputs.numpy())
true_labels.append(labels.numpy())
test_acc = 100 * correct / total
pred_prob = torch.cat(pred_prob, 0)
true_labels = torch.cat(true_labels, 0)
# 计算ROC曲线和AUC值
fpr, tpr, thresholds = roc_curve(true_labels, pred_prob[:, 1])
roc_auc = auc(fpr, tpr)
# 绘制ROC曲线
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc="lower right")
plt.show()
```
需要安装的Python库有:PyTorch、scikit-learn和matplotlib。代码中的卷积神经网络模型和训练参数可以根据需求进行修改。
阅读全文