您好,我在尝试使用 pytorch 训练 RNN 分类器时遇到此错误。我希望有人可以帮助我解决它或解释我的原因。
train_loader2=embeddings_train
train_loader2=torch.from_numpy(embeddings_train)
train_loader2 = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
y_train=np.array(y_train)
targets2= torch.from_numpy(y_train)
# Initialize network
model = BRNN(input_size, hidden_size, num_layers, num_classes).to(device)
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# Train Network
for epoch in range(num_epochs):
for i, (dataf, targets2) in enumerate(train_loader2):
# Get data to cuda if possible
dataf = dataf.to(device=device).squeeze(1)
targets2 = targets2.to(device=device)
# forward
scores = model(dataf)
loss = criterion(scores, targets2)
# backward
optimizer.zero_grad()
loss.backward()
# gradient descent or adam step
optimizer.step()
这是它发生的错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-42-d435f4563f2e> in <module>
15 # Train Network
16 for epoch in range(num_epochs):
---> 17 for i, (dataf, targets2) in enumerate(train_loader2):
18 # Get data to cuda if possible
19 dataf = dataf.to(device=device).squeeze(1)
ValueError: too many values to unpack (expected 2)