機械学習において、トレーニングと検証のメトリクス グラフから何がわかるでしょうか?

WBOY
リリース: 2023-04-08 21:31:07
転載
1302 人が閲覧しました

この記事では、トレーニングと検証で考えられる状況をまとめ、これらのチャートがどのような情報を提供できるかを紹介します。

機械学習において、トレーニングと検証のメトリクス グラフから何がわかるでしょうか?

簡単なコードから始めましょう。次のコードは、基本的なトレーニング プロセスのフレームワークを確立します。

from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
import torch
from torch.utils.data import Dataset, DataLoader
import torch.optim as torch_optim
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as pltclass MyCustomDataset(Dataset):
def __init__(self, X, Y, scale=False):
self.X = torch.from_numpy(X.astype(np.float32))
self.y = torch.from_numpy(Y.astype(np.int64))

def __len__(self):
return len(self.y)

def __getitem__(self, idx):
return self.X[idx], self.y[idx]def get_optimizer(model, lr=0.001, wd=0.0):
parameters = filter(lambda p: p.requires_grad, model.parameters())
optim = torch_optim.Adam(parameters, lr=lr, weight_decay=wd)
return optimdef train_model(model, optim, train_dl, loss_func):
# Ensure the model is in Training mode
model.train()
total = 0
sum_loss = 0
for x, y in train_dl:
batch = y.shape[0]
# Train the model for this batch worth of data
logits = model(x)
# Run the loss function. We will decide what this will be when we call our Training Loop
loss = loss_func(logits, y)
# The next 3 lines do all the PyTorch back propagation goodness
optim.zero_grad()
loss.backward()
optim.step()
# Keep a running check of our total number of samples in this epoch
total += batch
# And keep a running total of our loss
sum_loss += batch*(loss.item())
return sum_loss/total
def train_loop(model, train_dl, valid_dl, epochs, loss_func, lr=0.1, wd=0):
optim = get_optimizer(model, lr=lr, wd=wd)
train_loss_list = []
val_loss_list = []
acc_list = []
for i in range(epochs):
loss = train_model(model, optim, train_dl, loss_func)
# After training this epoch, keep a list of progress of
# the loss of each epoch
train_loss_list.append(loss)
val, acc = val_loss(model, valid_dl, loss_func)
# Likewise for the validation loss and accuracy
val_loss_list.append(val)
acc_list.append(acc)
print("training loss: %.5f valid loss: %.5f accuracy: %.5f" % (loss, val, acc))

return train_loss_list, val_loss_list, acc_list
def val_loss(model, valid_dl, loss_func):
# Put the model into evaluation mode, not training mode
model.eval()
total = 0
sum_loss = 0
correct = 0
batch_count = 0
for x, y in valid_dl:
batch_count += 1
current_batch_size = y.shape[0]
logits = model(x)
loss = loss_func(logits, y)
sum_loss += current_batch_size*(loss.item())
total += current_batch_size
# All of the code above is the same, in essence, to
# Training, so see the comments there
# Find out which of the returned predictions is the loudest
# of them all, and that's our prediction(s)
preds = logits.sigmoid().argmax(1)
# See if our predictions are right
correct += (preds == y).float().mean().item()
return sum_loss/total, correct/batch_count
def view_results(train_loss_list, val_loss_list, acc_list):
plt.rcParams["figure.figsize"] = (15, 5)
plt.figure()
epochs = np.arange(0, len(train_loss_list)) plt.subplot(1, 2, 1)
plt.plot(epochs-0.5, train_loss_list)
plt.plot(epochs, val_loss_list)
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val', 'acc'], loc = 'upper left')

plt.subplot(1, 2, 2)
plt.plot(acc_list)
plt.title('accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val', 'acc'], loc = 'upper left')
plt.show()

def get_data_train_and_show(model, batch_size=128, n_samples=10000, n_classes=2, n_features=30, val_size=0.2, epochs=20, lr=0.1, wd=0, break_it=False):
# We'll make a fictitious dataset, assuming all relevant
# EDA / Feature Engineering has been done and this is our
# resultant data
X, y = make_classification(n_samples=n_samples, n_classes=n_classes, n_features=n_features, n_informative=n_features, n_redundant=0, random_state=1972)

if break_it: # Specifically mess up the data
X = np.random.rand(n_samples,n_features)
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=val_size, random_state=1972) train_ds = MyCustomDataset(X_train, y_train)
valid_ds = MyCustomDataset(X_val, y_val)
train_dl = DataLoader(train_ds, batch_size=batch_size, shuffle=True)
valid_dl = DataLoader(valid_ds, batch_size=batch_size, shuffle=True) train_loss_list, val_loss_list, acc_list = train_loop(model, train_dl, valid_dl, epochs=epochs, loss_func=F.cross_entropy, lr=lr, wd=wd)
view_results(train_loss_list, val_loss_list, acc_list)
ログイン後にコピー

上記のコードは非常にシンプルで、データの取得、トレーニング、検証の基本的なプロセスです。

シナリオ 1 - モデルは学習しているように見えますが、検証や精度のパフォーマンスが不十分です

ハイパーパラメーターに関係なく、モデルの Train 損失はゆっくりと減少しますが、Val 損失は減少せず、その精度は示されませんそれは何かを学ぶことだということ。

たとえば、この場合、バイナリ分類の精度は約 50% にとどまります。

class Scenario_1_Model_1(nn.Module):
def __init__(self, in_features=30, out_features=2):
super().__init__()
self.lin1 = nn.Linear(in_features, out_features)
def forward(self, x):
x = self.lin1(x)
return x
get_data_train_and_show(Scenario_1_Model_1(), lr=0.001, break_it=True)
ログイン後にコピー

機械学習において、トレーニングと検証のメトリクス グラフから何がわかるでしょうか?

データには「学習」を可能にするのに十分な情報がありません。また、トレーニング データにはモデルが「学習」を可能にするのに十分な情報が含まれていない可能性があります。

この場合 (コード内のトレーニング データはランダム データです)、実質的なものは何も学習できないことを意味します。

データには、学習するのに十分な情報が含まれている必要があります。 EDA と特徴エンジニアリングが鍵となります。モデルは、存在しないものを作り上げるのではなく、学習できることを学習します。

シナリオ 2 - トレーニング、検証、および精度曲線はすべて非常に不安定です

たとえば、次のコード: lr=0.1, bs=128

class Scenario_2_Model_1(nn.Module):
def __init__(self, in_features=30, out_features=2):
super().__init__()
self.lin1 = nn.Linear(in_features, out_features)
def forward(self, x):
x = self.lin1(x)
return x
get_data_train_and_show(Scenario_2_Model_1(), lr=0.1)
ログイン後にコピー

機械学習において、トレーニングと検証のメトリクス グラフから何がわかるでしょうか?

「学習率が高すぎる」または「バッチが小さすぎる」 学習率を 0.1 から 0.001 に下げてみることができます。これは、学習率が「跳ね返る」のではなく、滑らかに減少することを意味します。

get_data_train_and_show(Scenario_1_Model_1(), lr=0.001)
ログイン後にコピー

機械学習において、トレーニングと検証のメトリクス グラフから何がわかるでしょうか?

学習率を下げるだけでなく、バッチサイズを増やすと学習もスムーズになります。

get_data_train_and_show(Scenario_1_Model_1(), lr=0.001, batch_size=256)
ログイン後にコピー

機械学習において、トレーニングと検証のメトリクス グラフから何がわかるでしょうか?

シナリオ 3 - トレーニング損失はゼロに近く、精度は良好に見えますが、検証は低下せず、上昇もします

class Scenario_3_Model_1(nn.Module):
def __init__(self, in_features=30, out_features=2):
super().__init__()
self.lin1 = nn.Linear(in_features, 50)
self.lin2 = nn.Linear(50, 150)
self.lin3 = nn.Linear(150, 50)
self.lin4 = nn.Linear(50, out_features)
def forward(self, x):
x = F.relu(self.lin1(x))
x = F.relu(self.lin2(x))
x = F.relu(self.lin3(x))
x = self.lin4(x)
return x
get_data_train_and_show(Scenario_3_Model_1(), lr=0.001)
ログイン後にコピー

機械学習において、トレーニングと検証のメトリクス グラフから何がわかるでしょうか?

これは間違いなく過学習です。トレーニング損失は低く、精度は高いですが、検証損失とトレーニング損失はますます大きくなっており、どちらも古典的な過学習指標です。

根本的に言えば、モデルの学習能力が強すぎます。トレーニング データをよく覚えているため、新しいデータに一般化することもできません。

最初に試せるのは、モデルの複雑さを軽減することです。

class Scenario_3_Model_2(nn.Module):
def __init__(self, in_features=30, out_features=2):
super().__init__()
self.lin1 = nn.Linear(in_features, 50)
self.lin2 = nn.Linear(50, out_features)
def forward(self, x):
x = F.relu(self.lin1(x))
x = self.lin2(x)
return x
get_data_train_and_show(Scenario_3_Model_2(), lr=0.001)
ログイン後にコピー

機械学習において、トレーニングと検証のメトリクス グラフから何がわかるでしょうか?

これにより改善され、L2 重み減衰正則化を導入してさらに改善することができます (浅いモデルの場合)。

get_data_train_and_show(Scenario_3_Model_2(), lr=0.001, wd=0.02)
ログイン後にコピー

機械学習において、トレーニングと検証のメトリクス グラフから何がわかるでしょうか?

モデルの深さとサイズを維持したい場合は、ドロップアウト (より深いモデルの場合) を使用してみてください。

class Scenario_3_Model_3(nn.Module):
def __init__(self, in_features=30, out_features=2):
super().__init__()
self.lin1 = nn.Linear(in_features, 50)
self.lin2 = nn.Linear(50, 150)
self.lin3 = nn.Linear(150, 50)
self.lin4 = nn.Linear(50, out_features)
self.drops = nn.Dropout(0.4)
def forward(self, x):
x = F.relu(self.lin1(x))
x = self.drops(x)
x = F.relu(self.lin2(x))
x = self.drops(x)
x = F.relu(self.lin3(x))
x = self.drops(x)
x = self.lin4(x)
return x
get_data_train_and_show(Scenario_3_Model_3(), lr=0.001)
ログイン後にコピー

機械学習において、トレーニングと検証のメトリクス グラフから何がわかるでしょうか?

场景 4 - 训练和验证表现良好,但准确度没有提高

lr = 0.001,bs = 128(默认,分类类别= 5

class Scenario_4_Model_1(nn.Module):
def __init__(self, in_features=30, out_features=2):
super().__init__()
self.lin1 = nn.Linear(in_features, 2)
self.lin2 = nn.Linear(2, out_features)
def forward(self, x):
x = F.relu(self.lin1(x))
x = self.lin2(x)
return x
get_data_train_and_show(Scenario_4_Model_1(out_features=5), lr=0.001, n_classes=5)
ログイン後にコピー

機械学習において、トレーニングと検証のメトリクス グラフから何がわかるでしょうか?

没有足够的学习能力:模型中的其中一层的参数少于模型可能输出中的类。 在这种情况下,当有 5 个可能的输出类时,中间的参数只有 2 个。

这意味着模型会丢失信息,因为它不得不通过一个较小的层来填充它,因此一旦层的参数再次扩大,就很难恢复这些信息。

所以需要记录层的参数永远不要小于模型的输出大小。

class Scenario_4_Model_2(nn.Module):
def __init__(self, in_features=30, out_features=2):
super().__init__()
self.lin1 = nn.Linear(in_features, 50)
self.lin2 = nn.Linear(50, out_features)
def forward(self, x):
x = F.relu(self.lin1(x))
x = self.lin2(x)
return x
get_data_train_and_show(Scenario_4_Model_2(out_features=5), lr=0.001, n_classes=5)
ログイン後にコピー

機械学習において、トレーニングと検証のメトリクス グラフから何がわかるでしょうか?

总结

以上就是一些常见的训练、验证时的曲线的示例,希望你在遇到相同情况时可以快速定位并且改进。


以上が機械学習において、トレーニングと検証のメトリクス グラフから何がわかるでしょうか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:51cto.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!