Tensor基礎操作
簡單的初始化
import torch as t Tensor基礎操作 # 構建張量空間,不初始化 x = t.Tensor(5,3) x
-2.4365e-20 -1.4335e-03 -2.4290e+25 -1.0283e-13 -2.8296e-07 -2.0769e+22 -1.3816e-33 -6.4672e-32 1.4497e-32 1.6020e-19 6.2625e+22 4.7428e+30 4.0095e-08 1.1943e-32 -3.5308e+35 [torch.FloatTensor of size 5x3]
# 構建張量空間,[0,1]均勻分布初始化 x = t.rand(5,3) x
0.9618 0.0669 0.1458 0.3154 0.0680 0.1883 0.1795 0.4173 0.0395 0.7673 0.4906 0.6148 0.0949 0.2366 0.7571 [torch.FloatTensor of size 5x3
檢查尺寸
# 查看矩陣形狀,返回時tuple的子類,可以直接索引 print(x.shape) print(x.size()) """ torch.Size([5, 3]) torch.Size([5, 3]) """
Tensor加法操作
- 符號加
- torch.add(out=Tensor)
- Tensor.add(),方法后面不帶有下划線時方法不會修改Tensor本身,僅僅返回新的值
- Tensor.add_(),方法后面帶有下划線時方法會修改Tensor本身,同時返回新的值
Tensor加法操作 # 加法操作:t.add() y = t.rand(5,3) print(x + y) print(t.add(x, y)) result = t.Tensor(5,3) t.add(x, y, out=result) print(result)
輸出,
1.6288 0.4566 0.9290 0.5943 0.4722 0.7359 0.4316 1.0932 0.7476 1.6499 1.3201 1.5611 0.3274 0.4651 1.5257 [torch.FloatTensor of size 5x3] 1.6288 0.4566 0.9290 0.5943 0.4722 0.7359 0.4316 1.0932 0.7476 1.6499 1.3201 1.5611 0.3274 0.4651 1.5257 [torch.FloatTensor of size 5x3] 1.6288 0.4566 0.9290 0.5943 0.4722 0.7359 0.4316 1.0932 0.7476 1.6499 1.3201 1.5611 0.3274 0.4651 1.5257 [torch.FloatTensor of size 5x3]
輸入:
# 加法操作:Tensor自帶方法 print(y) # 不改變y本身 print("y.add():\n", y.add(x)) print(y) print("y.add_():\n", y.add_(x)) print(y)
輸出,
Tensor索引以及和Numpy.array轉換
Tensor對象和numpy的array對象高度相似,不僅可以相互轉換,而且:
- 轉換前后的兩者共享內存,所以他們之間的轉換很快,而且幾乎不會消耗資源,這意味着一個改變另一個也隨之改變
- 兩者在調用時可以相互取代(應該是由於兩者的內置方法高度相似)
雖然有Tensor.numpy()和t.from_numpy(),但是記不住的話使用np.array(Tensor)和t.Tensor(array)即可,同樣可以共享內存。
Tensor索引
# Tensor索引和numpy的array類似 x[:, 1]
0.0669 0.0680 0.4173 0.4906 0.2366 [torch.FloatTensor of size 5]
Tensor->array
Tensor和numpy轉換 a = t.ones_like(x) b = a.numpy() # Tensor->array b
Tensor和array的交互
import numpy as np print(x) # Tensor和array的交互很強,一定程度上可以相互替代 a = np.ones_like(x) print(a)
0.9618 0.0669 0.1458 0.3154 0.0680 0.1883 0.1795 0.4173 0.0395 0.7673 0.4906 0.6148 0.0949 0.2366 0.7571 [torch.FloatTensor of size 5x3] [[ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.]]
array->Tensor(兩者共享內存的驗證)
b = t.from_numpy(a) # array->Tensor print(a) print(b) b.add_(1) # 兩者共享內存 print(a) print(b)
[[ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.]] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [torch.FloatTensor of size 5x3] [[ 2. 2. 2.] [ 2. 2. 2.] [ 2. 2. 2.] [ 2. 2. 2.] [ 2. 2. 2.]] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 [torch.FloatTensor of size 5x3]
試驗np.array(Tensor)和t.Tensor(array),
import numpy as np x = t.rand(5,3) # Tensor和array的交互很強,一定程度上可以相互替代 a = np.ones_like(x) print(a) b = t.Tensor(a) # array->Tensor print(a) print(b) b.add_(1) # 兩者共享內存 print(a) print(b) print(np.array(x))
[[ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.]] [[ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.]] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [torch.FloatTensor of size 5x3] [[ 2. 2. 2.] [ 2. 2. 2.] [ 2. 2. 2.] [ 2. 2. 2.] [ 2. 2. 2.]] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 [torch.FloatTensor of size 5x3] [[ 0.95334041 0.48346853 0.86516887] [ 0.0904668 0.05142063 0.42738861] [ 0.7112515 0.45674682 0.39708138] [ 0.06700033 0.90959501 0.4757393 ] [ 0.6760695 0.83767009 0.1341657 ]]
最后,實驗以下cpu加速,當然,由於我的筆記本沒有加速,所以條件是不滿足的。
if t.cuda.is_available(): x = x.cuda() y = y.cuda() x+y