title: Loss function used for multi-dimensional feature mapping
body: <p>I am working on a video animation project using PyTorch. My dataset contains 3904x60 mfcc audio features(input) and corresponding 3904x3 video features(output). The goal is to train a neural network model such that given an unknown audio feature, the model maps it into its corresponding video feature. In other words, the neural network performs a 60 to 3 feature mapping. I have already built the neural network following <a href="http://adventuresinmachinelearning.com/convolutional-neural-networks-tutorial-in-pytorch/" rel="nofollow noreferrer">this tutorial</a>:</p>
  <pre><code>class ConvNet(nn.Module):
      def __init__(self):
          super().__init__()
          self.layer1 = nn.Sequential(
              nn.Conv1d(1, 32, kernel_size=5, stride=1, padding=2),
              nn.ReLU(),
              nn.MaxPool1d(kernel_size=2, stride=2))
          self.layer2 = nn.Sequential(
              nn.Conv1d(32, 64, kernel_size=5, stride=1, padding=2),
              nn.ReLU(),
              nn.MaxPool1d(kernel_size=2, stride=2))
          self.drop_out = nn.Dropout()
          self.fc1 = nn.Linear(15 * 64, 1000)
          self.fc2 = nn.Linear(1000, 3)
  
      def forward(self, x):
          out = self.layer1(x)
          out = self.layer2(out)
          out = out.reshape(out.size(0), -1)
          out = self.drop_out(out)
          out = self.fc1(out)
          out = self.fc2(out)
          return out
  </code></pre>
  <p>and my training code looks like:</p>
  <pre><code>model = ConvNet()
  
  criterion = nn.CrossEntropyLoss()
  optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
  
  
  for epoch in range(num_epochs):
      for i, (a, v) in enumerate(train_loader):
          # Run the forward pass
          a = a.float()
          v = v.long()
          outputs = model(a.view(a.size(0),1,a.size(1)))
          loss = criterion(outputs, v)
          loss_list.append(loss.item())
  
          # Backprop and perform Adam optimisation
          optimizer.zero_grad()
          loss.backward()
          optimizer.step()
  
          # Track the accuracy
          total = labels.size(0)
          _, predicted = torch.max(outputs.data, 1)
          correct = (predicted == labels).sum().item()
          acc_list.append(correct / total)
  
          if (i + 1) % 100 == 0:
              print('Epoch[{}/{}],Step[{}/{}],Loss{:.4f},Accuracy{:.2f}%'
                .format(epoch + 1, num_epochs, i + 1, total_step, loss.item(),
                        (correct / total) * 100))
  </code></pre>
  <p>but received an error in training:</p>
  <blockquote>
  <p>---&gt; 15         loss = criterion(outputs, v)</p>
  <p>multi-target not supported at /Users/soumith/miniconda2/conda-bld/pytorch_1532623076075/work/aten/src/THNN/generic/ClassNLLCriterion.c:21</p>
  </blockquote>
  <p>I defined the batch size to be 4 so each a and v in the iteration should be a 4 by 60 tensor and a 4 by 3 tensor, respectively. How do I solve this problem?</p>