0

I face that error RuntimeError: multi-target not supported at /pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15____

My input is binary vector of 340, target is binary vector of 8, For '" loss = criterion(outputs, stat_batch), I got outputs.shape= [64,8] and stat_batch.shape=[64,8]

Here is the model

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()
Ivan
  • 34,531
  • 8
  • 55
  • 100
No Na
  • 27
  • 2
  • 7

1 Answers1

0

Your target stat_batch must have a shape of (64,) because nn.CrossEntropyLoss takes in class indices, not one-hot-encoding.

Either construct your label tensor appropriately or use stat_batch.argmax(axis=1) instead.

Ivan
  • 34,531
  • 8
  • 55
  • 100
  • the labels is long tensor already , I tried stat_batch.argmax(axis=1) but gave me an error says ''' loss = criterion(outputs, stat_batch.argmax(axis=1)) TypeError: argmax() got an unexpected keyword argument 'axis'''' – No Na Jan 29 '21 at 20:44
  • *"the labels is long tensor already"* doesn't matter, it needs to be 1D, not multi-dimensional. You must be running an older version of PyTorch: try `stat_batch.argmax(dim=1)`, or just `stat_batch.argmax(1)`. – Ivan Jan 29 '21 at 20:57
  • 1
    Thank you very much . yes it works , but it is still with shape (64,8). I also have very high loss around 1.99 . Can you please tell me where the green checkmark? – No Na Jan 30 '21 at 22:28