最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Pytorch實(shí)現(xiàn)List?Tensor轉(zhuǎn)Tensor,reshape拼接等操作

 更新時間:2022年11月03日 11:46:45   作者:Bagba  
這篇文章主要介紹了Pytorch實(shí)現(xiàn)List?Tensor轉(zhuǎn)Tensor,reshape拼接等操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

持續(xù)更新一些常用的Tensor操作,比如List,Numpy,Tensor之間的轉(zhuǎn)換,Tensor的拼接,維度的變換等操作。

其它Tensor操作如 einsum等見:待更新。

用到兩個函數(shù):

  • torch.cat
  • torch.stack

一、List Tensor轉(zhuǎn)Tensor (torch.cat)

// An highlighted block
>>> t1 = torch.FloatTensor([[1,2],[5,6]])
>>> t2 = torch.FloatTensor([[3,4],[7,8]])
>>> l = []
>>> l.append(t1)
>>> l.append(t2)
>>> ta = torch.cat(l,dim=0)
>>> ta = torch.cat(l,dim=0).reshape(2,2,2)
>>> tb = torch.cat(l,dim=1).reshape(2,2,2)
>>> ta
tensor([[[1., 2.],
         [5., 6.]],

        [[3., 4.],
         [7., 8.]]])
>>> tb
tensor([[[1., 2.],
         [3., 4.]],

        [[5., 6.],
         [7., 8.]]])

高維tensor

** 如果理解了2D to 3DTensor,以此類推,不難理解3D to 4D,看下面代碼即可明白:**

>>> t1 = torch.range(1,8).reshape(2,2,2)
>>> t2 = torch.range(11,18).reshape(2,2,2)
>>> l = []
>>> l.append(t1)
>>> l.append(t2)
>>> torch.cat(l,dim=2).reshape(2,2,2,2)
tensor([[[[ 1.,  2.],
          [11., 12.]],

         [[ 3.,  4.],
          [13., 14.]]],


        [[[ 5.,  6.],
          [15., 16.]],

         [[ 7.,  8.],
          [17., 18.]]]])
>>> torch.cat(l,dim=1).reshape(2,2,2,2)
tensor([[[[ 1.,  2.],
          [ 3.,  4.]],

         [[11., 12.],
          [13., 14.]]],


        [[[ 5.,  6.],
          [ 7.,  8.]],

         [[15., 16.],
          [17., 18.]]]])
>>> torch.cat(l,dim=0).reshape(2,2,2,2)
tensor([[[[ 1.,  2.],
          [ 3.,  4.]],

         [[ 5.,  6.],
          [ 7.,  8.]]],


        [[[11., 12.],
          [13., 14.]],

         [[15., 16.],
          [17., 18.]]]])

二、List Tensor轉(zhuǎn)Tensor (torch.stack)

代碼:

import torch

t1 = torch.FloatTensor([[1,2],[5,6]])
t2 = torch.FloatTensor([[3,4],[7,8]])
l = [t1, t2]

t3 = torch.stack(l, dim=2)
print(t3.shape)
print(t3)

## output:
## torch.Size([2, 2, 2])
## tensor([[[1., 3.],
##          [2., 4.]],
##        [[5., 7.],
##         [6., 8.]]])

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

高阳县| 遵化市| 稷山县| 成都市| 彭山县| 资兴市| 呈贡县| 乃东县| 桂阳县| 集贤县| 崇明县| 兴安盟| 延安市| 临湘市| 苍梧县| 荥阳市| 景德镇市| 乌鲁木齐县| 钟山县| 冷水江市| 普定县| 容城县| 赣州市| 定边县| 牙克石市| 乐业县| 中卫市| 陵川县| 兴山县| 德化县| 年辖:市辖区| 兴文县| 德钦县| 庆阳市| 武安市| 依安县| 蒙山县| 桃江县| 固始县| 洱源县| 清远市|