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())。layout
device
: 構(gòu)造張量的裝置。如果沒有,并且數(shù)據(jù)是張量,那么就使用數(shù)據(jù)設(shè)備。如果沒有且數(shù)據(jù)不是張量,則結(jié)果張量在CPU上構(gòu)造。requires_grad
: 是否需要保留梯度信息。默認(rèn)值:False。
3、生成序列
3.1、 生成一個(gè)指定步長的等差序列
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 生成一個(gè)指定步數(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、生成一個(gè)指定大小張量
torch.empty((2,3), dtype=torch.int64) Out[59]:? tensor([[0, 0, 0], ? ? ? ? [0, 0, 2]])
6、 創(chuàng)建一個(gè)指定大小的張量。張量的數(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中的浮點(diǎn)數(shù)運(yùn)算不準(zhǔn)確問題
這篇文章主要介紹了聊聊Python中的浮點(diǎn)數(shù)運(yùn)算不準(zhǔn)確問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03opencv3/Python 稠密光流calcOpticalFlowFarneback詳解
今天小編就為大家分享一篇opencv3/Python 稠密光流calcOpticalFlowFarneback詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python如何執(zhí)行精確的浮點(diǎn)數(shù)運(yùn)算
這篇文章主要介紹了Python如何執(zhí)行精確的浮點(diǎn)數(shù)運(yùn)算,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07全網(wǎng)首秀之Pycharm十大實(shí)用技巧(推薦)
PyCharm 應(yīng)該是大多數(shù) python 開發(fā)者的首選 IDE,每天我們都在上面敲著熟悉的代碼,寫出一個(gè)又一個(gè)奇妙的功能。這篇文章主要介紹了Pycharm十大實(shí)用技巧,需要的朋友可以參考下2020-04-04Python繪圖Matplotlib之坐標(biāo)軸及刻度總結(jié)
這篇文章主要介紹了Python繪圖Matplotlib之坐標(biāo)軸及刻度總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06解決Ubuntu pip 安裝 mysql-python包出錯(cuò)的問題
今天小編就為大家分享一篇解決Ubuntu pip 安裝 mysql-python包出錯(cuò)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06