Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

10.3. Tensor 张量



Scalar(标量):指一个数值
Vector(向量):指一维数组
Matrix(矩阵):指二维数组
Tensor(张量):指大于二维的数组,即多位数组

10.3.1. 创建静态 Tensor

			
import torch

tensor = torch.tensor([5, 8, 3, 7, 2, 4, 1, 0, 6, 9])
print(tensor)			
			
			

10.3.2. Tensor 尺寸

			
import torch

tensor = torch.eye(2, 3)
print(tensor)
print(tensor.size())
print(tensor.size(0))
print(tensor.size(1))			
				
			
			
tensor([[1., 0., 0.],
        [0., 1., 0.]])
torch.Size([2, 3])
2
3			
			
			

10.3.3. 创建连续数列的 Tensor

			
import torch

tensor = torch.range(1, 10)

print(tensor)			
			
			

10.3.4. 创建0数据的 Tensor

创建一个 3x3 的 Tensor

			
import torch

x = torch.zeros(3, 3)
print(x)			
			
			

输出结果

			
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
			
			

10.3.5. 判断变量是否为 Tensor

			
import torch

x = [1, 2, 3, 4, 5]
print(torch.is_tensor(x))

x1 = torch.rand(1, 2)
print(torch.is_tensor(x1))			
			
			

输出结果

			
False
True			
			
			

10.3.6. 统计 Tensor 中的元素数量

			
import torch

x = torch.zeros(3, 3)
print(x)
print(torch.numel(x))	
			
			

10.3.7. 创建对角线为1的 Tensor

			
import torch

x = torch.eye(3, 3)
print(x)
print(torch.numel(x))			
			
			

输出结果

			
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])
9			
			
			

10.3.8. 将 numpy 转换成 tensor

			
import numpy
import torch

array = numpy.array([1, 2, 3, 4, 5])
print(array)
tensor = torch.from_numpy(array);
print(tensor)			
			
			

输出结果

			
[1 2 3 4 5]
tensor([1, 2, 3, 4, 5])
			
			

10.3.9. 切分

			
tensor = torch.linspace(1, 9, 9)
print(tensor)
			
			
			
tensor([1., 2., 3., 4., 5., 6., 7., 8., 9.])			
			
			

10.3.10. 均匀分布数列

返回值在0~1之间

			
tensor = torch.rand(10)
print(tensor)	
			
			
			
tensor([0.8865, 0.3672, 0.7740, 0.6729, 0.8264, 0.0811, 0.4243, 0.7992, 0.6984,
        0.1110])
			
			

10.3.11. 正态分布数列

返回均值0,方差为1

			
tensor = torch.randn(10)
print(tensor)			
			
			

			
tensor([-0.3631,  0.9288,  0.5677,  0.5674,  0.2578, -0.4731, -0.3581, -0.6288,
        -1.1815, -0.1885])			
			
			

10.3.12. 随机数列

			
import torch

tensor = torch.randperm(10)
			
			

输出结果

			
tensor([5, 8, 3, 6, 0, 7, 1, 4, 2, 9])
			
			
			
import torch

tensor = torch.randint(1, 10, (3, 3))
print(tensor)			
			
			

输出结果

			
tensor([[6, 1, 9],
        [4, 3, 6],
        [4, 8, 4]])
			
			

10.3.13. arange 创建等差数列

arange(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor

参数



start (数字) – 起始值。默认值:0。
end (数字) – 结束值
step (数字) – 每对相邻数字之间的步长。默认值:1。

关键字参数



out (张量, 可选) – 输出张量。
dtype (torch.dtype,可选) – 据类型。默认值:如果为 None,则使用全局默认值(请参阅 torch.set_default_dtype())。如果未提供 dtype,则从其他输入参数推断数据类型。如果 start、end 或 stop 中的任何一个是浮点数,则 dtype 推断为默认 dtype,请参阅 get_default_dtype()。否则,dtype 推断为 torch.int64。
layout (torch.layout,可选) – 布局。默认值:torch.strided。
device (torch.device,可选) – 目标设备。默认值:如果为None,则使用默认张量类型当前的设备(参见 torch.set_default_device())。对于 CPU 张量类型,device 将为 CPU;对于 CUDA 张量类型,device 将为当前的 CUDA 设备。
requires_grad (bool, 可选) – autograd 是否应记录返回张量上的操作。默认值:False。

		
>>> torch.arange(5)  # 默认以 0 为起点
tensor([ 0,  1,  2,  3,  4])
>>> torch.arange(1, 4)  # 默认间隔为 1
tensor([ 1,  2,  3])
>>> torch.arange(1, 2.5, 0.5)  # 指定间隔 0.5
tensor([ 1.0000,  1.5000,  2.0000])		
		
			

		
import torch
from torch.utils.data import Dataset, DataLoader
#
data = torch.arange(15)
print(data)		
		
			
		
tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])		
		
			
			
import torch

tensor = torch.arange(1, 10, 1)
print(tensor)
tensor = torch.arange(1, 10, 2)
print(tensor)
tensor = torch.arange(1, 10, 3)
print(tensor)
			
			

输出结果

			
tensor([1, 2, 3, 4, 5, 6, 7, 8, 9])
tensor([1, 3, 5, 7, 9])
tensor([1, 4, 7])			
			
			
		
data = torch.arange(15).reshape(5,3)
print(data)		
		
			
		
tensor([[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8],
        [ 9, 10, 11],
        [12, 13, 14]])		
		
			

10.3.14. 获取最小值和最大值的索引

			
import torch

tensor = torch.randint(1, 10, (3, 3))
print(tensor)
min = torch.argmin(tensor, dim=0)
print(min)
max = torch.argmax(tensor, dim=0)
print(max)			
			
			

输出结果

			
tensor([[5, 1, 4],
        [7, 7, 8],
        [2, 2, 9]])
tensor([2, 0, 0])
tensor([1, 1, 2])			
			
			

最小值返回索引 tensor([2, 0, 0]),2 表示列第三个,0表示第二列第一个,最后一个0表示第三列第一个,最终可以获得 2,1,4 三个数据

最大值返回索引 tensor([1, 1, 2]),对应数据 7,7,9

10.3.15. 连接两个 Tensor

垂直连续,追加数据

			
import torch

tensor1 = torch.randint(1, 10, (3, 3))
print(tensor1)

tensor2 = torch.randint(1, 10, (3, 3))
print(tensor2)

tensor = torch.cat((tensor1, tensor2))
print(tensor)			
			
			

输出结果

			
tensor([[1, 7, 5],
        [8, 6, 8],
        [6, 9, 2]])
tensor([[6, 5, 6],
        [4, 2, 2],
        [5, 9, 3]])
tensor([[1, 7, 5],
        [8, 6, 8],
        [6, 9, 2],
        [6, 5, 6],
        [4, 2, 2],
        [5, 9, 3]])			
			
			

水平连接,扩展数据

			
import torch

tensor1 = torch.randint(1, 10, (3, 3))
print(tensor1)

tensor2 = torch.randint(1, 10, (3, 3))
print(tensor2)

tensor = torch.cat((tensor1, tensor2), dim=1)
print(tensor)			
			
			

输出结果

			
tensor([[6, 1, 6],
        [9, 4, 9],
        [5, 7, 5]])
tensor([[5, 9, 9],
        [2, 3, 8],
        [5, 2, 8]])
tensor([[6, 1, 6, 5, 9, 9],
        [9, 4, 9, 2, 3, 8],
        [5, 7, 5, 5, 2, 8]])
			
			

10.3.16. 数据切块

			
import torch

tensor = torch.randint(1, 10, (3, 3))
print(tensor)

# 将 tensor 切成 3 份
tensor1 = torch.chunk(tensor, 3)
print(tensor1)

# 基于 Y 轴将 tensor 切成 3 份
tensor2 = torch.chunk(tensor, 3, dim=1)
print(tensor2)			
			
			

输出结果

			
tensor([[7, 5, 6],
        [3, 5, 5],
        [2, 1, 8]])
(tensor([[7, 5, 6]]), tensor([[3, 5, 5]]), tensor([[2, 1, 8]]))
(tensor([[7],
        [3],
        [2]]), tensor([[5],
        [5],
        [1]]), tensor([[6],
        [5],
        [8]]))			
			
			

10.3.17. 通过索引下标选择数据

			
import torch

x = torch.randn(6, 6)
print(x)
index = torch.tensor([1, 3, 5])

tensor = torch.index_select(input=x, dim=0, index=index)
print(tensor)			
			
			

输出结果

			
tensor([[ 0.2502,  0.0171,  0.0468, -1.1985,  0.1249, -0.2472],
        [-1.0763, -0.9561,  0.7500,  0.0903, -0.2404,  0.7940],
        [-0.2090, -1.1881,  0.6415, -1.8224, -1.2121,  0.0428],
        [-1.3796, -0.3021, -0.0945,  0.1808, -0.1802, -1.6069],
        [-1.0586,  0.0227, -0.4314, -0.4522, -0.6734,  0.0220],
        [-0.3171, -0.9069,  1.9850,  0.9845, -0.5600, -1.3951]])
tensor([[-1.0763, -0.9561,  0.7500,  0.0903, -0.2404,  0.7940],
        [-1.3796, -0.3021, -0.0945,  0.1808, -0.1802, -1.6069],
        [-0.3171, -0.9069,  1.9850,  0.9845, -0.5600, -1.3951]])			
			
			

10.3.18. 分割

			
import torch

tensor = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(tensor)
print(torch.split(tensor, 3))			
			
			
			
tensor([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
(tensor([1, 2, 3]), tensor([4, 5, 6]), tensor([7, 8, 9]), tensor([10]))			
			
			

10.3.19. 矩阵转换

行列转换

			
import torch

tensor = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(tensor)
print(tensor.t())
print(tensor.transpose(1, 0))
			
			

输出结果

			
tensor([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])
tensor([[1, 4, 7],
        [2, 5, 8],
        [3, 6, 9]])	
tensor([[1, 4, 7],
        [2, 5, 8],
        [3, 6, 9]])	        
			
			

10.3.20. 矩阵运算

			
import torch

tensor = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(tensor)
print(torch.add(tensor, 1))
print(torch.mul(tensor, 2))			
			
			
			
import torch 
a=torch.tensor([[1,2,3],[4,5,6]])
b=torch.tensor([[2,3,4],[5,6,7]])
c=torch.mul(a,b)
print('a:',a)
print('b:',b)
print('c:',c)			
			
			

10.3.21. view

			
import torch

tensor = torch.arange(9)
print(tensor)
print(tensor.size())
tmp = tensor.view(3, 3)
print(tmp)
print(tmp.size())			
			
			

tensor 转为 3x3 矩阵

			
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8])
torch.Size([9])
tensor([[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]])
torch.Size([3, 3])