Python错误RuntimeError:标量类型应为Long,但找到Double
首先,一般来说,我对python/ML是相当陌生的。我试图在我自己的数据集上使用上描述的模型 一切都进展顺利,直到我准备好迎接新纪元 在调试过程中,我看到它在CrossEntropyLoss函数上失败,我得到了预期的错误long found double。它似乎失败的数据集是我计算的my tdiff列,但我似乎不知道如何将其转换为long 我在试图弄清楚这件事的过程中有没有遗漏什么 明确地说,我认为这是基于我对该功能极其有限的知识:Python错误RuntimeError:标量类型应为Long,但找到Double,python,numpy,pytorch,Python,Numpy,Pytorch,首先,一般来说,我对python/ML是相当陌生的。我试图在我自己的数据集上使用上描述的模型 一切都进展顺利,直到我准备好迎接新纪元 在调试过程中,我看到它在CrossEntropyLoss函数上失败,我得到了预期的错误long found double。它似乎失败的数据集是我计算的my tdiff列,但我似乎不知道如何将其转换为long 我在试图弄清楚这件事的过程中有没有遗漏什么 明确地说,我认为这是基于我对该功能极其有限的知识: import pandas as pd import sqli
import pandas as pd
import sqlite3 as sq
import numpy as np
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
import seaborn as sns
con = sq.connect("filepath")
df = pd.read_sql_query("SELECT b.actcod," +
"a.trnqty, " +
"b.dlytrn_id, " +
"a.usr_id, " +
" b.oprcod, " +
"b.usr_id as usrid," +
" b.tostol as frstol," +
" a.tostol, " +
" b.trndte as bdte," +
" a.trndte as adte," +
" '' as tdiff" +
" FROM" +
" (SELECT row_number() over(PARTITION by usr_id order by trndte) AS rown," +
" trndte," +
" dlytrn_id," +
" trnqty, " +
" oprcod," +
" actcod," +
" frstol," +
" tostol," +
" usr_id" +
" FROM Master_File" +
" WHERE actcod = 'PALPCK') a " +
" JOIN "+
" (SELECT row_number() over(PARTITION by usr_id order by trndte) AS rown," +
" trndte,dlytrn_id, trnqty, oprcod,actcod, frstol, tostol, usr_id" +
" FROM Master_File" +
" WHERE actcod = 'PALPCK' ) b" +
" ON b.rown = (a.rown -1)" +
" and b.usr_id = a.usr_id" #+
#" LIMIT 10"
,con)
# Create record Count
record_count = df['dlytrn_id'].count()
test_records = int(record_count * .2)
print(record_count)
print(test_records)
#Calculate time delta between scans
df['tdiff'] = pd.TimedeltaIndex.total_seconds(pd.to_datetime(df['adte'].values) - pd.to_datetime(df['bdte'].values))
# Establish column types
cat_column = ['usrid','actcod','tostol','frstol']
num_column = ['trnqty']
dte_column = ['bdte','adte']
op_column = ['tdiff']
#convert column datatypes
for category in cat_column:
df[category] = df[category].astype('category')
for category in num_column:
df[category] = df[category].astype('float64')
for category in dte_column:
df[category] = df[category].astype('datetime64')
#create categorical tensor
usr = df['usrid'].cat.codes.values
act = df['actcod'].cat.codes.values
to = df['tostol'].cat.codes.values
fr = df['frstol'].cat.codes.values
cat_data = np.stack([usr,act,to,fr], 1)
cat_data = torch.tensor(cat_data, dtype=torch.int64)
#Create Numerical Tensor
num_data = np.stack([df[col].values for col in num_column], 1)
num_data = torch.tensor(num_data, dtype=torch.float)
#convert output array
op_data = torch.tensor(df[op_column].values).flatten()
print(df.shape)
print(cat_data.shape)
print(num_data.shape)
print(op_data.shape)
#convert categorical data into an N-Dimensional vector,
#this will allow better ML analysis on relationships
cat_col_sizes = [len(df[column].cat.categories) for column in cat_column]
cat_embedding_sizes = [(col_size, min(50, (col_size+1)//2)) for col_size in cat_col_sizes]
print(cat_embedding_sizes)
#create test batches
cat_train_data = cat_data[:record_count-test_records]
cat_test_data = cat_data[record_count-test_records:record_count]
num_train_data = num_data[:record_count-test_records]
num_test_data = num_data[record_count-test_records:record_count]
train_op = op_data[:record_count-test_records]
test_op = op_data[record_count-test_records:record_count]
print(len(cat_train_data))
print(len(num_train_data))
print(len(train_op))
print(len(cat_test_data))
print(len(num_test_data))
print(len(test_op))
class Model(nn.Module):
def __init__(self, embedding_size, num_numerical_cols, output_size, layers, p=0.4):
super().__init__()
self.all_embeddings = nn.ModuleList([nn.Embedding(ni, nf) for ni, nf in embedding_size])
self.embedding_dropout = nn.Dropout(p)
self.batch_norm_num = nn.BatchNorm1d(num_numerical_cols)
all_layers = []
num_categorical_cols = sum((nf for ni, nf in embedding_size))
input_size = num_categorical_cols + num_numerical_cols
for i in layers:
all_layers.append(nn.Linear(input_size, i))
all_layers.append(nn.ReLU(inplace=True))
all_layers.append(nn.BatchNorm1d(i))
all_layers.append(nn.Dropout(p))
input_size = i
all_layers.append(nn.Linear(layers[-1], output_size))
self.layers = nn.Sequential(*all_layers)
def forward(self, x_categorical, x_numerical):
embeddings = []
for i,e in enumerate(self.all_embeddings):
embeddings.append(e(x_categorical[:,i]))
x = torch.cat(embeddings, 1)
x = self.embedding_dropout(x)
x_numerical = self.batch_norm_num(x_numerical)
x = torch.cat([x, x_numerical], 1)
x = self.layers(x)
return x
model = Model(cat_embedding_sizes, num_data.shape[1], 1, [200,100,50], p=0.4)
print(model)
loss_function = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
epochs = 300
aggregated_losses = []
for i in range(epochs):
i += 1
y_pred = model(cat_train_data, num_train_data)
single_loss = loss_function(y_pred, train_op)
aggregated_losses.append(single_loss)
if i%25 == 1:
print(f'epoch: {i:3} loss: {single_loss.item():10.8f}')
optimizer.zero_grad()
single_loss.backward()
optimizer.step()
print(f'epoch: {i:3} loss: {single_loss.item():10.10f}')
print(train_op)
print(type(train_op))
print(op_column)
print(type(op_column))
print(op_data)
print(type(op_data))
con.close()
输出:
51779
10355
(51779, 11)
torch.Size([51779, 4])
torch.Size([51779, 1])
torch.Size([51779])
[(185, 50), (1, 1), (302, 50), (303, 50)]
41424
41424
41424
10355
10355
10355
Model(
(all_embeddings): ModuleList(
(0): Embedding(185, 50)
(1): Embedding(1, 1)
(2): Embedding(302, 50)
(3): Embedding(303, 50)
)
(embedding_dropout): Dropout(p=0.4, inplace=False)
(batch_norm_num): BatchNorm1d(1, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(layers): Sequential(
(0): Linear(in_features=152, out_features=200, bias=True)
(1): ReLU(inplace=True)
(2): BatchNorm1d(200, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(3): Dropout(p=0.4, inplace=False)
(4): Linear(in_features=200, out_features=100, bias=True)
(5): ReLU(inplace=True)
(6): BatchNorm1d(100, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(7): Dropout(p=0.4, inplace=False)
(8): Linear(in_features=100, out_features=50, bias=True)
(9): ReLU(inplace=True)
(10): BatchNorm1d(50, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(11): Dropout(p=0.4, inplace=False)
(12): Linear(in_features=50, out_features=1, bias=True)
)
)
Traceback (most recent call last):
File line 193, in <module>
single_loss = loss_function(y_pred, train_op)
File anaconda3\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File anaconda3\lib\site-packages\torch\nn\modules\loss.py", line 961, in forward
return F.cross_entropy(input, target, weight=self.weight,
File anaconda3\lib\site-packages\torch\nn\functional.py", line 2468, in cross_entropy
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
File anaconda3\lib\site-packages\torch\nn\functional.py", line 2264, in nll_loss
ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: expected scalar type Long but found Double
51779
10355
(51779, 11)
火炬尺寸([51779,4])
火炬尺寸([51779,1])
火炬尺寸([51779])
[(185, 50), (1, 1), (302, 50), (303, 50)]
41424
41424
41424
10355
10355
10355
模型(
(所有嵌入):模块列表(
(0):嵌入(185,50)
(1) :嵌入(1,1)
(2) :嵌入(302,50)
(3) :嵌入(303,50)
)
(嵌入_辍学):辍学(p=0.4,原地=假)
(批次标准数量):批次标准1D(1,eps=1e-05,动量=0.1,仿射=True,轨迹运行统计=True)
(层):顺序(
(0):线性(输入特征=152,输出特征=200,偏差=真)
(1) :ReLU(就地=真)
(2) :BatchNormalD(200,eps=1e-05,动量=0.1,仿射=True,轨迹\跑步\统计=True)
(3) :辍学(p=0.4,原地=假)
(4) :线性(输入特征=200,输出特征=100,偏差=真)
(5) :ReLU(就地=真)
(6) :BatchNormalD(100,eps=1e-05,动量=0.1,仿射=True,轨迹\跑步\统计=True)
(7) :辍学(p=0.4,原地=假)
(8) :线性(输入特征=100,输出特征=50,偏差=真)
(9) :ReLU(就地=真)
(10) :BatchNormalD(50,eps=1e-05,动量=0.1,仿射=True,轨迹\跑步\统计=True)
(11) :辍学(p=0.4,原地=假)
(12) :线性(输入特征=50,输出特征=1,偏差=真)
)
)
回溯(最近一次呼叫最后一次):
文件行193,在
单次损耗=损耗功能(y\U pred,列车运行)
文件anaconda3\lib\site packages\torch\nn\modules\module.py”,第727行,在调用impl中
结果=自我转发(*输入,**kwargs)
文件anaconda3\lib\site packages\torch\nn\modules\loss.py”,第961行,向前
返回F.交叉熵(输入,目标,权重=自身权重,
文件anaconda3\lib\site packages\torch\nn\functional.py”,第2468行,交叉显示
返回nll_损失(log_softmax(输入,1),目标,重量,无,忽略索引,无,减少)
文件anaconda3\lib\site packages\torch\nn\functional.py”,第2264行,nll\U丢失
ret=torch.\u C.\u nn.nll\u损失(输入、目标、重量、减少量、获取枚举(减少量)、忽略索引)
RuntimeError:标量类型应为Long,但找到Double
nn.CrossEntropyLoss()
需要类型为Long
的目标张量,但您传递的是类型为Double
试着改变这一行
发件人:
single\u loss=loss\u功能(y\u pred,train\u op)
致:
single_loss=loss_函数(y_pred,train_op.long())
谢谢@planet_pluto,它改变了错误哈哈。索引器:目标159超出了范围。如果我不能弄清楚,我会继续玩它,然后回来问更多问题。再次感谢。不客气。你的新错误意味着输入标签和mo输出之间的类数不匹配例如:您的输入标签可能有5个类[0,1,2,3,4]
,但模型的预测输出可能是<5个类[0,1,2,3]
,在这种情况下,会出现错误。我该如何找出差异所在?我假设它在这里的某个地方所有层=[]num\u categorical\u cols=sum((nf表示ni,nf表示嵌入大小)input\u size=num\u categorical\u cols+num\u numerical\u cols,但我不知道从哪里开始更改它。从您的模型中,我可以看到最后一层输出一个值。在计算损耗函数()之前,您是否可以尝试打印训练op.shape
和y\u pred.shape
然后在这里发布?