RuntimeError: 0D or 1D target tensor expected, multi-target not supported

username: giwoung
cooked: <p>import torch<br>
from torch import nn, optim<br>
from torch.utils.data import DataLoader<br>
import numpy as np<br>
from torchsummary import summary</p>
<p>X1 = torch.randn(300, 1, 256, 256)<br>
X2 = torch.randn(300, 10)<br>
y= torch.randn(300, 10)</p>
<p>class MyModel(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(MyModel, self).<strong>init</strong>()<br>
self.features1 = nn.Sequential(<br>
nn.Conv2d(1, 3, 3, 1, 1),<br>
nn.MaxPool2d(2),<br>
nn.ReLU(),<br>
)<br>
self.features2 = nn.Sequential(<br>
nn.Linear(10, 5),<br>
nn.ReLU(),<br>
)</p>
<pre><code>    self.classifier = nn.Linear(128 * 128 * 3 + 5, 4)

def forward(self, x1, x2):
    x1 = self.features1(x1)
    x2 = self.features2(x2)

    x1 = x1.view(x1.size(0), -1)
    x2 = x2.view(x2.size(0), -1)

    x = torch.cat((x1, x2), dim=1)
    x = self.classifier(x)
    return x
</code></pre>
<p>class MultiTaskDataset:<br>
def <strong>init</strong>(self, image_input, non_image_input, target):<br>
self.image_input = image_input<br>
self.non_image_input = non_image_input<br>
self.target = target</p>
<pre><code>def __len__(self):
    return self.image_input.shape[-1]

def __getitem__(self, idx):
    image_input = self.image_input[idx]
    non_image_input = self.non_image_input[idx]
    target = self.target[idx]

    return ([image_input,
            non_image_input],
            target)

    #return ([torch.tensor(image_input, dtype=torch.float32),
    #        torch.tensor(non_image_input, dtype=torch.float32)],
    #        torch.tensor(target, dtype=torch.long))
</code></pre>
<p>model = MyModel()<br>
print(model)</p>
<p>ds = MultiTaskDataset(X1, X2, y)<br>
train_loader = DataLoader(ds, batch_size=1, shuffle=True)</p>
<p>first_batch = next(iter(train_loader))</p>
<p>def train(epoch):<br>
model.train()<br>
<span class="hashtag-raw">#exp_lr_scheduler</span>.step()<br>
print(“epoch”,epoch)</p>
<pre><code>for batch_idx, (data, target) in enumerate(train_loader):
    amp, phase = data
    print("amp shape, phase.shape")
    print(amp.shape, phase.shape)

    if torch.cuda.is_available():
        amp = amp.cuda()
        phase = phase.cuda()
        target = target.cuda()


    optimizer.zero_grad()
    output = model(amp, phase)
    loss = criterion(output, target.long())

    loss.backward()
    optimizer.step()

    print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
            epoch, (batch_idx + 1) * len(data), len(train_loader.dataset),
            100. * (batch_idx + 1) / len(train_loader), loss.data))
</code></pre>
<p>optimizer = optim.Adam(model.parameters(), lr=0.003)<br>
criterion = nn.CrossEntropyLoss()</p>
<p>n_epochs = 5<br>
for epoch in range(n_epochs):<br>
train(epoch)</p>
<p>torch.save(model, ‘model.pth’)<br>
model = torch.load(“model.pth”)<br>
print(model)</p>

username: ptrblck
cooked: <p>Your code is not properly formatted and hard to read, but the error is raised by passing a wrong target to <code>nn.CrossEntropyLoss</code> as seen here:</p>
<pre data-code-wrap="python"><code class="lang-python">batch_size = 5
nb_classes = 10

criterion = nn.CrossEntropyLoss()
output = torch.randn(batch_size, nb_classes, requires_grad=True)
target = torch.randint(0, nb_classes, (batch_size,))

# works
loss = criterion(output, target)

# fails since shape is wrong using indices
target = torch.randint(0, nb_classes, (batch_size, nb_classes))
loss = criterion(output, target)
# RuntimeError: Expected floating point type for target with class probabilities, got Long

# works if probabilities / soft targets are used
target = torch.randint(0, 2, (batch_size, nb_classes)).float()
loss = criterion(output, target)
</code></pre>