首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在nn.crossentropyloss中将期望更改为多维而不是一维

在nn.crossentropyloss中将期望更改为多维而不是一维
EN

Stack Overflow用户
提问于 2020-07-01 20:52:23
回答 2查看 534关注 0票数 0

我的目标是使用EMNIST数据集在Pytorch中进行多类图像分类。作为损失函数,我想使用多类交叉熵损失。

目前,我定义我的损失函数如下:

代码语言:javascript
复制
criterion = nn.CrossEntropyLoss()

我按如下方式训练我的模型:

代码语言:javascript
复制
iter = 0
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        
        # Add a single channel dimension
        # From: [batch_size, height, width]
        # To: [batch_size, 1, height, width]
        images = images.unsqueeze(1)

        # Forward pass to get output/logits
        outputs = model(images)
        
        # Clear gradients w.r.t. parameters
        optimizer.zero_grad()
        
        # Forward pass to get output/logits
        outputs = model(images)

        # Calculate Loss: softmax --> cross entropy loss
        loss = criterion(outputs, labels)
        
        # Getting gradients w.r.t. parameters
        loss.backward()
        
        # Updating parameters
        optimizer.step()
        
        iter += 1
        
        if iter % 500 == 0:
            # Calculate Accuracy         
            correct = 0
            total = 0
            # Iterate through test dataset
            for images, labels in test_loader:
               
                images = images.unsqueeze(1)
                
                # Forward pass only to get logits/output
                outputs = model(images)
                
                # Get predictions from the maximum value
                _, predicted = torch.max(outputs.data, 1)
                
                # Total number of labels
                total += labels.size(0)
                
                correct += (predicted == labels).sum()
            
            accuracy = 100 * correct / total
            
            # Print Loss
            print('Iteration: {}. Loss: {}. Accuracy: {}'.format(iter, loss.data[0], accuracy))

但是,我得到的错误是:

代码语言:javascript
复制
RuntimeError                              Traceback (most recent call last)
<ipython-input-15-c26c43bbc32e> in <module>()
     21 
     22         # Calculate Loss: softmax --> cross entropy loss
---> 23         loss = criterion(outputs, labels)
     24 
     25         # Getting gradients w.r.t. parameters

3 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
   2113                          .format(input.size(0), target.size(0)))
   2114     if dim == 2:
-> 2115         ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
   2116     elif dim == 4:
   2117         ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)

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

我的CNN输出26个变量,我的目标变量也是26D。

如何更改代码以使nn.crossentropyloss()期望26D输入而不是1D输入?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-01 21:53:06

nn.CrossEntropy()(input, target)期望input是一个大小为batchsize X num_classes的单热点向量,target是大小为batchsize的真类的id。

因此,简而言之,您可以使用target = torch.argmax(target, dim=1)更改目标,使其适合nn.CrossEntropy()

票数 2
EN

Stack Overflow用户

发布于 2020-07-01 23:29:24

除了@Flicic Suo的答案之外,你应该使用predicted = torch.argmax(output, dim=1)来得到预测的labels。现在你得到的是最大值,你要找的是具有最大值的类。

你可以通过这种方式获得0.0的准确性,所以argmax是正确的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62677285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档