为什么我的损失函数仅返回NAN值?
以下是我
import numpy as np
import torch
from torch.utils import data
import torch.nn as nn
import pandas as pd
# PREPPING DATA FROM CSV FILE
csvFile = pd.read_csv('/Users/ericbeep999/Desktop/Web Development/Projects/Python/pytorch/3. Linear Regression/weather.csv')
labels, features = csvFile.iloc[:, 4], csvFile.iloc[:, 5]
#labels - min temp
#features - max temp
labels = torch.tensor(labels, dtype=torch.float32).reshape(-1, 1)
features = torch.tensor(features, dtype=torch.float32).reshape(-1,1)
# READING DATASET
def load_array(data_arrays, batch_size, is_train = True):
dataset = data.TensorDataset(*data_arrays)
return data.DataLoader(dataset, batch_size, shuffle= is_train)
batch_size = 20
data_set = load_array((features, labels), batch_size)
#DEFININING MODEL AND PARAMETERS
model = nn.Sequential(nn.Linear(1, 1))
model[0].weight.data.normal_(0, 0.1)
model[0].bias.data.fill_(0)
#DEFINING LOSS FUNCTION AND OPTIMIZATION ALGORITHMN
lossFunc = nn.MSELoss()
learning_rate = 0.01
gradient = torch.optim.SGD(model.parameters(), learning_rate)
#TRAINING MODEL
num_epochs = 100
for epoch in range(num_epochs):
for X, Y in data_set:
loss = lossFunc(model(X), Y)
gradient.zero_grad()
loss.backward()
gradient.step()
loss = lossFunc(model(features), labels)
print(f'epoch: {epoch + 1}, loss: {loss}')
print(f"{model[0].weight.data}, {model[0].bias.data}")
正在导入数据的CSV文件,可以在 https://www.kaggle.com/datasets/smid80/weatherww2?datasetid=3759&searchquery=pytorch
我的标签是最小温度,
每当我运行代码时,我的功能是最大的特征,唯一印刷的是
epoch: 1, loss: nan
epoch: 2, loss: nan
epoch: 3, loss: nan
epoch: 4, loss: nan
epoch: 5, loss: nan
epoch: 6, loss: nan
epoch: 7, loss: nan
epoch: 8, loss: nan
epoch: 9, loss: nan
epoch: 10, loss: nan
我真的不明白为什么它只是打印nan
below is my code
import numpy as np
import torch
from torch.utils import data
import torch.nn as nn
import pandas as pd
# PREPPING DATA FROM CSV FILE
csvFile = pd.read_csv('/Users/ericbeep999/Desktop/Web Development/Projects/Python/pytorch/3. Linear Regression/weather.csv')
labels, features = csvFile.iloc[:, 4], csvFile.iloc[:, 5]
#labels - min temp
#features - max temp
labels = torch.tensor(labels, dtype=torch.float32).reshape(-1, 1)
features = torch.tensor(features, dtype=torch.float32).reshape(-1,1)
# READING DATASET
def load_array(data_arrays, batch_size, is_train = True):
dataset = data.TensorDataset(*data_arrays)
return data.DataLoader(dataset, batch_size, shuffle= is_train)
batch_size = 20
data_set = load_array((features, labels), batch_size)
#DEFININING MODEL AND PARAMETERS
model = nn.Sequential(nn.Linear(1, 1))
model[0].weight.data.normal_(0, 0.1)
model[0].bias.data.fill_(0)
#DEFINING LOSS FUNCTION AND OPTIMIZATION ALGORITHMN
lossFunc = nn.MSELoss()
learning_rate = 0.01
gradient = torch.optim.SGD(model.parameters(), learning_rate)
#TRAINING MODEL
num_epochs = 100
for epoch in range(num_epochs):
for X, Y in data_set:
loss = lossFunc(model(X), Y)
gradient.zero_grad()
loss.backward()
gradient.step()
loss = lossFunc(model(features), labels)
print(f'epoch: {epoch + 1}, loss: {loss}')
print(f"{model[0].weight.data}, {model[0].bias.data}")
the csv file I am importing the data from can be found at https://www.kaggle.com/datasets/smid80/weatherww2?datasetId=3759&searchQuery=pytorch
My labels are the min temperature and my features are the max temperature
whenever I run the code, the only thing that prints is
epoch: 1, loss: nan
epoch: 2, loss: nan
epoch: 3, loss: nan
epoch: 4, loss: nan
epoch: 5, loss: nan
epoch: 6, loss: nan
epoch: 7, loss: nan
epoch: 8, loss: nan
epoch: 9, loss: nan
epoch: 10, loss: nan
i don't really understand why it is only printing NaN
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将您的学习率更改为
0.001
,并且它在不给NAN的情况下运行(尽管没有学习任何东西,因为可以从最大温度中预测最小温度,因此可能不容易学习)。我的猜测是问题在于您的输入/输出数据的规模,即它们在0-40之类的范围内,这对于神经网络来说不是很好 - 这可能会导致学习变得更加不稳定。我建议您首先将输入/输出扩展到[0,1]
或[ - 1,1]
的范围内。我将指导您此博客有关实现这一目标的详细信息,他们还更详细地讨论了为什么扩展数据对于使用神经网络学习很重要。I changed your learning rate to
0.001
and it runs without giving NaNs (albeit not learning anything since predicting min temperature from max temperature in that data may not be easily learned). My guess is the issue is with the scale of your input/output data, i.e. they're in the range of something like 0-40 which isn't great for neural networks - it can cause the learning to go unstable more easily. I would suggest you first scale your inputs/outputs to be in a range of[0, 1]
or[-1, 1]
. I'll direct you to this blog for details on achieving that, they also discuss in more detail why scaling the data is important for learning with neural networks.