日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python meshgrid_torch.meshgrid()和np.meshgrid()的区别

發布時間:2024/1/23 python 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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()函數的區別,就不用文字總結了,自己體會哈哈哈。

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的python meshgrid_torch.meshgrid()和np.meshgrid()的区别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。