私はPyTorchの新機能ですが、損失機能の計算時にターゲットと入力のサイズがtorch.nn.BCELoss()でどのように機能するかを理解しようとしています。PyTorch ValueError:ターゲットと入力の要素数が同じでなければなりません
import torch
import torch.nn as nn
from torch.autograd import Variable
time_steps = 15
batch_size = 3
embeddings_size = 100
num_classes = 2
model = nn.LSTM(embeddings_size, num_classes)
input_seq = Variable(torch.randn(time_steps, batch_size, embeddings_size))
lstm_out, _ = model(input_seq)
last_out = lstm_out[-1]
print(last_out)
loss = nn.BCELoss()
target = Variable(torch.LongTensor(batch_size).random_(0, num_classes))
print(target)
err = loss(last_out.long(), target)
err.backward()
私は次のエラーを受け取りました:
Warning (from warnings module):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/nn/functional.py", line 767
"Please ensure they have the same size.".format(target.size(), input.size()))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/nn/functional.py", line 770, in binary_cross_entropy
"!= input nelement ({})".format(target.nelement(), input.nelement()))
ValueError: Target and input must have the same number of elements. target nelement (3) != input nelement (6)
このエラーは間違いなくlast_out(サイズ3×2)とターゲット(サイズ3)の種類のサイズから来ています。だから私の質問はどのようにlast_outを(3のサイズとちょうど0と1を含む)ターゲットのようなものに変換することができますか?