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

wmomyfyw  于 2024-01-08  发布在  其他
关注(0)|答案(1)|浏览(165)

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

  1. import torch
  2. import torch.nn as nn
  3. from residual_block import ResidualBlock
  4. import pytorch_lightning as pl
  5. from torchmetrics import Accuracy
  6. class ResNet(pl.LightningModule):
  7. def __init__(self, block, layers, image_channels, num_classes, learning_rate):
  8. super(ResNet, self).__init__()
  9. self.in_channels = 64
  10. self.conv1 = nn.Conv2d(
  11. image_channels, 64, kernel_size=7, stride=2, padding=3, bias=False)
  12. self.bn1 = nn.BatchNorm2d(64)
  13. self.relu = nn.ReLU()
  14. self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  15. self.layer1 = self._make_layer(
  16. block, layers[0], intermediate_channels=64, stride=1)
  17. self.layer2 = self._make_layer(
  18. block, layers[1], intermediate_channels=128, stride=2)
  19. self.layer3 = self._make_layer(
  20. block, layers[2], intermediate_channels=256, stride=2)
  21. self.layer4 = self._make_layer(
  22. block, layers[3], intermediate_channels=512, stride=2)
  23. self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
  24. self.fc = nn.Linear(512 * 4, num_classes)
  25. self.learning_rate = learning_rate
  26. self.train_accuracy = Accuracy()
  27. self.val_accuracy = Accuracy()
  28. self.test_accuracy = Accuracy()
  29. def forward(self, x):
  30. x = self.conv1(x)
  31. x = self.bn1(x)
  32. x = self.relu(x)
  33. x = self.maxpool(x)
  34. x = self.layer1(x)
  35. x = self.layer2(x)
  36. x = self.layer3(x)
  37. x = self.layer4(x)
  38. x = self.avgpool(x)
  39. x = x.reshape(x.shape[0], -1)
  40. x = self.fc(x)
  41. return x
  42. def configure_optimizers(self):
  43. optimizer = torch.optim.Adam(self.parameters(), lr=self.learning_rate)
  44. return optimizer
  45. def training_step(self, train_batch, batch_idx):
  46. images, labels = train_batch
  47. outputs = self(images)
  48. criterion = nn.CrossEntropyLoss()
  49. loss = criterion(outputs, labels)
  50. self.train_accuracy(outputs, labels)
  51. self.log('train_loss', loss)
  52. self.log('train_accuracy', self.train_accuracy)
  53. return loss
  54. def validation_step(self, val_batch, batch_idx):
  55. images, labels = val_batch
  56. outputs = self(images)
  57. criterion = nn.CrossEntropyLoss()
  58. loss = criterion(outputs, labels)
  59. self.val_accuracy(outputs, labels)
  60. self.log('val_loss', loss)
  61. self.log('val_accuracy', self.val_accuracy)
  62. def test_step(self, test_batch, batch_idx):
  63. images, labels = test_batch
  64. outputs = self(images)
  65. criterion = nn.CrossEntropyLoss()
  66. loss = criterion(outputs, labels)
  67. self.test_accuracy(outputs, labels)
  68. self.log('test_loss', loss)
  69. self.log('test_accuracy', self.test_accuracy)
  70. def _make_layer(self, block, num_residual_blocks, intermediate_channels, stride):
  71. identity_downsample = None
  72. layers = []
  73. if stride != 1 or self.in_channels != intermediate_channels * 4:
  74. identity_downsample = nn.Sequential(nn.Conv2d(self.in_channels, intermediate_channels * 4,
  75. kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(intermediate_channels * 4),)
  76. layers.append(
  77. block(self.in_channels, intermediate_channels, identity_downsample, stride))
  78. self.in_channels = intermediate_channels * 4
  79. for i in range(num_residual_blocks - 1):
  80. layers.append(block(self.in_channels, intermediate_channels))
  81. return nn.Sequential(*layers)
  82. @classmethod
  83. def ResNet50(cls, img_channels, num_classes, learning_rate):
  84. return ResNet(ResidualBlock, [3, 4, 6, 3], img_channels, num_classes, learning_rate)
  85. @classmethod
  86. def ResNet101(cls, img_channels, num_classes, learning_rate):
  87. return ResNet(ResidualBlock, [3, 4, 23, 3], img_channels, num_classes, learning_rate)
  88. @classmethod
  89. def ResNet152(cls, img_channels, num_classes, learning_rate):
  90. 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
或者你可以在一个设备上使用这个:

  1. def training_step(self, batch, batch_idx):
  2. ...
  3. loss = nn.functional.mse_loss(x_hat, x)
  4. return loss
  5. def training_epoch_end(self, outputs) -> None:
  6. loss = sum(output['loss'] for output in outputs) / len(outputs)
  7. print(loss)

字符串
多个GPU:

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


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

展开查看全部

相关问题