使用Pytorch回归的神经网络
我正在尝试实施一个神经网络,以预测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.
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您执行回归,因此
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 theNLLLoss()
function. TheCrossEntropyLoss()
expectsC
classes forC
predictions but you have specified only one class. TheNLLLoss()
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()
withloss_function = MSELoss()
您的响应变量H1_hemoglobin看起来像是连续的响应变量。如果是这种情况,请更改
y_train
和y_test
的火炬张量类型,从longtensor
floattensor
或DoubleTensor
。根据Pytorch文档,
Crossentropyloss
对于许多类别的分类问题很有用。尝试将您的lose_function
从Crossentropyloss
更改为更适合您的连续响应变量H1_HEMOGLOBIN
。就您而言,以下可能会这样做。
Your response variable h1_hemoglobin looks like continous response variable. If that's the case please change the Torch Tensor Type for
y_train
andy_test
fromLongTensor
toFloatTensor
orDoubleTensor
.According to the Pytorch docs,
CrossEntropyLoss
is useful for classification problems with a number of classes. Try to change yourloss_function
fromCrossEntropyLoss
to a more suitable one for your continuous response variableh1_hemoglobin
.In your case, the following might do it.
Pytorch MSELoss
Pytorch CrossEntropyLoss