本文共 5635 字,大约阅读时间需要 18 分钟。
张量是标量、向量、矩阵的高维拓展,统一了多维数据的表示方式,是PyTorch中核心的数据结构。
在PyTorch中,Tensor是核心的数据类型,而Variable则是用于封装Tensor并支持梯度计算的数据结构。Variable主要包含以下属性:
从PyTorch 0.4.0版本开始,Variable与Tensor合并,Tensor已经内置了Variable的功能。Tensor的主要属性包括:
PyTorch提供了多种方法来创建张量:
import torchimport numpy as np# 创建Tensor并移动到GPUarr = np.ones((3, 3))t = torch.tensor(arr, device='cuda')print(t) # 输出: tensor([[1., 1., 1.], ...], device='cuda:0', dtype=torch.float64)
arr = np.array([[1, 2, 3], [4, 5, 6]])t = torch.from_numpy(arr)# 修改arr时,t也会随之改变arr[0, 0] = 0print(arr) # numpy array: [[0, 2, 3], ...]print(t) # tensor: tensor([[0, 2, 3], ...], dtype=torch.int32)
PyTorch提供了多种方法依据数值创建张量:
t = torch.zeros((3, 3))print(t) # tensor([[0., 0., 0.], ...], dtype=torch.float64)
t = torch.full((3, 3), 1)print(t) # tensor([[1., 1., 1.], ..., dtype=torch.float64)
t = torch.arange(2, 10, 2)print(t) # tensor([2, 4, 6, 8])
t = torch.linspace(2, 10, 5)print(t) # tensor([2., 4., 6., 8., 10.])
t = torch.logspace(0, 10, 100, base=10)print(t) # tensor([1., 10., 100., ...])
t = torch.eye(2, 2)print(t) # tensor([[1., 0.], [0., 1.]], dtype=torch.float64)
PyTorch提供了多种方法依据概率分布创建张量:
mean = torch.arange(1, 5, dtype=torch.float)std = torch.arange(1, 5, dtype=torch.float)t_normal = torch.normal(mean, std)print(mean, std, t_normal) # 输出: mean: tensor([1., 2., 3., 4.]), std: tensor([1., 2., 3., 4.]), t_normal: tensor([...])
t = torch.rand((3, 3))print(t) # tensor([[0.682..., 0.682..., ...], ...], dtype=torch.float64)
t = torch.randint(0, 9, (3, 3))print(t) # tensor([[3, 8, 8], ..., dtype=torch.long)
t = torch.bernoulli(torch.tensor([0.5, 0.3, 0.7]))print(t) # tensor([1, 0, 1], dtype=torch.long)
t = torch.randperm(8)print(t) # tensor([0, 6, 4, 7, 2, 5, 1, 3])
PyTorch提供了多种方法进行张量的拼接和切分:
t = torch.ones((2, 3))t0 = torch.cat([t, t], dim=0)t1 = torch.cat([t, t], dim=1)print(t0, t1) # 输出: tensor([[1., 1., 1.], ...], shape: [4, 3]), tensor([[1., 1., 1., 1., 1., 1.], ...], shape: [2, 6])
t = torch.stack([t, t, t], dim=0)print(t) # tensor([[[1., 1., 1.], [1., 1., 1.]], ...], shape: [3, 2, 3])
t = torch.ones((2, 5))list_of_tensors = torch.chunk(t, dim=1, chunks=2)for idx, t in enumerate(list_of_tensors): print(t.shape)# 输出: shape: [2, 3], [2, 2], [2, 1]
t = torch.ones((2, 5))list_of_tensors = torch.split(t, 2, dim=1)for idx, t in enumerate(list_of_tensors): print(t.shape)# 输出: shape: [2, 2], [2, 2], [2, 1]
PyTorch提供了多种方法进行张量的索引操作:
t = torch.randint(0, 9, (3, 3))idx = torch.tensor([0, 2], dtype=torch.long)t_select = torch.index_select(t, dim=1, index=idx)print(t, t_select) # 输出: tensor([[5, 8, 8], ...], tensor([[5, 8], ..., [1, 3]])
t = torch.randint(0, 9, (3, 3))mask = t.le(5)t_select = torch.masked_select(t, mask)print(t, t_select) # 输出: tensor([[5, 8, 8], ...], tensor([5, 8, 8]))
PyTorch提供了多种方法进行张量的变换:
t = torch.randperm(8)t_reshape = torch.reshape(t, (-1, 2, 2))print(t, t_reshape) # 输出: tensor([...], shape: [8]), tensor([...], shape: [2, 4])
t = torch.rand((2, 3, 4))t_transpose = torch.transpose(t, 1, 2)print(t, t_transpose) # 输出: tensor([...], shape: [2, 3, 4]), tensor([...], shape: [2, 4, 3])
t = torch.rand((1, 2))t_t = torch.t(t)print(t, t_t) # 输出: tensor([...], shape: [2, 1]), tensor([...], shape: [1, 2])
t = torch.rand((1, 2, 3, 1))t_sq = torch.squeeze(t)t_sq1 = torch.squeeze(t, dim=0)t_sq2 = torch.squeeze(t, dim=1)print(t.shape, t_sq.shape, t_sq1.shape, t_sq2.shape) # 输出: (1, 2, 3, 1), (2, 3), (2, 3, 1), (1, 2, 3, 1)
t = torch.rand((2, 3))t_unsqueezed = torch.unsqueeze(t, dim=1)print(t.shape, t_unsqueezed.shape) # 输出: (2, 3), (2, 1, 3)
PyTorch提供了丰富的数学运算功能,包括加减乘除、对数、指数、幂函数、三角函数等。以下是常用方法示例:
这些操作都可以按元素方式进行,适用于张量计算。
转载地址:http://iedo.baihongyu.com/