PyTorch | UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to

UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.

 

源程序大概是这样的:

import torch.nn as nn

.......

class gan(object):

  def __init__(self,opt,dataloader=None):

    ......

    self.sf = nn.Softmax

    ......

  def test(self):

    self.x = self.sf(self.y)

运行,报错:

/home/liuming/weilanlan/DL/lib/model.py:420: UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.
  self.gt2_pre_labels_tmp[i*self.opt.batchsize : i*self.opt.batchsize+error.size(0)] = torch.argmax(self.smax(self.latent_i_rs), dim = 1)
Segmentation fault (core dumped)

根据报错,添加了dim的信息,self.x = self.sf(self.y,dim=1),运行依然报错

    self.gt2_pre_labels_tmp[i*self.opt.batchsize : i*self.opt.batchsize+error.size(0)] = torch.argmax(self.smax(self.latent_i_rs, dim=1), dim = 1)
  File "/home/liuming/.local/lib/python2.7/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
TypeError: forward() got an unexpected keyword argument 'dim'

 

excuse me????我以为是什么内部问题,后来看到这篇文章https://discuss.pytorch.org/t/hypocritical-error-messages-re-softmax/27206,才发现是我把dim位置放错了

于是按照提示修改了dim的位置,

self.sf =nn.Softmax(dim=1)

成功的解决了这个问题!

解决这个问题花费了我两个小时的时间,希望以后有人在遇到类似的问题可以快速解决这个问题!!

 

欢迎各位不吝赐教。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值