PyTorch框架学习二——基本数据结构(张量)
PyTorch框架學(xué)習(xí)二——基本數(shù)據(jù)結(jié)構(gòu)(張量)
- 一、什么是張量?
- 二、Tensor與Variable(PyTorch中)
- 1.Variable
- 2.Tensor
- 三、Tensor的創(chuàng)建
- 1.直接創(chuàng)建Tensor
- (1)torch.tensor()
- (2)torch.from_numpy()
- 2.依據(jù)數(shù)值創(chuàng)建
- (1)torch.zeros()
- (2)torch.zeros_like()
- (3)torch.ones()
- (4)torch.ones_like()
- (5)torch.full()
- (6)torch.full_like()
- (7)torch.arange()
- (8)torch.linspace()
- (9)torch.logspace()
- (10)torch.eye()
- 3.依據(jù)概率分布創(chuàng)建張量
- (1)torch.normal()
- (2)torch.randn()
- (3)torch.randn_like()
- (4)torch.rand()
- (5)torch.rand_like()
- (6)torch.randint()
- (7)torch.randint_like()
- (8)torch.randperm()
- (9)torch.bernoulli()
一、什么是張量?
先來看一些數(shù)學(xué)上常見的名詞:
標量:0維張量
向量:1維張量
矩陣:2維張量
所以,張量(Tensor)是一個多維數(shù)組,是上述提及的標量、向量和矩陣的高維拓展。
二、Tensor與Variable(PyTorch中)
Variable是PyTorch 0.4.0之前的一種數(shù)據(jù)類型,PyTorch 0.4.0開始,Variable已經(jīng)并入了Tensor,所以已經(jīng)不再需要用到Variable,但是為了學(xué)習(xí)的一個循序漸進的過程,我們還是先來看一下Variable這個數(shù)據(jù)類型。
1.Variable
是torch.autograd中的數(shù)據(jù)類型,主要用于封裝Tensor,使得Tensor可以自動求導(dǎo)。
Variable這種數(shù)據(jù)類型包括了五種屬性,分別如下所示:
Variable可以看成是給Tensor加上了四個與梯度相關(guān)的屬性。
2.Tensor
PyTorch 0.4.0 版本開始,Variable并入Tensor。Tensor在Variable的基礎(chǔ)上又添加了三個與數(shù)據(jù)相關(guān)的屬性,一共有八個屬性,分別如下所示:
前四個屬性與數(shù)據(jù)相關(guān),后四個屬性與梯度求導(dǎo)相關(guān)。
三、Tensor的創(chuàng)建
一般Tensor的創(chuàng)建有三種方式:直接創(chuàng)建、依數(shù)值創(chuàng)建和依概率分布創(chuàng)建,下面分別進行介紹:
1.直接創(chuàng)建Tensor
這種創(chuàng)建方式一般是將列表(list)、數(shù)組(ndarray)封裝數(shù)據(jù)的形式直接轉(zhuǎn)變?yōu)門ensor,常用的有torch.tensor()、torch.from_numpy()。
(1)torch.tensor()
功能:從data創(chuàng)建tensor
先給出官方的函數(shù)形式以及參數(shù):
torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False)下面看一下所有的參數(shù):
(2)torch.from_numpy()
功能:從numpy.ndarray創(chuàng)建tensor,相比tensor(),數(shù)據(jù)類型只能是numpy.ndarray。
torch.from_numpy(ndarray)它只有一個參數(shù),就是輸入的ndarry,但是它需要注意的是:torch.from_numpy()創(chuàng)建的tensor與原ndarry是共享內(nèi)存的,當(dāng)修改其中一個數(shù)據(jù)時,另一個也將會被改動。
arr = np.array([[1, 2, 3], [4, 5, 6]]) t = torch.from_numpy(arr) print("numpy array: ", arr) print("tensor : ", t) print("\n修改arr") arr[0, 0] = 0 print("numpy array: ", arr) print("tensor : ", t) print("\n修改tensor") t[0, 0] = -1 print("numpy array: ", arr) print("tensor : ", t)2.依據(jù)數(shù)值創(chuàng)建
(1)torch.zeros()
功能:依據(jù)size創(chuàng)建全0張量
torch.zeros(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)看一下所有的參數(shù):
(2)torch.zeros_like()
功能:依據(jù)input形狀創(chuàng)建全0張量
torch.zeros_like(input, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format)看一下所有的參數(shù):
(3)torch.ones()
功能:依據(jù)size創(chuàng)建全1張量
參數(shù)與torch.zeros()類似
(4)torch.ones_like()
功能:依據(jù)input形狀創(chuàng)建全1張量
參數(shù)與torch.ones()類似
(5)torch.full()
功能:創(chuàng)建一個尺寸為size的張量,里面每個元素的值全為fill_value
torch.full(size, fill_value, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)參數(shù)如下:
(6)torch.full_like()
功能:依據(jù)input的尺寸創(chuàng)建一個張量,值全為full_value。
torch.full_like(input, fill_value, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, memory_format=torch.preserve_format)(7)torch.arange()
功能:創(chuàng)建等差的一維張量。注意數(shù)值區(qū)間為 [ start, end )
(8)torch.linspace()
功能:創(chuàng)建均分的一維張量。注意數(shù)值區(qū)間為 [ start, end ]。
torch.linspace(start, end, steps=100, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)(9)torch.logspace()
功能:創(chuàng)建對數(shù)均分的一維張量,長度為steps,對數(shù)的底為base。
torch.logspace(start, end, steps=100, base=10.0, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)(10)torch.eye()
功能:創(chuàng)建單位對角矩陣(2維張量),默認為方陣。
torch.eye(n, m=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)3.依據(jù)概率分布創(chuàng)建張量
(1)torch.normal()
功能:返回隨機數(shù)的張量,這些隨機數(shù)是從給定平均值和標準差的獨立正態(tài)分布中提取的。
torch.normal(mean, std, *, generator=None, out=None)
有四種模式:
1.mean是標量,std是標量:這是只有一個高斯分布,這時需要指定張量的尺寸,即在這個高斯分布里采樣多少次。
2和3. mean和std中有一個是張量,一個是標量:
這里就是從四個高斯分布中分別采樣了一次得到的張量。
4. mean和std都為張量:
(2)torch.randn()
功能:生成標準正態(tài)分布,normal的特殊情況。
torch.randn(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) >>> torch.randn(4) tensor([-2.1436, 0.9966, 2.3426, -0.6366]) >>> torch.randn(2, 3) tensor([[ 1.5954, 2.8929, -1.0923],[ 1.1719, -0.4709, -0.1996]])(3)torch.randn_like()
torch.randn_like(input, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format)(4)torch.rand()
功能:生成[0, 1)之間均勻分布的隨機采樣張量。
torch.rand(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) >>> torch.rand(4) tensor([ 0.5204, 0.2503, 0.3525, 0.5673]) >>> torch.rand(2, 3) tensor([[ 0.8237, 0.5781, 0.6879],[ 0.3816, 0.7249, 0.0998]])(5)torch.rand_like()
torch.rand_like(input, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format)(6)torch.randint()
功能:生成[low, high)之間均勻分布的隨機采樣張量。
torch.randint(low=0, high, size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)(7)torch.randint_like()
torch.randint_like(input, low=0, high, dtype=None, layout=torch.strided, device=None, requires_grad=False, memory_format=torch.preserve_format)(8)torch.randperm()
功能:生成從0到n-1的隨機排列。
torch.randperm(n, out=None, dtype=torch.int64, layout=torch.strided, device=None, requires_grad=False)(9)torch.bernoulli()
功能:以input為概率,生成伯努利分布(0-1分布,兩點分布)。
torch.bernoulli(input, *, generator=None, out=None)總結(jié)
以上是生活随笔為你收集整理的PyTorch框架学习二——基本数据结构(张量)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: duilib中界面的布局方式
- 下一篇: 计算机视觉(一)概述