IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

参考:http://www.seotest.cn/jishu/57263.html

问题背景:两个不一样的神经网络,读取数据和训练模型等代码一样,只是两个模型不一样。经过分析问题一定出在模型上

原代码

   def forward(self, input):
        x = self.feature(input)
        x = x.squeeze()
        fc = torch.nn.Linear(512, 2).cuda()
        x = fc(x)
        x = F.softmax(x, dim=1)
        return x

报错

D:\APP\Anaconda3\envs\pytorch\python.exe "D:/Workspace/Pycharm/Dog vs. Cat/main.py"
train:   0%|          | 0/20000 [00:00<?, ?it/s]Epoch 1/20
----------
Training...

Traceback (most recent call last):
  File "D:/Workspace/Pycharm/Dog vs. Cat/main.py", line 65, in <module>
    main(epoch_n)
  File "D:/Workspace/Pycharm/Dog vs. Cat/main.py", line 22, in main
    model_trained = train.train_model(model, epoch_n, criterion, optimizer)
  File "D:\Workspace\Pycharm\Dog vs. Cat\train.py", line 60, in train_model
    y_pred = model(batch)
  File "D:\APP\Anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "D:\Workspace\Pycharm\Dog vs. Cat\model_GAP2.py", line 67, in forward
    x = F.softmax(x, dim=1)
  File "D:\APP\Anaconda3\envs\pytorch\lib\site-packages\torch\nn\functional.py", line 1263, in softmax
    ret = input.softmax(dim)
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

Process finished with exit code 1

错误分析:维度错误,想要得到二维的,但是得到的是一维的,对于维度的操作代码可能出现错误; x = x.squeeze()可能错误,换一个方式

改正后代码

    def forward(self, input):
        x = self.feature(input)
        # x = x.squeeze()
        x = x.view(-1, 512)
        fc = torch.nn.Linear(512, 2).cuda()
        x = fc(x)
        x = F.softmax(x, dim=1)
        return x

经过改正,错误完美解决

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
"IndexError: Dimension out of range (expected to be in range of [-2, 1])" 是一个索引错误。这个错误通常发生在代码中当我们尝试访问一个数组或列表的维度超出了其范围时。在这个错误消息中,[-2, 1]表示预期的索引范围,而实际获得的索引超出了这个范围。具体来说,代码中期望使用的是一个二维数组或列表,但实际上提供的是一个一维的数据。 这个错误通常发生在编程中,当我们试图访问数组或列表中不存在的索引或超出范围的索引时。为了解决这个错误,我们需要确保我们的索引值在正确的范围内,并且与数组或列表的维度相对应。 例如,在Python中,如果我们有一个二维数组,我们可以使用索引[i][j]来访问其中的元素。如果我们提供的i超过了数组的行数,或者j超过了数组的列数,就会出现"IndexError: Dimension out of range"错误。 为了解决这个问题,我们需要检查我们的索引值,并确保它们在正确的范围内。我们还需要检查我们的数据结构是否与我们的访问方式相匹配,即确保我们使用的是正确维度的数据结构。 总结来说,"IndexError: Dimension out of range (expected to be in range of [-2, 1])"错误表示我们在访问数组或列表时使用了超出范围的索引。为了解决这个问题,我们需要检查索引值和数据结构是否匹配,并确保索引值在正确的范围内。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)](https://blog.csdn.net/wu_xin1/article/details/116488764)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 2)](https://blog.csdn.net/qq_44711932/article/details/108550088)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值