return _VF.meshgrid(tensors, **kwargs) 的参考解决方法

写在前面

自己的测试环境:
Ubuntu20.04, anaconda

一、问题描述

/home/wong/ProgramFiles/anaconda3/envs/pytorch_env/lib/python3.8/site-packages/torch/functional.py:504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at /opt/conda/conda-bld/pytorch_1702400431970/work/aten/src/ATen/native/TensorShape.cpp:3526.)
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]

二、解决方法

出现这个个警告是关于 PyTorch 中的 torch.meshgrid 函数的使用,未来版本中将要求传递索引参数。解决这个警告的方法是在调用 torch.meshgrid 函数时 显式 地传递索引参数。
也就是自己程序中的

U, V = torch.meshgrid(u, v)

需要改为

U, V = torch.meshgrid(u, v, indexing='xy')

三、调用示例

torch.meshgrid 函数程序调用示例:
torch.meshgrid 函数用于生成一个网格,这个网格包含了所有输入张量的组合。通常情况下,我们会用它来生成坐标网格,用于在二维或三维空间中进行计算或者可视化操作。
indexing参数有两个可选值:xyijxy 模式是默认的,它遵循二维网格的 (x, y) 索引顺序。ij 模式则遵循数学上的(i, j)索引顺序。

import torch

# 定义两个一维张量
u = torch.tensor([0, 1, 2])
v = torch.tensor([3, 4, 5])

# 生成坐标网格
U, V = torch.meshgrid(u, v, indexing='xy')

print(U)
print(V)

输出结果为

tensor([[0, 1, 2],
        [0, 1, 2],
        [0, 1, 2]])
tensor([[3, 3, 3],
        [4, 4, 4],
        [5, 5, 5]])

参考链接

[1] gpt.

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值