이 글에서는 훈련과 검증 중에 발생할 수 있는 상황을 요약하고 이 차트가 우리에게 어떤 정보를 제공할 수 있는지 소개하겠습니다.
몇 가지 간단한 코드부터 시작해 보겠습니다. 다음 코드는 기본 학습 프로세스 프레임워크를 설정합니다.
from sklearn.model_selection import train_test_split<br>from sklearn.datasets import make_classification<br>import torch<br>from torch.utils.data import Dataset, DataLoader<br>import torch.optim as torch_optim<br>import torch.nn as nn<br>import torch.nn.functional as F<br>import numpy as np<br>import matplotlib.pyplot as pltclass MyCustomDataset(Dataset):<br>def __init__(self, X, Y, scale=False):<br>self.X = torch.from_numpy(X.astype(np.float32))<br>self.y = torch.from_numpy(Y.astype(np.int64))<br><br>def __len__(self):<br>return len(self.y)<br><br>def __getitem__(self, idx):<br>return self.X[idx], self.y[idx]def get_optimizer(model, lr=0.001, wd=0.0):<br>parameters = filter(lambda p: p.requires_grad, model.parameters())<br>optim = torch_optim.Adam(parameters, lr=lr, weight_decay=wd)<br>return optimdef train_model(model, optim, train_dl, loss_func):<br># Ensure the model is in Training mode<br>model.train()<br>total = 0<br>sum_loss = 0<br>for x, y in train_dl:<br>batch = y.shape[0]<br># Train the model for this batch worth of data<br>logits = model(x)<br># Run the loss function. We will decide what this will be when we call our Training Loop<br>loss = loss_func(logits, y)<br># The next 3 lines do all the PyTorch back propagation goodness<br>optim.zero_grad()<br>loss.backward()<br>optim.step()<br># Keep a running check of our total number of samples in this epoch<br>total += batch<br># And keep a running total of our loss<br>sum_loss += batch*(loss.item())<br>return sum_loss/total<br>def train_loop(model, train_dl, valid_dl, epochs, loss_func, lr=0.1, wd=0):<br>optim = get_optimizer(model, lr=lr, wd=wd)<br>train_loss_list = []<br>val_loss_list = []<br>acc_list = []<br>for i in range(epochs): <br>loss = train_model(model, optim, train_dl, loss_func)<br># After training this epoch, keep a list of progress of <br># the loss of each epoch <br>train_loss_list.append(loss)<br>val, acc = val_loss(model, valid_dl, loss_func)<br># Likewise for the validation loss and accuracy<br>val_loss_list.append(val)<br>acc_list.append(acc)<br>print("training loss: %.5f valid loss: %.5f accuracy: %.5f" % (loss, val, acc))<br><br>return train_loss_list, val_loss_list, acc_list<br>def val_loss(model, valid_dl, loss_func):<br># Put the model into evaluation mode, not training mode<br>model.eval()<br>total = 0<br>sum_loss = 0<br>correct = 0<br>batch_count = 0<br>for x, y in valid_dl:<br>batch_count += 1<br>current_batch_size = y.shape[0]<br>logits = model(x)<br>loss = loss_func(logits, y)<br>sum_loss += current_batch_size*(loss.item())<br>total += current_batch_size<br># All of the code above is the same, in essence, to<br># Training, so see the comments there<br># Find out which of the returned predictions is the loudest<br># of them all, and that's our prediction(s)<br>preds = logits.sigmoid().argmax(1)<br># See if our predictions are right<br>correct += (preds == y).float().mean().item()<br>return sum_loss/total, correct/batch_count<br>def view_results(train_loss_list, val_loss_list, acc_list):<br>plt.rcParams["figure.figsize"] = (15, 5)<br>plt.figure()<br>epochs = np.arange(0, len(train_loss_list)) plt.subplot(1, 2, 1)<br>plt.plot(epochs-0.5, train_loss_list)<br>plt.plot(epochs, val_loss_list)<br>plt.title('model loss')<br>plt.ylabel('loss')<br>plt.xlabel('epoch')<br>plt.legend(['train', 'val', 'acc'], loc = 'upper left')<br><br>plt.subplot(1, 2, 2)<br>plt.plot(acc_list)<br>plt.title('accuracy')<br>plt.ylabel('accuracy')<br>plt.xlabel('epoch')<br>plt.legend(['train', 'val', 'acc'], loc = 'upper left')<br>plt.show()<br><br>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):<br># We'll make a fictitious dataset, assuming all relevant<br># EDA / Feature Engineering has been done and this is our <br># resultant data<br>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)<br><br>if break_it: # Specifically mess up the data<br>X = np.random.rand(n_samples,n_features)<br>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)<br>valid_ds = MyCustomDataset(X_val, y_val)<br>train_dl = DataLoader(train_ds, batch_size=batch_size, shuffle=True)<br>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)<br>view_results(train_loss_list, val_loss_list, acc_list)
위 코드는 매우 간단합니다. 데이터 획득, 교육 및 검증의 기본 프로세스입니다.
시나리오 1 - 모델이 학습하는 것처럼 보이지만 검증이나 정확도가 좋지 않습니다.
모델 Train loss는 하이퍼파라미터에 관계없이 천천히 감소하지만 Val loss는 감소하지 않으며 Accuracy는 학습 중임을 나타내지 않습니다. .
예를 들어 이 경우 이진 분류의 정확도는 약 50% 정도입니다.
class Scenario_1_Model_1(nn.Module):<br>def __init__(self, in_features=30, out_features=2):<br>super().__init__()<br>self.lin1 = nn.Linear(in_features, out_features)<br>def forward(self, x):<br>x = self.lin1(x)<br>return x<br>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):<br>def __init__(self, in_features=30, out_features=2):<br>super().__init__()<br>self.lin1 = nn.Linear(in_features, out_features)<br>def forward(self, x):<br>x = self.lin1(x)<br>return x<br>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 - 훈련 손실이 0에 가깝고 정확도가 좋아 보이지만 검증도 떨어지지 않고 올라갑니다
class Scenario_3_Model_1(nn.Module):<br>def __init__(self, in_features=30, out_features=2):<br>super().__init__()<br>self.lin1 = nn.Linear(in_features, 50)<br>self.lin2 = nn.Linear(50, 150)<br>self.lin3 = nn.Linear(150, 50)<br>self.lin4 = nn.Linear(50, out_features)<br>def forward(self, x):<br>x = F.relu(self.lin1(x))<br>x = F.relu(self.lin2(x))<br>x = F.relu(self.lin3(x))<br>x = self.lin4(x)<br>return x<br>get_data_train_and_show(Scenario_3_Model_1(), lr=0.001)
이것은 확실히 과적합입니다. 훈련 손실이 낮습니다. 정확도가 높고 검증 손실과 훈련 손실이 점점 커지고 있는데 이는 전형적인 과적합 지표입니다.
기본적으로 모델 학습 능력이 너무 강해요. 훈련 데이터를 너무 잘 기억하므로 새 데이터로 일반화할 수도 없습니다.
가장 먼저 시도할 수 있는 것은 모델의 복잡성을 줄이는 것입니다.
class Scenario_3_Model_2(nn.Module):<br>def __init__(self, in_features=30, out_features=2):<br>super().__init__()<br>self.lin1 = nn.Linear(in_features, 50)<br>self.lin2 = nn.Linear(50, out_features)<br>def forward(self, x):<br>x = F.relu(self.lin1(x))<br>x = self.lin2(x)<br>return x<br>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):<br>def __init__(self, in_features=30, out_features=2):<br>super().__init__()<br>self.lin1 = nn.Linear(in_features, 50)<br>self.lin2 = nn.Linear(50, 150)<br>self.lin3 = nn.Linear(150, 50)<br>self.lin4 = nn.Linear(50, out_features)<br>self.drops = nn.Dropout(0.4)<br>def forward(self, x):<br>x = F.relu(self.lin1(x))<br>x = self.drops(x)<br>x = F.relu(self.lin2(x))<br>x = self.drops(x)<br>x = F.relu(self.lin3(x))<br>x = self.drops(x)<br>x = self.lin4(x)<br>return x<br>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):<br>def __init__(self, in_features=30, out_features=2):<br>super().__init__()<br>self.lin1 = nn.Linear(in_features, 2)<br>self.lin2 = nn.Linear(2, out_features)<br>def forward(self, x):<br>x = F.relu(self.lin1(x))<br>x = self.lin2(x)<br>return x<br>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):<br>def __init__(self, in_features=30, out_features=2):<br>super().__init__()<br>self.lin1 = nn.Linear(in_features, 50)<br>self.lin2 = nn.Linear(50, out_features)<br>def forward(self, x):<br>x = F.relu(self.lin1(x))<br>x = self.lin2(x)<br>return x<br>get_data_train_and_show(Scenario_4_Model_2(out_features=5), lr=0.001, n_classes=5)
总结
以上就是一些常见的训练、验证时的曲线的示例,希望你在遇到相同情况时可以快速定位并且改进。
위 내용은 훈련 및 검증 측정항목 그래프는 머신러닝에서 무엇을 알려줄 수 있나요?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

ON-DEVICE AI의 힘을 활용 : 개인 챗봇 CLI 구축 최근에 개인 AI 조수의 개념은 공상 과학처럼 보였다. 기술 애호가 인 Alex, 똑똑하고 현지 AI 동반자를 꿈꾸는 것을 상상해보십시오.

AI4MH의 첫 출시는 2025 년 4 월 15 일에 열렸으며, 유명한 정신과 의사이자 신경 과학자 인 Luminary Dr. Tom Insel 박사는 킥오프 스피커 역할을했습니다. Insel 박사는 정신 건강 연구 및 테크노에서 뛰어난 작업으로 유명합니다.

Engelbert는 "WNBA가 모든 사람, 플레이어, 팬 및 기업 파트너가 안전하고 가치가 있으며 권한을 부여받는 공간으로 남아 있기를 원합니다. 아노

소개 Python은 특히 데이터 과학 및 생성 AI에서 프로그래밍 언어로 탁월합니다. 대규모 데이터 세트를 처리 할 때 효율적인 데이터 조작 (저장, 관리 및 액세스)이 중요합니다. 우리는 이전에 숫자와 st를 다루었습니다

다이빙하기 전에 중요한 경고 : AI 성능은 비 결정적이며 고도로 사용하는 것이 중요합니다. 간단히 말하면 마일리지는 다를 수 있습니다. 이 기사 (또는 다른) 기사를 최종 단어로 취하지 마십시오. 대신 에이 모델을 자신의 시나리오에서 테스트하십시오.

뛰어난 AI/ML 포트폴리오 구축 : 초보자 및 전문가를위한 안내서 인공 지능 (AI) 및 머신 러닝 (ML)의 역할을 확보하는 데 강력한 포트폴리오를 만드는 것이 중요합니다. 이 안내서는 포트폴리오 구축에 대한 조언을 제공합니다

결과? 소진, 비 효율성 및 탐지와 동작 사이의 넓은 차이. 이 중 어느 것도 사이버 보안에서 일하는 사람에게는 충격이되지 않습니다. 그러나 에이전트 AI의 약속은 잠재적 인 전환점으로 부상했다. 이 새로운 수업

장기 파트너십 대 즉각적인 영향? 2 주 전 Openai는 2025 년 5 월 말까지 미국과 캐나다 대학생들에게 Chatgpt Plus에 무료로 이용할 수있는 강력한 단기 제안으로 발전했습니다.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

SublimeText3 영어 버전
권장 사항: Win 버전, 코드 프롬프트 지원!

Dreamweaver Mac版
시각적 웹 개발 도구

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

에디트플러스 중국어 크랙 버전
작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음
