日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

PyTorch中的torch.cat簡單介紹_python

作者:DaYinYi ? 更新時間: 2022-05-19 編程語言

1.toych簡單介紹

torch包含了多維疑是的數據結構及基于其上的多種數學操作。

torch包含了多維張量的數據結構以及基于其上的多種數學運算。此外,它也提供了多種實用工具,其中一些可以更有效地對張量和任意類型進行序列化的工具。

它具有CUDA的對應實現,可以在NVIDIA GPU上進行張量運算(計算能力>=3.0)

2. 張量Tensors

torch.is_tensor(obj):如果obj是一個pytorch張量,則返回True

torch.is_storage(obj):如果obj是一個pytorch storage對象,則返回True

torch.numel(input):返回input張量中的元素個數。

3.torch.cat

a = torch.ones([1,2])
?
b = torch.ones([1,2])
?
z = torch.cat([a,b],1)
?
a
Out[47]: tensor([[1., 1., 1., 1.]])
?
a
Out[48]: tensor([[1., 1.]])

如果第二個參數是1,torch.cat就是將a,b 按列放在一起,大小為torch.Size([1,4])。如果第二個參數是0,則按行

行放在一起,大小為 torch.Size([2, 2]) 。

字面理解:torch.cat是將兩個張量(tensor)拼接在一起,cat是concatenate的意思,即拼接,聯系在一起。

例子理解:

import torch
A = torch.ones(2,3)
A
#tensor([[1., 1., 1.],
# ? ? ? ?[1., 1., 1.]])
B=2*torch.ones(4,3)
B
#tensor([[2., 2., 2.],
# ? ? ? ?[2., 2., 2.],
# ? ? ? ?[2., 2., 2.],
# ? ? ? ?[2., 2., 2.]])
C = torch.cat((A,B),0) #按維數0(添加到行)拼接
C
#tensor([[1., 1., 1.],
# ? ? ? ?[1., 1., 1.],
# ? ? ? ?[2., 2., 2.],
# ? ? ? ?[2., 2., 2.],
# ? ? ? ?[2., 2., 2.],
# ? ? ? ?[2., 2., 2.]])
D = 2*torch.ones(2,4)
M = torch.cat((A,D),1) ?# 按維數1(列)拼接
M
#tensor([[1., 1., 1., 2., 2., 2., 2.],
# ? ? ? ?[1., 1., 1., 2., 2., 2., 2.]])
M.size()
#torch.Size([2, 7])

使用torch.cat((A,B),dim)時,除拼接維數dim數值可不同外其余維數數值需相同,方能對齊

原文鏈接:https://blog.csdn.net/qq_36998053/article/details/123378198

欄目分類
最近更新