python meshgrid_torch.meshgrid()和np.meshgrid()的区别
np.meshgrid()函数常用于生成二维网格,比如图像的坐标点。
pytorch中也有一个类似的函数torch.meshgrid(),功能也类似,但是两者的用法有区别,使用时需要注意(刚踩坑,因此记录一下。。。)
比如我要生成一张图像(h=6, w=10)的xy坐标点,看下两者的实现方式:
np.meshgrid()
>>> import numpy as np
>>> h = 6
>>> w = 10
>>> xs, ys = np.meshgrid(np.arange(w), np.arange(h))
>>> xs.shape
(6, 10)
>>> ys.shape
(6, 10)
>>> xs
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
>>> ys
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]])
>>> xys = np.stack([xs, ys], axis=-1)
>>> xys.shape
(6, 10, 2)
torch.meshgrid()
>>> import torch
>>> h = 6
>>> w = 10
>>> ys,xs = torch.meshgrid(torch.arange(h), torch.arange(w))
>>> xs.shape
torch.Size([6, 10])
>>> ys.shape
torch.Size([6, 10])
>>> xs
tensor([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
>>> ys
tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]])
>>> xys = torch.stack([xs, ys], dim=-1)
>>> xys.shape
torch.Size([6, 10, 2])
从python交互式窗口可以清晰的看出numpy和pytorch中meshgrid()函数的区别,就不用文字总结了,自己体会哈哈哈。
python meshgrid_torch.meshgrid()和np.meshgrid()的区别相关推荐
- Python基础--range()和np.arrange()的区别与联系
在使用python时,我们经常需要使用Numpy,而Numpy中又有arange()这个函数,它和python自带函数range()的功能貌似比较相同.那么,range()和np.arange()有什 ...
- np.meshgrid函数中的indexing参数问题
目录 二维meshgrid函数 三维meshgrid函数 特别说明 meshgrid函数在二维空间中可以简单地理解为将x轴与y轴的每个位置的坐标关联起来形成了一个网格,我们知道空间中的点是由坐标确定的 ...
- np.meshgrid()函数
文章目录 (1)自己理解 (2)官方解释 (3)参数: 3.1 x1, x2,-, xn:array_like 3.2 sparse:bool, optional 默认false 3.3 copy:b ...
- np.meshgrid
np.meshgrid参考 官方文档给出的解释 Return coordinate matrices from coordinate vectors. Make N-D coordinate arra ...
- NumPy - np.meshgrid()
np.meshgrid(*xi, **kwargs) 从坐标向量中返回坐标矩阵. np.meshgrid的作用是根据传入的两个一维数组参数生成两个数组元素的列表. 如下所示: 第一个数组为xnums, ...
- np.meshgrid, ravel(), np.c_, plt.contourf()函数的用法,以及决策边界的画法。
前言: 楼主最近在学机器学习时碰到的一些函数,用来画决策边界.记录现在的想法. 1: np.meshgrid的用法: X,Y = np.meshgrid(x,y)是将x中的每个点与y中的每个点连起来成 ...
- np.meshgrid()函数 以及 三维空间中的坐标位置生成 以及 numpy.repeat()函数介绍
一.np.meshgrid()函数 1.np.meshgrid()介绍 X, Y = np.meshgrid(x, y) 代表的是将x中每一个数据和y中每一个数据组合生成很多点,然后将这些点的x坐标放 ...
- np.meshgrid plt.contourf
np.meshgrid & plt.contourf 吴恩达机器学习作业 np.meshgrid && plt.contourf plt.contourf 吴恩达机器学习作业 ...
- np.meshgrid()
目录 1.meshgrid函数介绍 2.meshgrid函数官方说明 1.meshgrid函数介绍 参数: *xi,也就是x1,x2,-,xn :表示网格坐标的一维数组. copy:默认为True,如 ...
最新文章
- Android中图表AChartEngine学习使用与例子
- 怎么把cad的图导入ps_PS中怎么抠图?以扣取头部图像为例
- Enterprise Library 4.0
- 关于tensorflow和keras那些事儿
- python嗅探网页视频_网络嗅探python
- ADMM参考资料(part1)
- 多面体 (Multipatch)
- 斯坦佛编程教程-Unix编程工具(五)
- 微课|玩转Python轻松过二级:第3章课后习题解答4
- ios java环境变量_iOS 环境变量配置(DebugReleaseTest)
- bzoj 3238: [Ahoi2013]差异(后缀数组+单调栈)
- 20165218 《网络对抗技术》Exp0 Kali安装 Week1
- matlab3阶幺矩阵,Matlab操作矩阵的相关方法
- 图片计算景深matlab程序,在线景深计算器
- Google Guice
- Linux的动态库和静态库
- mis系统的编写与设计
- 报错 Tensorflow 2.0 Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
- vue自动计算日期天数
- 对结对编程盟友的个人项目代码分析