我建立了具有特征dim = [1124823,13]和标签dim = [1124823,1]的声学模型,然后将两者拆分以进行训练,测试和开发。当我尝试运行模型时出现此错误的问题

RuntimeError:预期的标量类型为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):

10-06 10:21