gpt4 book ai didi

python - 运行时错误: expected scalar type Long but found Int in loss = criterion(outputs, y_train)

转载 作者:行者123 更新时间:2023-12-01 06:22:25 28 4
gpt4 key购买 nike

我使用特征 dim = [1124823,13] 和标签 dim = [1124823,1] 构建了这个声学模型,并将两者拆分为训练、测试和开发。当我尝试运行模型时出现此错误的问题

运行时错误:预期标量类型为 Long,但在
中找到 Int 损失=标准(输出,y_train)

import torch
import torch.nn as nn
from fela import feat, labels
from Dataloader import train_loader, test_loader, X_train, X_test, X_val, y_train, y_test, y_val

################################################################################################
input_size = 13
hidden1_size = 13
hidden2_size = 128
hidden3_size = 64
output_size = 50

################################################################################################

class DNN(nn.Module):
def __init__(self, input_size, hidden2_size, hidden3_size, output_size):
super(DNN, self).__init__()
self.fc1 = nn.Linear(input_size, hidden1_size)
self.relu1 = nn.ReLU()
self.fc2 = nn.Linear(hidden1_size, hidden2_size)
self.relu2 = nn.ReLU()
self.fc3 = nn.Linear(hidden2_size, hidden3_size)
self.relu3 = nn.ReLU()
self.fc4 = nn.Linear(hidden3_size, output_size)
self.relu4 = nn.ReLU()

def forward(self, x):
out = self.fc1(x)
out = self.relu1(out)
out = self.fc2(out)
out = self.relu2(out)
out = self.fc3(out)
out = self.relu3(out)
out = self.fc4(out)
out = self.relu4(out)
return out
################################################################################################
# Instantiate the model
batch_size = 50
n_iterations = 50
no_epochs = 80
model = DNN(input_size, hidden2_size, hidden3_size, output_size)

################################################################################################
# Define the loss criterion and optimizer
criterion = nn.CrossEntropyLoss()
learning_rate = 0.01
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
print(model)
########################################################################################################################
# train the network
iter = 0
for epoch in range(no_epochs):
for i, (X_train, y_train) in enumerate(train_loader):
optimizer.zero_grad()
outputs = model(X_train)
loss = criterion(outputs, torch.max(labels, 1)[1])
loss.backward()
optimizer.step()
iter += 1
if iter % 500 == 0:
correct = 0
total = 0
for X_test, y_test in test_loader:
outputs = model(X_test)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum()
accuracy = 100 * correct / total
print(iter, loss.data[0], accuracy)

最佳答案

我认为 no_epochs=0 与此初始化。可能 (len(train_loader)/batch_size) > n_iterations。然后 int(no_eps) = 0。例如,尝试手动将 no_epochs 更改为 100。

no_eps = n_iterations / (len(train_loader) / batch_size)
no_epochs = int(no_eps)
for epoch in range(no_epochs):

关于python - 运行时错误: expected scalar type Long but found Int in loss = criterion(outputs, y_train),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60300668/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com