使用Pytorch回归的神经网络

发布于 2025-02-13 09:24:10 字数 1683 浏览 4 评论 0原文

我正在尝试实施一个神经网络,以预测pytorch中的H1_hemoglobin。创建模型后,我将1放在输出层中,因为这是回归。但是我得到了下面的错误。我无法理解这个错误。在输出层中保持大量100的价值会删除错误,但在我试图实现回归时使模型无用。

数据:

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0)
##### Creating Tensors
X_train=torch.tensor(X_train)
X_test=torch.tensor(X_test)
y_train=torch.LongTensor(y_train)
y_test=torch.LongTensor(y_test)
class ANN_Model(nn.Module):
    def __init__(self,input_features=4,hidden1=20,hidden2=20,out_features=1):
        super().__init__()
        self.f_connected1=nn.Linear(input_features,hidden1)
        self.f_connected2=nn.Linear(hidden1,hidden2)
        self.out=nn.Linear(hidden2,out_features)
    def forward(self,x):
        x=F.relu(self.f_connected1(x))
        x=F.relu(self.f_connected2(x))
        x=self.out(x)
        return x

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

epochs = 500
final_losses = []
for i in range(epochs):
    i = i + 1
    y_pred = model.forward(X_train.float())
    loss=loss_function(y_pred, y_train)
    final_losses.append(loss.item())
    if i%10==1:
        print("Epoch number: {} and the loss: {}".format(i, loss.item()))
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

错误:

I am trying to implement a Neural Network for predicting the h1_hemoglobin in PyTorch. After creating a model, I kept 1 in the output layer as this is Regression. But I got the error as below. I'm not able to understand the mistake. Keeping a large value like 100 in the output layer removes the error but renders the model useless as I am trying to implement regression.

Data:
Data

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0)
##### Creating Tensors
X_train=torch.tensor(X_train)
X_test=torch.tensor(X_test)
y_train=torch.LongTensor(y_train)
y_test=torch.LongTensor(y_test)
class ANN_Model(nn.Module):
    def __init__(self,input_features=4,hidden1=20,hidden2=20,out_features=1):
        super().__init__()
        self.f_connected1=nn.Linear(input_features,hidden1)
        self.f_connected2=nn.Linear(hidden1,hidden2)
        self.out=nn.Linear(hidden2,out_features)
    def forward(self,x):
        x=F.relu(self.f_connected1(x))
        x=F.relu(self.f_connected2(x))
        x=self.out(x)
        return x

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

epochs = 500
final_losses = []
for i in range(epochs):
    i = i + 1
    y_pred = model.forward(X_train.float())
    loss=loss_function(y_pred, y_train)
    final_losses.append(loss.item())
    if i%10==1:
        print("Epoch number: {} and the loss: {}".format(i, loss.item()))
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

Error:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

つ低調成傷 2025-02-20 09:24:10

由于您执行回归,因此crossentropyloss()内部实现nllloss()函数。 crossentropyloss()期望c c预测的类,但您仅指定了一个类。 nllloss()试图根据地面真实值索引到预测逻辑。例如,在您的情况下,地面真相是单个值14。损失步骤试图将预测的第14个logit索引以获取其相应的值,以便它可以计算出其上的负日志可能性,这实际上是 - 这是 - -log(probinality_k)其中k是基地真相输出的索引。由于您的预测中只有logit,因此它会出现错误 - index out bounds

对于回归问题,您应该考虑使用基于距离的损失,例如mseloss()

尝试替换损失函数 - lose_function = crossentropyloss() lose_function = mseloss()

Since you are performing regression, the CrossEntropyLoss() internally implements the NLLLoss() function. The CrossEntropyLoss() expects C classes for C predictions but you have specified only one class. The NLLLoss() tries to index into the prediction logits based on the ground-truth value. E.g., in your case, the ground-truth is a single value 14. The loss step tries to index into the 14th logit of your predictions to get its corresponding value so that it can compute the negative log likelihood on it, which is essentially - -log(probability_k) where k is the index that the ground-truth outputs. Since you have only logit in your predictions, it throws an error - index out of bounds.

For regression problems, you should consider using distance based losses such as MSELoss().

Try replacing your loss function - loss_function = CrossEntropyLoss() with loss_function = MSELoss()

够运 2025-02-20 09:24:10

您的响应变量H1_hemoglobin看起来像是连续的响应变量。如果是这种情况,请更改y_trainy_test的火炬张量类型,从longtensor floattensorDoubleTensor

根据Pytorch文档,Crossentropyloss对于许多类别的分类问题很有用。尝试将您的lose_functionCrossentropyloss更改为更适合您的连续响应变量H1_HEMOGLOBIN

就您而言,以下可能会这样做。

y_train=torch.DoubleTensor(y_train)
y_test=torch.DoubleTensor(y_test)
...
...
loss_function = nn.MSELoss()

Your response variable h1_hemoglobin looks like continous response variable. If that's the case please change the Torch Tensor Type for y_train and y_test from LongTensor to FloatTensor or DoubleTensor.

According to the Pytorch docs, CrossEntropyLoss is useful for classification problems with a number of classes. Try to change your loss_function from CrossEntropyLoss to a more suitable one for your continuous response variable h1_hemoglobin.

In your case, the following might do it.

y_train=torch.DoubleTensor(y_train)
y_test=torch.DoubleTensor(y_test)
...
...
loss_function = nn.MSELoss()

Pytorch MSELoss

Pytorch CrossEntropyLoss

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文