Pytorch Tensor的性質及生成方法
1.torch.rand()
torch。rand(*size, out=None) -> Tensor
作用:返回一個從區間 [0,1)的均勻分佈中抽取的一組隨機數,張量的形狀由引數sizes定義
引數
sizes 整數序列,定義了輸出張量的形狀
out 結果張量
Example
torch。rand(2,3)
tensor([[0。1371, 0。0177, 0。5417],
[0。6575, 0。6141, 0。9619]])
2.torch.randn()
torch。randn(*size, out=None) -> Tensor
作用:返回一個張量,包含了標準正態分佈 (均值為0,方差為1,即高斯白噪聲) 中抽取的一組隨機數,張量的形狀由引數size定義
引數:
size (int) 整數序列,定義了輸出張量的形狀
out (Tensor, optinal) 結果張量
Example
>>> torch。randn(2, 3)
tensor([[ 0。1933, 0。2193, -0。7458],
[ 0。5407, -0。9313, 0。1914]])
3.torch.normal()
torch。normal(means, std, out=None) -> Tensor
作用:返回一個張量,包含了從指定均值means和標準差std的離散正態分佈中抽取的一組隨機數,標準差std是一個張量,包含每個輸出元素相關的正態分佈標準差
引數:
means (float, optinal) 均值
std (Tensor) 標準差
out (Tensor) 輸出張量
Example
>>> torch。normal(mean=0。5, std=torch。arange(1。, 6。))
tensor([ 0。7295, -0。9491, -1。9996, 4。0057, -2。6932])
4.torch.linspace()
torch。linspace(start, end, steps =100, out = None) -> Tensor
作用:返回一個一維張量,包含在區間start和end上均勻間隔的step個點,輸出張量的長度由step決定
引數:
start (float) 區間的起始點
end (float) 區間的終點
steps (int) 在start和end間生成的樣本數
out ( Tensor, optimal) 結果張量
Example
>>> torch。linspace(3, 10, steps=5)
tensor([ 3。0000, 4。7500, 6。5000, 8。2500, 10。0000])
5.torch.Tensor()
Example:
>>> torch。Tensor([1,2,3,4])
tensor([1。, 2。, 3。, 4。])
6。torch。Tensor(size)
Example
>>> torch。Tensor(2,3)
tensor([[0。0000e+00, 0。0000e+00, 1。8754e+28],
[1。0396e-05, 4。1760e+21, 1。2926e-11]])
7.torch.Tensor(sequence)
Example
>>> torch。Tensor([1,2,3,4,5,6])
tensor([1。, 2。, 3。, 4。, 5。, 6。])
8. torch.eye(n)
Example
>>> torch。eye(3)
tensor([[1。, 0。, 0。],
[0。, 1。, 0。],
[0。, 0。, 1。]])
9. torch.from_numpy(ndarray)
Example
>>> import numpy as np
>>> a = np。array([1,2,3,4,5,6])
>>> torch。from_numpy(a)
tensor([1, 2, 3, 4, 5, 6], dtype=torch。int32)
10. Tensor attributes
Tensor attributes中有三個類:
torch。dtype 展示torch。Tensor資料型別的類,pytorch有八種不同的資料型別,下表是完整的dtype列表
torch。device 展示torch。Tensor被分配的裝置型別的類,其中分為‘cpu’和 ‘cuda’兩種,如果裝置序號沒有顯示則表示此tensor被分配到當前裝置,比如‘cuda’等同於‘cuda’:X, X為torch。cuda。current_device()返回值
torch。layout 表現torch。Tensor記憶體分佈的類,目前只支援torch。strided
11. 建立特定的tensor
torch。zeros(*size, out=None, 。。。) #返回大小為sizes的零矩陣
>>> torch。zeros(2,3)
tensor([[0。, 0。, 0。],
[0。, 0。, 0。]])
torch。zeros_like(input,。。。) #返回與input相同size的零矩陣
>>> a = torch。Tensor([1,2,3,4,5])
>>> torch。zeros_like(a)
tensor([0。, 0。, 0。, 0。, 0。])
torch。ones(*size, out=None, 。。。) #返回大小為sizes的單位矩陣
>>> torch。ones(2,3)
tensor([[1。, 1。, 1。],
[1。, 1。, 1。]])
torch。ones_like(input, 。。。) #返回與input相同size的單位矩陣
>>> a = torch。Tensor([[1,2,3],[4,5,6]])
>>> torch。ones_like(a)
tensor([[1。, 1。, 1。],
[1。, 1。, 1。]])
torch。full(size, fill_value, 。。。) #返回大小為sizes,單位值為fill_value的矩陣
>>> torch。full([2,3],100)
tensor([[100, 100, 100],
[100, 100, 100]])
torch。full_like(input, fill_value, 。。。) #返回input相同size,單位值為fill_value的矩陣
>>> a = torch。full([2,3],100)
>>> torch。full_like(a,88)
tensor([[88, 88, 88],
[88, 88, 88]])
torch。arange(start = 0, end, step =1, 。。。) #返回從start到end,單位步長為step的1-d tensor
>>> torch。arange(start = 0, end=8, step =1)
tensor([0, 1, 2, 3, 4, 5, 6, 7])
torch。linspace(start, end, steps=100, 。。。) #返回從start到end,間隔中的插值數目為steps的1-d tensor
>>> torch。linspace(start=0, end=10, steps=1)
tensor([0。])
>>> torch。linspace(start=0, end=10, steps=2)
tensor([ 0。, 10。])
>>> torch。linspace(start=0, end=10, steps=3)
tensor([ 0。, 5。, 10。])
>>> torch。linspace(start=0, end=10, steps=4)
tensor([ 0。0000, 3。3333, 6。6667, 10。0000])
>>> torch。linspace(start=0, end=10, steps=5)
tensor([ 0。0000, 2。5000, 5。0000, 7。5000, 10。0000])
torch。logspace(start, end, steps =100, 。。。) #返回1-d tensor, 從10^start到10^end的steps個對數間隔
>>> torch。logspace(0, 10, steps =10)
tensor([1。0000e+00, 1。2915e+01, 1。6681e+02, 2。1544e+03, 2。7826e+04, 3。5938e+05,
4。6416e+06, 5。9948e+07, 7。7426e+08, 1。0000e+10])
>>> torch。logspace(0, 10, steps =20)
tensor([1。0000e+00, 3。3598e+00, 1。1288e+01, 3。7927e+01, 1。2743e+02, 4。2813e+02,
1。4384e+03, 4。8329e+03, 1。6238e+04, 5。4556e+04, 1。8330e+05, 6。1585e+05,
2。0691e+06, 6。9519e+06, 2。3357e+07, 7。8476e+07, 2。6367e+08, 8。8587e+08,
2。9764e+09, 1。0000e+10])
12. 建立特定的矩陣
torch。eye(n, m=None, out=None, 。。。) #返回2-D的單位對角矩陣
>>> torch。eye(4)
tensor([[1。, 0。, 0。, 0。],
[0。, 1。, 0。, 0。],
[0。, 0。, 1。, 0。],
[0。, 0。, 0。, 1。]])
torch。empty(*size, out=None, 。。。) #但會被未被初始化的數值填充,大小為sizes的tensor
>>> torch。empty(3,4)
tensor([[0。, 0。, 0。, 0。],
[0。, 0。, 0。, 0。],
[0。, 0。, 0。, 0。]])
torch。empty_like(input, 。。。) #返回與input相同 size, 並被未初始化的數值填充的tensor
>>> a = torch。Tensor([[1,2,3],[4,5,6]])
>>> torch。empty_like(a)
tensor([[0。, 0。, 0。],
[0。, 0。, 0。]])
14. 隨機生成
torch。normal(mean, std, out=None)
>>> torch。normal(mean=0。5, std=torch。arange(1。, 6。))
tensor([ 0。7295, -0。9491, -1。9996, 4。0057, -2。6932])
torch。rand(*size, out=None, dtype=None, 。。。) #返回[0,1]之間均勻分佈的隨機數值
>>> torch。rand(2,3)
tensor([[0。3537, 0。9894, 0。0231],
[0。3032, 0。1898, 0。9811]])
torch。rand_like(input, dtype=None, 。。。) #返回與input相同size的tensor,填充均勻分佈的隨機數值
>>> a = torch。rand(2,3)
tensor([[0。2030, 0。7614, 0。6949],
[0。7133, 0。3333, 0。4325]])
>>> torch。rand_like(a)
tensor([[0。7307, 0。7065, 0。8321],
[0。2001, 0。8140, 0。3055]])
torch。randint(low=0, high, size, 。。。) #返回均勻分佈的[low,high]之間的整數隨機值
>>> torch。randint(low=0, high=10, size=[2,5])
tensor([[4, 4, 0, 3, 5],
[9, 5, 6, 9, 0]])
torch。randint_like(input, low=0, high, dtype=None, 。。。)
>>> a = torch。randint(low=0, high=10, size=[2,5])
tensor([[4, 8, 4, 5, 8],
[5, 9, 8, 3, 8]])
>>> torch。randint_like(a, low=10, high=20)
tensor([[14, 18, 16, 19, 15],
[10, 14, 10, 13, 17]])
torch。randn(*sizes, out=None, 。。。) #返回大小為size,由均值為0,方差為1的正態分佈的隨機數值
>>> torch。randn(2,3)
tensor([[ 0。6346, 0。0708, 1。4831],
[ 0。1756, 0。3013, -2。2280]])
torch。randn_like(input,dtype=None,。。。)
>>> a = torch。randn(2,3)
tensor([[ 0。2087, 0。3215, -0。7790],
[-1。0760, -1。1548, -1。1065]])
>>> torch。randn_like(a)
tensor([[ 0。2261, -0。2747, -0。5213],
[-1。7855, -0。2093, -0。0597]])
torch。randperm(n,out=None, dtype=torch。int64) #返回0到n-1的數列的隨機值
>>> torch。randperm(10,out=None, dtype=torch。int64)
>>> tensor([9, 5, 2, 0, 1, 3, 8, 4, 6, 7])
Reference
PyTorch 生成隨機數Tensor的方法彙總
zcyanqiu:pytorch入坑一 | Tensor及其基本操作