pytorch創(chuàng)建tensor函數(shù)詳情
1、通過復(fù)制數(shù)據(jù)構(gòu)造張量
1.1 torch.tensor()
torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])
Out[111]:?
tensor([[0.1000, 1.2000],
? ? ? ? [2.2000, 3.1000],
? ? ? ? [4.9000, 5.2000]])
torch.tensor([0, 1])?
Out[112]: tensor([0, 1])
torch.tensor([[0.11111, 0.222222, 0.3333333]],
? ? ? ? ? ? ?dtype=torch.float64, ? ? ? ? ? ? ?device=torch.device('cpu'))
Out[113]: tensor([[0.1111, 0.2222, 0.3333]], dtype=torch.float64)
torch.tensor(3.14159)
Out[114]: tensor(3.1416)
torch.tensor([])?
Out[115]: tensor([])
torch.tensor([[0.11111, 0.222222, 0.3333333]],
? ? ? ? ? ? ?dtype=torch.float64, ? ? ? ? ? ? ?device=torch.device('cpu'), requires_grad=True, pin_memory=False)
Out[117]: tensor([[0.1111, 0.2222, 0.3333]], dtype=torch.float64, requires_grad=True)dtype(torch.dtype,可選)–返回張量的所需數(shù)據(jù)類型。默認(rèn)值:如果沒有,則根據(jù)數(shù)據(jù)推斷數(shù)據(jù)類型。device(torch.device,可選)–構(gòu)造張量的裝置。如果沒有,并且數(shù)據(jù)是張量,那么就使用數(shù)據(jù)設(shè)備。如果沒有且數(shù)據(jù)不是張量,則結(jié)果張量在CPU上構(gòu)造。require_grad(bool,可選)– 是否需要保留梯度信息。默認(rèn)值:False。pin_memory(bool,可選)–如果設(shè)置了,返回的張量將分配到pind內(nèi)存中。僅適用于CPU張量。默認(rèn)值:False。
1.2 將numpy的ndarray轉(zhuǎn)為tensor
>>> a = numpy.array([1, 2, 3])
>>> t = torch.as_tensor(a)
>>> t
tensor([1, 2, 3])
>>> t[0] = -1
>>> a
array([-1, ?2, ?3])
>>> a = numpy.array([1, 2, 3])
>>> t = torch.as_tensor(a, device=torch.device('cuda'))
>>> t[0] = -1
>>> a
array([1, 2, 3])
t = torch.as_tensor([2, 2, 2], device=torch.device('cuda'))
>>> t
tensor([2, 2, 2], device='cuda:0')
a = numpy.array([1, 2, 3])
t = torch.from_numpy(a)
t
Out[38]: tensor([1, 2, 3])
t[0] = -1
a
Out[40]: array([-1, ?2, ?3])2、生成全0或者全1的tensor
torch.zeros(2, 3) Out[41]:? tensor([[0., 0., 0.], ? ? ? ? [0., 0., 0.]]) torch.zeros(5) Out[42]: tensor([0., 0., 0., 0., 0.]) torch.ones(2, 3) Out[43]:? tensor([[1., 1., 1.], ? ? ? ? [1., 1., 1.]]) torch.ones(5) Out[44]: tensor([1., 1., 1., 1., 1.])
參數(shù)列表:
out:輸出的對象dtype:返回的張量的所需數(shù)據(jù)類型。默認(rèn)值:如果沒有,則使用全局默認(rèn)值(請參閱torch.set_Default_tensor_type())。layoutdevice: 構(gòu)造張量的裝置。如果沒有,并且數(shù)據(jù)是張量,那么就使用數(shù)據(jù)設(shè)備。如果沒有且數(shù)據(jù)不是張量,則結(jié)果張量在CPU上構(gòu)造。requires_grad: 是否需要保留梯度信息。默認(rèn)值:False。
3、生成序列
3.1、 生成一個指定步長的等差序列
torch.arange(5) Out[45]: tensor([0, 1, 2, 3, 4]) torch.arange(1, 4) Out[46]: tensor([1, 2, 3]) torch.arange(1, 2.5, 0.5) Out[47]: tensor([1.0000, 1.5000, 2.0000])
start: 點(diǎn)集的開始值。默認(rèn)值:0。end: 點(diǎn)集的結(jié)束值step: 每對相鄰點(diǎn)之間的間隙。默認(rèn)值:1,可以是小數(shù)。
3.2 生成一個指定步數(shù)的等差數(shù)列
torch.linspace(3, 10, steps=5) Out[49]: tensor([ 3.0000, ?4.7500, ?6.5000, ?8.2500, 10.0000]) torch.linspace(-10, 10, steps=5) Out[50]: tensor([-10., ?-5., ? 0., ? 5., ?10.]) torch.linspace(start=-10, end=10, steps=1) Out[51]: tensor([-10.])
4、生成指定大小的單位矩陣
torch.eye(3) Out[58]:? tensor([[1., 0., 0.], ? ? ? ? [0., 1., 0.], ? ? ? ? [0., 0., 1.]])
5、生成一個指定大小張量
torch.empty((2,3), dtype=torch.int64) Out[59]:? tensor([[0, 0, 0], ? ? ? ? [0, 0, 2]])
6、 創(chuàng)建一個指定大小的張量。張量的數(shù)據(jù)是填充的指定值
torch.full((2, 3), 3.141592) Out[67]:? tensor([[3.1416, 3.1416, 3.1416], ? ? ? ? [3.1416, 3.1416, 3.1416]])
到此這篇關(guān)于pytorch創(chuàng)建tensor函數(shù)詳情的文章就介紹到這了,更多相關(guān)pytorch創(chuàng)建tensor函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python使用py2neo創(chuàng)建neo4j的節(jié)點(diǎn)和關(guān)系
這篇文章主要介紹了python使用py2neo創(chuàng)建neo4j的節(jié)點(diǎn)和關(guān)系,第一步使用py2neo連接neo4j的方法然后根據(jù)dict創(chuàng)建Node,更多相關(guān)資料需要的朋友參考下面文章內(nèi)容2022-02-02
numpy.random.shuffle打亂順序函數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了numpy.random.shuffle打亂順序函數(shù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Python識別驗(yàn)證碼的實(shí)現(xiàn)示例
這篇文章主要介紹了Python識別驗(yàn)證碼的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Python實(shí)現(xiàn)監(jiān)聽目錄并取消文件只讀屬性
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)監(jiān)聽目錄并取消文件只讀屬性,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-07-07
python繪制子圖技巧之plt.subplot、plt.subplots及坐標(biāo)軸修改
一個圖片里邊繪制多個圖像是繪圖中的常見需求,下面這篇文章主要給大家介紹了關(guān)于python繪制子圖技巧之plt.subplot、plt.subplots及坐標(biāo)軸修改的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05

