PyTorch 介绍 | TENSORS

Tensor是一种特殊的数据结构,非常类似于数组和矩阵。在PyTorch中,我们使用tensor编码模型的输入和输出,以及模型的参数。

Tensor类似于Numpy的ndarrays,除了tensor能在GPUs或其它硬件加速器上运行。事实上,tensor和NumPy数组可以共享相同的底层内存,而不需要拷贝数据(see Bridge with NumPy)。Tensor还被优化用于自动微分(Autograd)。如果你熟悉ndarray,那么你也将对Tensor API也很熟悉,如果不是,跟着学吧!

import torch
import numpy as np

初始化Tensor

Tensor初始化有多种方式。查看下面的例子。

直接从数据初始化

Tensor可以直接利用data创建。数据类型是自动判断的。

data = [[1, 2], [3, 4]]
x_data = torch.tesnor(data)

从Numpy array初始化

Tensor可以从NumPy array初始化,反之亦可,见Bridge with NumPy

np_array = np.array(data)
x_np = torch.from_numpy(np_array)

从另一个tensor初始化

新的tensor保留参数tensor的属性(shape, datatype),除非明确重写。

x_ones = torch.ones_like(x_data) # 保留x_data的属性
print(f"Ones Tensor: \n {x_ones} \n")

x_rand = torch.rand_like(x_data, dtype=torch.float) # 重写x_data的数据类型
print(f"Random Tensor: \n {x_rand} \n")

输出:

Ones Tensor:
 tensor([[1, 1],
        [1, 1]])

Random Tensor:
 tensor([[0.4557, 0.7406],
        [0.5935, 0.1859]])

使用随机值或常数初始化

shape 是表示tensor维度的元组。在下面的函数中,它决定了输出tensor的维度

shape = (2, 3,)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeroat32s(shape)

print(f"Random Tensor: \n {rand_tensor} \n")
print(f"Ones Tensor: \n {ones_tensor} \n")
print(f"Zeros Tensor: \n {zeros_tensor}")

输出:

Random Tensor:
 tensor([[0.8012, 0.4547, 0.4156],
        [0.6645, 0.1763, 0.3860]])

Ones Tensor:
 tensor([[1., 1., 1.],
        [1., 1., 1.]])

Zeros Tensor:
 tensor([[0., 0., 0.],
        [0., 0., 0.]])

Tensor的属性

Tensor的属性描述了它们的shape,datatype,以及保存的硬件

tensor = torch.rand(3, 4)

print(f"Shape of tensor: {tensor.shape}")
print(f"Dataype of tenor: {tensor.dtype}")
print(f"Device tenor is stored on: {tenosr.device}")

输出:

Shape of tensor: torch.Size([3, 4])
Datatype of tensor: torch.float32
Device tensor is stored on: cpu

Tensor的操作

有超过100种的tensor操作,包括运算、线性代数、矩阵操作(转置、索引、切片),抽样,更多有关tensor操作的全面描述见here

每一种操作都可以在GPU(通常速度快于CPU)运行。

默认情况下,tensor是在CPU上创建的。我们需要使用 .to 方法明确地将tensor移动到GPU(在检查GPU可用后)。注意,跨设备复制大型的tensor需要花费大量的时间和内存。

# We move our tensor to the GPU if available
if torch.cuda.is_available():
    tensor = tensor.to('cuda')

尝试list的一些操作。如果你熟悉NumPy API,你会发现Tensor API使用也会很容易。

标准的类似于numpy的indexing和slicing

tensor = torch.ones(4, 4)
print('First row: ', tensor[0])
print('First column: ', tensor[:, 0])
print('Last column: ', tenosr[..., -1])
tensor[:, 1] = 0
print(tensor)

输出:

First row:  tensor([1., 1., 1., 1.])
First column:  tensor([1., 1., 1., 1.])
Last column: tensor([1., 1., 1., 1.])
tensor([[1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.]])

tensor拼接

你可以使用 torch.cat 沿着给定维度拼接一系列的tensor。还有torch.stack,它是另一个tensor拼接操作,与 torch.cat 有细微的差别。

t1 = torch.cat([tensor, tensor, tensor], dim=1)
print(t1)

输出:

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

数学运算

# 计算tensor的矩阵乘法. y1,y2,y3是相同的
y1 = tensor @ tensor.T
y2 = tensor.matmul(tensor.T)

y3 = torch.rand_like(tensor)
torch.matmul(tensor, tensor.T, out=y3)

# 逐像素相乘. z1,z2,z3是相同的
z1 = tensor * tensor
z2 = tensor.mul(tensor)

z3 = torch.rand_like(tensor)
torch.mul(tensor, tensor, out=z3)

单元素tensor

如果是单元素tensor,例如把一个tensor的所有值聚合一个,你可以使用 item() 将其转换成Python数值

12.0 <class 'float'>

In-place operations

In-place operations 会将结果保存到in-place操作数中。它们用下缀 _ 表示,例如: x.copy()x.t_(),将会改变 x

print(tensor, '\n')
tensor.add_(5)
print(tensor)

输出:

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

tensor([[6., 5., 6., 6.],
        [6., 5., 6., 6.],
        [6., 5., 6., 6.],
        [6., 5., 6., 6.]])

注意:In-place操作节约内存,但是在计算导数时可能会出问题,因为会立即丢失历史值。因此,不推荐使用。

Bridge with NumPy

CPU上的Tensor和NumPy数组共享底层内存位置,改变其中一个将改变另一个。

Tensor to NumPy array

t = torch.ones(5)
print(f"t: {t}")
n = t.numpy()
print(f"t: {n}")

输出:

t: tensor([1., 1., 1., 1., 1.])
n: [1. 1. 1. 1. 1.]

在tensor上发生的改变将会反应在NumPy数组上。

t.add_(1)
print(f"t: {t}")
print(f"n: {n}")

输出:

t: tensor([2., 2., 2., 2., 2.])
n: [2. 2. 2. 2. 2.]

NumPy array to Tensor

n = np.ones(5)
t = torch.from_numy(n)

在NumPy数组上发生的改变也会反应在Tensor上:

np.add(n, 1, out=n)
print(f"t: {t}")
print(f"n: {n}")

输出:

t: tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
n: [2. 2. 2. 2. 2.]
posted @ 2022-01-25 16:51  R-DeepH  阅读(318)  评论(0编辑  收藏  举报