keras Pytorch lightning 打印精度和每个时期结束时的损失

wmomyfyw  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(138)

在tensorflow keras中,当我训练一个模型时,在每个epoch它都会打印准确度和损失,我想用pythorch lightning做同样的事情。
我已经创建了我的模块,但我不知道如何做到这一点。

import torch
import torch.nn as nn
from residual_block import ResidualBlock
import pytorch_lightning as pl
from torchmetrics import Accuracy

class ResNet(pl.LightningModule):
    def __init__(self, block, layers, image_channels, num_classes, learning_rate):
        super(ResNet, self).__init__()
        self.in_channels = 64
        self.conv1 = nn.Conv2d(
            image_channels, 64, kernel_size=7, stride=2, padding=3, bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU()
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(
            block, layers[0], intermediate_channels=64, stride=1)
        self.layer2 = self._make_layer(
            block, layers[1], intermediate_channels=128, stride=2)
        self.layer3 = self._make_layer(
            block, layers[2], intermediate_channels=256, stride=2)
        self.layer4 = self._make_layer(
            block, layers[3], intermediate_channels=512, stride=2)
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = nn.Linear(512 * 4, num_classes)
        self.learning_rate = learning_rate
        self.train_accuracy = Accuracy()
        self.val_accuracy = Accuracy()
        self.test_accuracy = Accuracy()

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avgpool(x)
        x = x.reshape(x.shape[0], -1)
        x = self.fc(x)
        return x
    
    def configure_optimizers(self):
        optimizer = torch.optim.Adam(self.parameters(), lr=self.learning_rate)
        return optimizer
    
    def training_step(self, train_batch, batch_idx):
        images, labels = train_batch
        outputs = self(images)
        criterion = nn.CrossEntropyLoss()
        loss = criterion(outputs, labels)
        self.train_accuracy(outputs, labels)
        self.log('train_loss', loss)
        self.log('train_accuracy', self.train_accuracy)
        return loss
    
    def validation_step(self, val_batch, batch_idx):
        images, labels = val_batch
        outputs = self(images)
        criterion = nn.CrossEntropyLoss()
        loss = criterion(outputs, labels)
        self.val_accuracy(outputs, labels)
        self.log('val_loss', loss)
        self.log('val_accuracy', self.val_accuracy)
    
    def test_step(self, test_batch, batch_idx):
        images, labels = test_batch
        outputs = self(images)
        criterion = nn.CrossEntropyLoss()
        loss = criterion(outputs, labels)
        self.test_accuracy(outputs, labels)
        self.log('test_loss', loss)
        self.log('test_accuracy', self.test_accuracy)

    def _make_layer(self, block, num_residual_blocks, intermediate_channels, stride):
        identity_downsample = None
        layers = []
        if stride != 1 or self.in_channels != intermediate_channels * 4:
            identity_downsample = nn.Sequential(nn.Conv2d(self.in_channels, intermediate_channels * 4,
                                                kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(intermediate_channels * 4),)
        layers.append(
            block(self.in_channels, intermediate_channels, identity_downsample, stride))
        self.in_channels = intermediate_channels * 4
        for i in range(num_residual_blocks - 1):
            layers.append(block(self.in_channels, intermediate_channels))
        return nn.Sequential(*layers)

    @classmethod
    def ResNet50(cls, img_channels, num_classes, learning_rate):
        return ResNet(ResidualBlock, [3, 4, 6, 3], img_channels, num_classes, learning_rate)

    @classmethod
    def ResNet101(cls, img_channels, num_classes, learning_rate):
        return ResNet(ResidualBlock, [3, 4, 23, 3], img_channels, num_classes, learning_rate)

    @classmethod
    def ResNet152(cls, img_channels, num_classes, learning_rate):
        return ResNet(ResidualBlock, [3, 8, 36, 3], img_channels, num_classes, learning_rate)

字符串

我只想打印每个epoch结束时的训练和验证精度和损失。

jogvjijk

jogvjijk1#

第一个月
上面的代码将train_loss记录到进度条。
https://pytorch-lightning.readthedocs.io/en/stable/extensions/logging.html#automatic-logging
或者你可以在一个设备上使用这个:

def training_step(self, batch, batch_idx):
    ...
    loss = nn.functional.mse_loss(x_hat, x)
    return loss

def training_epoch_end(self, outputs) -> None:
    loss = sum(output['loss'] for output in outputs) / len(outputs)
    print(loss)

字符串
多个GPU:

def training_epoch_end(self, outputs) -> None:
    gathered = self.all_gather(outputs)
    if self.global_rank == 0:
        # print(gathered)
        loss = sum(output['loss'].mean() for output in gathered) / len(outputs)
        print(loss.item())


更新于2023年12月:我认为自从Lightning 2.0以来它不再工作了,我必须使用TorchMetric来自定义我的指标。

相关问题