RuntimeError: multi-target not supported at /pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15____
RuntimeError: multi-target not supported at /pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15____
我遇到了那个错误
RuntimeError:/pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15____
不支持多目标
我的输入是340的二进制向量,目标是8的二进制向量,对于'"
loss = criterion(outputs, stat_batch)
, 我得到 outputs.shape
= [64,8] 和 stat_batch.shape
=[64,8]
这是模型
class MMP(nn.Module):
def __init__(self, M=1):
super(MMP, self).__init__()
# input layer
self.layer1 = nn.Sequential(
nn.Conv1d(340, 256, kernel_size=1, stride=1, padding=0),
nn.ReLU())
self.layer2 = nn.Sequential(
nn.Conv1d(256, 128, kernel_size=1, stride=1, padding=0),
nn.ReLU())
self.layer3 = nn.Sequential(
nn.Conv1d(128, 64, kernel_size=1, stride=1, padding=0),
nn.ReLU())
self.drop1 = nn.Sequential(nn.Dropout())
self.batch1 = nn.BatchNorm1d(128)
# LSTM
self.lstm1=nn.Sequential(nn.LSTM(
input_size=64,
hidden_size=128,
num_layers=2,
bidirectional=True,
batch_first= True))
self.fc1 = nn.Linear(128*2,8)
self.sof = nn.Softmax(dim=-1)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = self.drop1(out)
out = out.squeeze()
out = out.unsqueeze(0)
#out = out.batch1(out)
out,_ = self.lstm1(out)
print("lstm",out.shape)
out = self.fc1(out)
out =out.squeeze()
#out = out.squeeze()
out = self.sof(out)
return out
#traiin_model
criterion = nn.CrossEntropyLoss()
if CUDA:
criterion = criterion.cuda()
optimizer = optim.SGD(model.parameters(), lr=LEARNING_RATE, momentum=0.9)
for epoch in range(N_EPOCHES):
tot_loss=0
# Training
for i, (seq_batch, stat_batch) in enumerate(training_generator):
# Transfer to GPU
seq_batch, stat_batch = seq_batch.to(device), stat_batch.to(device)
print(i)
print(seq_batch)
print(stat_batch)
optimizer.zero_grad()
# Model computation
seq_batch = seq_batch.unsqueeze(-1)
outputs = model(seq_batch)
if CUDA:
loss = criterion(outputs, stat_batch).float().cuda()
else:
loss = criterion(outputs.view(-1), stat_batch.view(-1))
print(f"Epoch: {epoch},number: {i}, loss:{loss.item()}...\n\n")
tot_loss += loss.item(print(f"Epoch: {epoch},file_number: {i}, loss:{loss.item()}...\n\n"))
loss.backward()
optimizer.step()
您的目标 stat_batch
必须具有 (64,)
的形状,因为 nn.CrossEntropyLoss
接受 class 索引, 而不是 一个-热编码。
要么适当地构建标签张量,要么改用 stat_batch.argmax(axis=1)
。
我遇到了那个错误 RuntimeError:/pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15____
不支持多目标我的输入是340的二进制向量,目标是8的二进制向量,对于'"
loss = criterion(outputs, stat_batch)
, 我得到 outputs.shape
= [64,8] 和 stat_batch.shape
=[64,8]
这是模型
class MMP(nn.Module):
def __init__(self, M=1):
super(MMP, self).__init__()
# input layer
self.layer1 = nn.Sequential(
nn.Conv1d(340, 256, kernel_size=1, stride=1, padding=0),
nn.ReLU())
self.layer2 = nn.Sequential(
nn.Conv1d(256, 128, kernel_size=1, stride=1, padding=0),
nn.ReLU())
self.layer3 = nn.Sequential(
nn.Conv1d(128, 64, kernel_size=1, stride=1, padding=0),
nn.ReLU())
self.drop1 = nn.Sequential(nn.Dropout())
self.batch1 = nn.BatchNorm1d(128)
# LSTM
self.lstm1=nn.Sequential(nn.LSTM(
input_size=64,
hidden_size=128,
num_layers=2,
bidirectional=True,
batch_first= True))
self.fc1 = nn.Linear(128*2,8)
self.sof = nn.Softmax(dim=-1)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = self.drop1(out)
out = out.squeeze()
out = out.unsqueeze(0)
#out = out.batch1(out)
out,_ = self.lstm1(out)
print("lstm",out.shape)
out = self.fc1(out)
out =out.squeeze()
#out = out.squeeze()
out = self.sof(out)
return out
#traiin_model
criterion = nn.CrossEntropyLoss()
if CUDA:
criterion = criterion.cuda()
optimizer = optim.SGD(model.parameters(), lr=LEARNING_RATE, momentum=0.9)
for epoch in range(N_EPOCHES):
tot_loss=0
# Training
for i, (seq_batch, stat_batch) in enumerate(training_generator):
# Transfer to GPU
seq_batch, stat_batch = seq_batch.to(device), stat_batch.to(device)
print(i)
print(seq_batch)
print(stat_batch)
optimizer.zero_grad()
# Model computation
seq_batch = seq_batch.unsqueeze(-1)
outputs = model(seq_batch)
if CUDA:
loss = criterion(outputs, stat_batch).float().cuda()
else:
loss = criterion(outputs.view(-1), stat_batch.view(-1))
print(f"Epoch: {epoch},number: {i}, loss:{loss.item()}...\n\n")
tot_loss += loss.item(print(f"Epoch: {epoch},file_number: {i}, loss:{loss.item()}...\n\n"))
loss.backward()
optimizer.step()
您的目标 stat_batch
必须具有 (64,)
的形状,因为 nn.CrossEntropyLoss
接受 class 索引, 而不是 一个-热编码。
要么适当地构建标签张量,要么改用 stat_batch.argmax(axis=1)
。