【基礎】-tensor(張量)
一.創建一個tensor
1.只分配空間
x1 = torch.Tensor(5, 3) # 數據是隨機的,只是分配了可見,沒有初始化,所及數據是對應空間裏的數據
print(x1)
輸出:
tensor([[7.3470e-39, 8.9082e-39, 8.9082e-39],
[1.0194e-38, 9.1837e-39, 4.6837e-39],
[9.2755e-39, 1.0837e-38, 8.4490e-39],
[9.9184e-39, 9.9184e-39, 9.0000e-39],
[1.0561e-38, 1.0653e-38, 4.1327e-39]])
2.0-1分佈
x2 = torch.rand(5, 3) # 使用[0,1]分佈(默認的)隨機初始化數據,構建(5, 3)shape的數組
print(x2)
輸出:
tensor([[0.0786, 0.0817, 0.5277],
[0.3758, 0.9402, 0.2716],
[0.0723, 0.3258, 0.7880],
[0.9141, 0.7395, 0.1126],
[0.9843, 0.5128, 0.9107]])
3.直接創建
x3 = torch.tensor([ [1,2,3,4],
[5,6,7,8]])
print(x3)
輸出:
tensor([[1, 2, 3, 4],
[5, 6, 7, 8]])
4.同一個數值
x4 = torch.full((5, 3), 6)
print(x4)
輸出:
tensor([[6., 6., 6.],
[6., 6., 6.],
[6., 6., 6.],
[6., 6., 6.],
[6., 6., 6.]])
5.在一個區間內按一定步長
x5 = torch.arange(0, 100, 10) # 從0到99,按步長爲10進行生成
print(x5)
輸出:
tensor([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
或者:
在一定區間內均勻生成多少份,步長自動計算
x6 = torch.linspace(0, 100, 10) # 在0到100均勻生成10份
print(x6)
輸出:
tensor([ 0.0000, 11.1111, 22.2222, 33.3333, 44.4444, 55.5556, 66.6667,
77.7778, 88.8889, 100.0000])
這裏可見看出來使用torch.linspace包含end,0到100分成10個數,相對於在0到100的線段上均勻定9個點,100/9 = 11.111111······,所以,步長爲11.11111.
如果生成11份,步長就爲10了。
輸出:
tensor([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100.])
6.特殊矩陣
# 特殊矩陣
x7 = torch.zeros(5, 3) # 全0矩陣
print(x7)
x8 = torch.ones(5, 3) # 全1矩陣
print(x8)
x9 = torch.eye(3, 3) # 單位陣
print(x9)
x10 = torch.eye(5, 3) # 當輸入shape不是方陣時,一部分是單位陣,多出來的部分爲0
print(x10)
輸出:
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.],
[0., 0., 0.]])
7.生成一個與已知tensor的shape相同的tensor
x11 = torch.rand_like(x10)
print(x11)
輸出:
tensor([[0.2761, 0.7960, 0.0800],
[0.3035, 0.3525, 0.5574],
[0.3570, 0.5426, 0.4790],
[0.3504, 0.3996, 0.1984],
[0.5939, 0.3260, 0.6721]])
8.從numpy轉
x12 = np.random.rand(5, 3)
print(x12)
x12 = torch.from_numpy(x12)
print(x12)
輸出:
[[0.14858865 0.18512316 0.97759539]
[0.96559993 0.75191884 0.1561388 ]
[0.71575248 0.88542421 0.29086326]
[0.67362585 0.00512253 0.34022816]
[0.69759491 0.25110932 0.71962754]]
tensor([[0.1486, 0.1851, 0.9776],
[0.9656, 0.7519, 0.1561],
[0.7158, 0.8854, 0.2909],
[0.6736, 0.0051, 0.3402],
[0.6976, 0.2511, 0.7196]], dtype=torch.float64)
二.tensor的屬性
x2 = torch.rand(5, 3) # 使用[0,1]分佈(默認的)隨機初始化數據,構建(5, 3)shape的數組
print(x2)
print(x2.size()) # (列,行)
print(x2.shape) # (列, 行)
print(x2.dtype) # 數據類型
輸出:
tensor([[0.6725, 0.6270, 0.0352],
[0.0420, 0.4865, 0.7263],
[0.9950, 0.3957, 0.3868],
[0.3802, 0.3337, 0.0465],
[0.0089, 0.7211, 0.1279]])
torch.Size([5, 3])
torch.Size([5, 3])
torch.float32
三.tensor的操作
1.加
import torch
x1 = torch.randint(0, 10, (5, 3), dtype=torch.float)
print(x1)
x2 = torch.eye(5, 3)
print(x2)
# 1
print(x1 + x2)
# 2
print(torch.add(x1, x2))
# 3
print(x1.add(x2))
# 4
sum = torch.Tensor(5, 3) # 預先分配空間
torch.add(x1, x2, out=sum) # 加的結果保存在sum裏
print(sum)
輸出:
tensor([[5., 5., 8.],
[9., 3., 8.],
[9., 8., 3.],
[5., 5., 6.],
[6., 9., 5.]])
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.],
[0., 0., 0.]])
tensor([[6., 5., 8.],
[9., 4., 8.],
[9., 8., 4.],
[5., 5., 6.],
[6., 9., 5.]])
tensor([[6., 5., 8.],
[9., 4., 8.],
[9., 8., 4.],
[5., 5., 6.],
[6., 9., 5.]])
tensor([[6., 5., 8.],
[9., 4., 8.],
[9., 8., 4.],
[5., 5., 6.],
[6., 9., 5.]])
tensor([[6., 5., 8.],
[9., 4., 8.],
[9., 8., 4.],
[5., 5., 6.],
[6., 9., 5.]])
tensor1.add()和tensor1.add_()的區別
這個算是和python的特性一致,不加下劃線的第一種就是普通的加法,不會改變tensor1的內容。而加了下劃線,即第二種,加的結果會賦值給tensor1。
例子:
print(x1.add(x2))
print(x1)
print(x1.add_(x2))
print(x1)
輸出:
tensor([[5., 8., 9.],
[2., 4., 3.],
[6., 2., 9.],
[3., 1., 3.],
[8., 5., 9.]])
tensor([[4., 8., 9.],
[2., 3., 3.],
[6., 2., 8.],
[3., 1., 3.],
[8., 5., 9.]])
tensor([[5., 8., 9.],
[2., 4., 3.],
[6., 2., 9.],
[3., 1., 3.],
[8., 5., 9.]])
tensor([[5., 8., 9.],
[2., 4., 3.],
[6., 2., 9.],
[3., 1., 3.],
[8., 5., 9.]])
其餘運算
其他的減法,乘法,除法均可使用:-,*,/。同時也可以使用
torch.sub()
減法torch.mul()
乘法,只是簡單的對應位置相乘,不是矩陣的乘法torch.div()
除法,對應位置相除
或者
tensor1.sub(tensor2)
tensor1.mul(tensor2)
tensor.div(tensor2)
上面三種加也可以加下劃線,和add的例子相同。
其中,如果被除數爲0,會出現這樣的結果:
tensor([[9., inf, inf],
[inf, 6., inf],
[inf, inf, 0.],
[nan, inf, inf],
[inf, inf, inf]])
上面的三種運算中torch.sub()
這種也都可以添加out=
.
此外,還有很多其他的運算,基本都比較簡單。
可能的一些錯誤
RuntimeError: expected backend CPU and dtype Float but got backend CPU and dtype Long
這種情況,根據提示信息就是數據的類型不對。
一般可以在創建tensor的時候添加dtype=torch.float
類似的語句來改。也可以使用tensor1.float()
來修改
查看tensor的數據類型時:使用tensor1.dtype