損失関数の種類
問題例 | 損失関数 | 概要 |
---|---|---|
回帰 | nn.MSELoss | 平均二乗誤差 |
↓ | nn.L1Loss | 平均絶対値誤差 |
二値分類 | nn.BCELoss | バイナリ交差エントロピ |
↓ | nn.BCEWithLogitsLoss | ロジット・バイナリ交差エントロピ |
多クラス分類 | nn.CrossEntropyLoss | ソフトマックス交差エントロピ誤差 |
回帰 - nn.MSELoss - 平均二乗誤差損失関数の種類
回帰 - nn.L1Loss - 平均絶対値誤差
二値分類 - nn.BCELoss - バイナリ交差エントロピ
二値分類 - nn.BCEWithLogitsLoss - ロジット・バイナリ交差エントロピ
「ロジット」とは?
まず、確率( )に対し、 それが起こらない確率との比 ( )を「オッズ」と呼び、 更にこのlogをとったものをロジットと呼ぶ(以下)。
また、ロジットはロジスティック関数の逆関数でもある
多クラス分類 - nn.CrossEntropyLoss - ソフトマックス交差エントロピ
pytorchによる例:
#!/usr/local/python3/bin/python3 # -*- coding: utf-8 -*- import torch import torchvision import torch.nn as nn import torch.optim as optim import torch.nn.functional as F import torchvision.transforms as transforms import numpy as np from matplotlib import pyplot as plt def main(): # MSELoss x = torch.randn(4) y = torch.randn(4) print("x: ",x) print("y: ",y) criterion = nn.MSELoss() loss = criterion(x, y) print("MSELoss: ",loss) # L1Loss criterion = nn.L1Loss() loss = criterion(x, y) print("L1Loss: ",loss) # CrossEntropyLoss x = torch.randn(1, 4) y = torch.LongTensor([1]).random_(4) criterion = nn.CrossEntropyLoss() loss = criterion(x, y) print("CrossEntropyLoss: ",loss) if __name__ == '__main__': main()
↑こう書くと、↓こう表示されます
$ ./foo.py x: tensor([-1.3960, -0.1662, -0.3867, -1.3959]) y: tensor([ 0.3319, 0.7981, 0.5625, -0.0747]) MSELoss: tensor(1.6406) L1Loss: tensor(1.2407) CrossEntropyLoss: tensor(1.4561)