欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyTorch定義Tensor及索引和切片(最新推薦)

 更新時間:2023年04月20日 15:07:04   作者:林每天都要努力  
這篇文章主要介紹了PyTorch定義Tensor以及索引和切片,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

深度學(xué)習(xí)--PyTorch定義Tensor

一、創(chuàng)建Tensor

1.1未初始化的方法

?這些方法只是開辟了空間,所附的初始值(非常大,非常小,0),后面還需要我們進(jìn)行數(shù)據(jù)的存入。

torch.empty():返回一個沒有初始化的Tensor,默認(rèn)是FloatTensor類型。

#torch.empty(d1,d2,d3)函數(shù)輸入的是shape 
torch.empty(2,3,5)
 
#tensor([[[-1.9036e-22,  6.8944e-43,  0.0000e+00,  0.0000e+00, -1.0922e-20],
#         [ 6.8944e-43, -2.8812e-24,  6.8944e-43, -5.9272e-21,  6.8944e-43],
#         [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00]],
#
#        [[ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00],
#         [ 0.0000e+00,  0.0000e+00,  1.4013e-45,  0.0000e+00,  0.0000e+00],
#         [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00]]])

torch.FloatTensor():返回沒有初始化的FloatTensor。

#torch.FloatTensor(d1,d2,d3)
torch.FloatTensor(2,2)
 
#tensor([[-0.0000e+00,  4.5907e-41],
#        [-7.3327e-21,  6.8944e-43]])

torch.IntTensor():返回沒有初始化的IntTensor。

#torch.IntTensor(d1,d2,d3)
torch.IntTensor(2,2)
 
#tensor([[          0,  1002524760],
#        [-1687359808,         492]], dtype=torch.int32)

1.2 隨機(jī)初始化

  • 隨機(jī)均勻分布:rand/rand_like,randint

rand:[0,1)均勻分布;randint(min,max,[d1,d2,d3]) 返回[min,max)的整數(shù)均勻分布

#torch.rand(d1,d2,d3)
torch.rand(2,2)
 
#tensor([[0.8670, 0.6158],
#        [0.0895, 0.2391]])
 
#rand_like()
a=torch.rand(3,2)
torch.rand_like(a)
 
#tensor([[0.2846, 0.3605],
#        [0.3359, 0.2789],
#        [0.5637, 0.6276]])
 
#randint(min,max,[d1,d2,d3])
torch.randint(1,10,[3,3,3])
 
#tensor([[[3, 3, 8],
#         [2, 7, 7],
#         [6, 5, 9]],
#
#        [[7, 9, 9],
#         [6, 3, 9],
#         [1, 5, 6]],
#
#        [[5, 4, 8],
#         [7, 1, 2],
#         [3, 4, 4]]])
  • 隨機(jī)正態(tài)分布 randn

randn返回一組符合N(0,1)正態(tài)分布的隨機(jī)數(shù)據(jù)

#randn(d1,d2,d3)
torch.randn(2,2)
 
#tensor([[ 0.3729,  0.0548],
#        [-1.9443,  1.2485]])
 
#normal(mean,std) 需要給出均值和方差
torch.normal(mean=torch.full([10],0.),std=torch.arange(1,0,-0.1))
 
#tensor([-0.8547,  0.1985,  0.1879,  0.7315, -0.3785, -0.3445,  0.7092,  0.0525, 0.2669,  0.0744])
#后面需要用reshape修正成自己想要的形狀

1.3 賦值初始化

full:返回一個定值

#full([d1,d2,d3],num)
torch.full([2,2],6)
 
#tensor([[6, 6],
#        [6, 6]])
 
torch.full([],6)
#tensor(6)   標(biāo)量
 
torch.full([1],6)
#tensor([6]) 向量

arange:返回一組階梯,等差數(shù)列

#torch.arange(min,max,step):返回一個[min,max),步長為step的集體數(shù)組,默認(rèn)為1
torch.arange(0,10)
 
#tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 
torch.arange(0,10,2)
#tensor([0, 2, 4, 6, 8])

linspace/logspace:返回一組階梯

#torch.linspace(min,max,steps):返回一個[min,max],數(shù)量為steps的數(shù)組
torch.linspace(1,10,11)
 
#tensor([ 1.0000,  1.9000,  2.8000,  3.7000,  4.6000,  5.5000,  6.4000,  7.3000,
#         8.2000,  9.1000, 10.0000])
 
#torch.logspace(a,b,steps):返回一個[10^a,10^b],數(shù)量為steps的數(shù)組
torch.logspace(0,1,10)
 
#tensor([ 1.0000,  1.2915,  1.6681,  2.1544,  2.7826,  3.5938,  4.6416,  5.9948,
#         7.7426, 10.0000])

ones/zeros/eye:返回全1全0或者對角陣 ones_like/zeros_like

#torch.ones(d1,d2)
torch.ones(2,2)
 
#tensor([[1., 1.],
#        [1., 1.]])
 
#torch.zeros(d1,d2)
torch.zeros(2,2)
 
#tensor([[0., 0.],
#        [0., 0.]])
 
#torch.eye() 只能接收一個或兩個參數(shù)
torch.eye(3)
 
#tensor([[1., 0., 0.],
#        [0., 1., 0.],
#        [0., 0., 1.]])
 
torch.eye(2,3)
 
#tensor([[1., 0., 0.],
#        [0., 1., 0.]])

1.4 隨機(jī)打散變量

randperm:一般用于位置操作。類似random.shuffle()。

torch.randperm(8)
#tensor([2, 6, 7, 5, 3, 4, 1, 0])

二、索引與切片

簡單索引方式

a=torch.rand(4,3,28,28)
a[0].shape
#torch.Size([3, 28, 28])
a[0,0,0,0]
#tensor(0.9373)

批量索引方式 開始位置:結(jié)束位置 左邊取的到,右邊取不到 算是一種切片 [0,1,2]->[-3,-2,-1]

a[:2].shape
#torch.Size([2, 3, 28, 28])
a[1:].shape
#torch.Size([3, 3, 28, 28])

隔行采樣方式 開始位置:結(jié)束位置:間隔

a[:,:,0:28:2,:].shape
#torch.Size([4, 3, 14, 28])

任意取樣方式 a.index_select(d,[d層的數(shù)據(jù)索引])

a.index_select(0,torch.tensor([0,2])).shape
#torch.Size([2, 3, 28, 28])
 
a.index_select(1,torch.tensor([0,2])).shape
#torch.Size([4, 2, 28, 28])

...任意維度取樣

a[...].shape
#torch.Size([4, 3, 28, 28])
 
a[0,...].shape
#torch.Size([3, 28, 28])
 
a[:,2,...].shape
#torch.Size([4, 28, 28])

掩碼索引mask x.ge(0.5) 表示大于等于0.5的為1,小于0.5的為0

#torch.masked_select 取出掩碼對應(yīng)位置的值
x=torch.randn(3,4)
mask=x.ge(0.5)
torch.masked_select(x,mask)
 
#tensor([1.6950, 1.2207, 0.6035])

具體索引 take(變量,位置) 會把變量變?yōu)橐痪S的

x=torch.randn(3,4)
torch.take(x,torch.tensor([0,1,5]))
 
#tensor([-2.2092, -0.2652,  0.4848])

到此這篇關(guān)于PyTorch定義Tensor以及索引和切片的文章就介紹到這了,更多相關(guān)PyTorch Tensor索引和切片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Django中使用 Closure Table 儲存無限分級數(shù)據(jù)

    Django中使用 Closure Table 儲存無限分級數(shù)據(jù)

    對于數(shù)據(jù)量大的情況(比如用戶之間有邀請鏈,有點三級分銷的意思),就要用到 closure table 的結(jié)構(gòu)來進(jìn)行存儲。這篇文章主要介紹了Django中使用 Closure Table 儲存無限分級數(shù)據(jù),需要的朋友可以參考下
    2019-06-06
  • python命令行工具Click快速掌握

    python命令行工具Click快速掌握

    這篇文章主要介紹了python命令行工具Click快速掌握,寫 Python 的經(jīng)常要寫一些命令行工具,雖然標(biāo)準(zhǔn)庫提供有命令行解析工具 Argparse,但是寫起來非常麻煩,我很少會使用它。命令行工具中用起來最爽的就是 Click,,需要的朋友可以參考下
    2019-07-07
  • Django中模型Model添加JSON類型字段的方法

    Django中模型Model添加JSON類型字段的方法

    這篇文章主要介紹了Django中模型Model添加JSON類型字段的方法,實例分析了Python的Django框架模型使用技巧,需要的朋友可以參考下
    2015-06-06
  • python在不同層級目錄import模塊的方法

    python在不同層級目錄import模塊的方法

    這篇文章主要介紹了python 在不同層級目錄import 模塊的方法,需要的朋友可以參考下
    2016-01-01
  • pytorch實現(xiàn)mnist手寫彩色數(shù)字識別

    pytorch實現(xiàn)mnist手寫彩色數(shù)字識別

    這篇文章主要介紹了pytorch-實現(xiàn)mnist手寫彩色數(shù)字識別,文章圍繞主題展開詳細(xì)的內(nèi)容姐介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Pytorch使用DataLoader實現(xiàn)批量加載數(shù)據(jù)

    Pytorch使用DataLoader實現(xiàn)批量加載數(shù)據(jù)

    這篇文章主要介紹了Pytorch使用DataLoader實現(xiàn)批量加載數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • python文件名批量重命名腳本實例代碼

    python文件名批量重命名腳本實例代碼

    這篇文章主要給大家介紹了關(guān)于python文件名批量重命名腳本的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • python3 常見解密加密算法實例分析【base64、MD5等】

    python3 常見解密加密算法實例分析【base64、MD5等】

    這篇文章主要介紹了python3 常見解密加密算法,結(jié)合實例形式分析了Python的base64模塊加密,以及基于pycrypto模塊的MD5加密等相關(guān)操作技巧,需要的朋友可以參考下
    2019-12-12
  • Python使用win32com模塊實現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)自動生成word表格的方法

    Python使用win32com模塊實現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)自動生成word表格的方法

    這篇文章主要介紹了Python使用win32com模塊實現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)自動生成word表格的方法,結(jié)合實例形式分析了win32com模塊下載、連接mysql、查詢獲取表結(jié)構(gòu)以及使用win32com生成word表格的相關(guān)操作技巧,需要的朋友可以參考下
    2018-07-07
  • pytorch常見的Tensor類型詳解

    pytorch常見的Tensor類型詳解

    今天小編就為大家分享一篇pytorch常見的Tensor類型詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01

最新評論