简体   繁体   English

为什么pytorch分类model不学习?

[英]Why the pytorch classification model not learning?

I have created a simple pytorch classification model with sample datasets generated using sklearns make_classification .我创建了一个简单的 pytorch 分类 model ,其中包含使用 sklearns make_classification生成的示例数据集。 Even after training for thousands of epochs the accuracy of the model hovers between 30 and 40 percentage.即使经过数千个 epoch 的训练,model 的准确率也徘徊在 30% 到 40% 之间。 During training itself the loss value is fluctuating very far and wide.在训练期间,损失值波动很大。 I am wondering why this model is not learning, whether its due to some logical error in the code.我想知道为什么这个 model 不学习,是否由于代码中的一些逻辑错误。

import torch
from torch.utils.data import Dataset, DataLoader
import torch.nn as nn
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

X,y = make_classification(n_features=15,n_classes=5,n_informative=4)

DEVICE = torch.device('cuda')
epochs = 5000

class CustomDataset(Dataset):
    def __init__(self,X,y):
        self.X =  torch.from_numpy(X)
        self.y = torch.from_numpy(y)
    def __len__(self):
        return len(self.X)
    def __getitem__(self, index):
        X = self.X[index]
        y = self.y[index]
        return (X,y)

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.l1 = nn.Linear(15,10)
        self.l2 = nn.Linear(10,5)
        self.relu = nn.ReLU()
    def forward(self,x):
        x = self.l1(x)
        x = self.relu(x)
        x = self.l2(x)
        x = self.relu(x)
        return x

model = Model().double().to(DEVICE)

optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
loss_function = nn.CrossEntropyLoss()

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
train_data = CustomDataset(X_train,y_train)
test_data = CustomDataset(X_test,y_test)

trainloader = DataLoader(train_data, batch_size=32, shuffle=True)
testloader = DataLoader(test_data, batch_size=32, shuffle=True)

for i in range(epochs):
    for (x,y) in trainloader:
        x = x.to(DEVICE)
        y = y.to(DEVICE)
        optimizer.zero_grad()
        output = model(x)
        loss = loss_function(output,y)
        loss.backward()
        optimizer.step()
    if i%200==0:
        print("epoch: ",i," Loss: ",loss.item())

correct = 0
total = 0
# since we're not training, we don't need to calculate the gradients for our outputs
with torch.no_grad():
    for x, y in testloader:
        
        # calculate outputs by running x through the network
        outputs = model(x.to(DEVICE)).to(DEVICE)
        # the class with the highest energy is what we choose as prediction
        _, predicted = torch.max(outputs.data, 1)
        total += y.size(0)
        correct += (predicted == y.to(DEVICE)).sum().item()

    print(f'Accuracy of the network on the test data: {100 * correct // total} %')

You should not be using ReLU activation on your output layer.您不应该在 output 层上使用 ReLU 激活。 Usually softmax activation is used for multi class classification on the final layer, or the logits are fed to the loss function directly without explicitly adding a softmax activation layer.通常 softmax 激活用于最后一层的多 class 分类,或者直接将 logits 馈送到损失 function 而不显式添加 softmax 激活层。

Try removing the ReLU activation from the final layer.尝试从最后一层移除 ReLU 激活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pytorch 二进制分类RNN Model 不学习 - Pytorch Binary Classification RNN Model not Learning 为什么我的用于二进制分类的 CNN model 没有学习? - Why isn't my CNN model for a Binary Classification not learning? 为什么我的二进制分类 model 不学习,甚至过度拟合? - Why is my binary classification model not learning, even to overfit? Pytorch RNN model 什么都没学到 - Pytorch RNN model not learning anything 关于环境声音分类的分类方法和机器学习模型的建议 - Suggestion on Classification Method and Machine Learning Model for Environment Sound Classification 我应该使用哪种分类模型来进行机器学习中的作者归因? - Which classification model should I use for author attribution in machine learning? 减少分类机器学习中特征的最佳方法 model - best way to whittle down features in a classification machine learning model 如何在您的分类机器学习 model 中增加真阳性? - How to increase true positive in your classification Machine Learning model? 或者在 pytorch 中训练多任务学习模型 - 权重更新 - Alternatively train multi task learning model in pytorch - weight updating 我想实现一个机器学习或深度学习 model 用于文本分类(100类) - I want to implement a machine learning or deep learning model for text classification (100 classes)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM