pytorch报错:UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5.。。原因

修正PyTorch警告与变量使用
本文解决PyTorch中常见的警告问题,包括如何正确转换0维张量为Python数字,以及更新Variable用法避免错误。介绍了将loss.data[0]替换为loss.item(),并更新Variable初始化方式。
部署运行你感兴趣的模型镜像

UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number

当报这个错的一般会有两个原因,一个是这种写法:

loss.data[0]   

应该改成loss.item()

还有就是这种写法:

im = Variable(im.cuda(),volatile=True)

应该改成im = Variable(im.cuda()) 

 

您可能感兴趣的与本文相关的镜像

PyTorch 2.5

PyTorch 2.5

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

代码报错UserWarning: std(): degrees of freedom is <= 0. Correction should be strictly less than the reduction factor (input numel divided by output numel). (Triggered internally at C:\cb\pytorch_1000000000000\work\aten\src\ATen\native\ReduceOps.cpp:1808.) rewards = (rewards - rewards.mean()) / (rewards.std() + 1e-7) D:\anaconda\envs\9-25-pytorch\Lib\site-packages\torch\nn\modules\loss.py:538: UserWarning: Using a target size (torch.Size([1])) that is different to the input size (torch.Size([])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size. return F.mse_loss(input, target, reduction=self.reduction) Traceback (most recent call last): File "D:\python草稿程序\ppo-uav-5\main.py", line 585, in <module> train_uav_env() File "D:\python草稿程序\ppo-uav-5\main.py", line 483, in train_uav_env ppo.update() File "D:\python草稿程序\ppo-uav-5\PPO.py", line 190, in update logprobs_cont, logprobs_disc, state_values, dist_entropy_cont, dist_entropy_disc = self.policy.evaluate( ^^^^^^^^^^^^^^^^^^^^^ File "D:\python草稿程序\ppo-uav-5\PPO.py", line 110, in evaluate dist_continuous = MultivariateNormal(action_mean, cov_mat) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\anaconda\envs\9-25-pytorch\Lib\site-packages\torch\distributions\multivariate_normal.py", line 178, in __init__ super().__init__(batch_shape, event_shape, validate_args=validate_args) File "D:\anaconda\envs\9-25-pytorch\Lib\site-packages\torch\distributions\distribution.py", line 70, in __init__ raise ValueError( ValueError: Expected parameter loc (Tensor of shape (2,)) of distribution MultivariateNormal(loc: torch.Size([2]), covariance_matrix: torch.Size([2, 2])) to satisfy the constraint IndependentConstraint(Real(), 1), but found invalid values: tensor([nan, nan], device='cuda:0', grad_fn=<ExpandBackward0>)
03-18
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值