英文原文:Deep Learning with PyTorch: A 60 Minute Blitz
作者: Soumith Chintala
原文地址:Deep Learning with PyTorch: A 60 Minute Blitz
中文译文:YukonKong/Chinese-version.Deep-Learning-with-PyTorch-A-60-Minute-Blitz
推荐下载此仓库中的 jupyter notebook 文件使用。
# 原文地址:https://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html
# 翻译转载:https://github.com/YukonKong/Chinese-version.Deep-Learning-with-PyTorch-A-60-Minute-Blitz
Tensors (张量)¶
Tensors are a specialized data structure that are very similar to arrays and matrices. In PyTorch, we use tensors to encode the inputs and outputs of a model, as well as the model's parameters.
张量是一种特殊的数据结构,与数组和矩阵非常相似。在 PyTorch 中,我们使用张量来编码模型的输入和输出,以及模型的参数。
Tensors are similar to NumPy's ndarrays, except that tensors can run on GPUs or other specialized hardware to accelerate computing. If you're familiar with ndarrays, you'll be right at home with the Tensor API. If not, follow along in this quick API walkthrough.
张量类似于 NumPy 的 ndarrays,但张量可以在 GPU 或其他专用硬件上运行以加速计算。如果您熟悉 ndarrays,您将很快适应 Tensor API。如果不熟悉,请跟随本快速 API 演示。
import torch
import numpy as np
Tensor Initialization (张量初始化)¶
Tensors can be initialized in various ways. Take a look at the following examples:
张量可以使用多种方式初始化。请看以下示例:
Directly from data (直接从数据创建)
Tensors can be created directly from data. The data type is automatically inferred.
张量可以直接从数据创建。数据类型会自动推断。
data = [[1, 2], [3, 4]]
x_data = torch.tensor(data)
From a NumPy array (从 NumPy 数组创建)
Tensors can be created from NumPy arrays (and vice versa - see
bridge-to-np-label
{.interpreted-text role="ref"}).
张量可以从 NumPy 数组创建。(反之亦然 - 见 bridge-to-np-label )
np_array = np.array(data)
x_np = torch.from_numpy(np_array)
From another tensor (从另一个张量创建):
The new tensor retains the properties (shape, datatype) of the argument tensor, unless explicitly overridden.
新的张量保留了参数张量的属性(形状、数据类型),除非明确给出覆盖参数的语句。
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.6377, 0.7891], [0.3292, 0.9347]])
With random or constant values (从随机数或常数创建:):
shape
is a tuple of tensor dimensions. In the functions below, it
determines the dimensionality of the output tensor.
shape 是一个记录张量维度的元组。在下面的函数中,它确定输出张量的维度。
shape = (2, 3,)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(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.8125, 0.5069, 0.8712], [0.0385, 0.4978, 0.3444]]) Ones Tensor: tensor([[1., 1., 1.], [1., 1., 1.]]) Zeros Tensor: tensor([[0., 0., 0.], [0., 0., 0.]])
Tensor Attributes (张量属性)¶
Tensor attributes describe their shape, datatype, and the device on which they are stored.
张量属性描述了它们的形状、数据类型以及存储它们的设备(CPU/GPU)。
tensor = torch.rand(3, 4)
print(f"Shape of tensor: {tensor.shape}")
print(f"Datatype of tensor: {tensor.dtype}")
print(f"Device tensor is stored on: {tensor.device}")
Shape of tensor: torch.Size([3, 4]) Datatype of tensor: torch.float32 Device tensor is stored on: cpu
Tensor Operations 张量运算¶
Over 100 tensor operations, including transposing, indexing, slicing, mathematical operations, linear algebra, random sampling, and more are comprehensively described here.
超过 100 个张量操作,包括转置、索引、切片、数学运算、线性代数、随机抽样等,在此进行了全面描述。
Each of them can be run on the GPU (at typically higher speeds than on a CPU). If you're using Colab, allocate a GPU by going to Edit > Notebook Settings.
每个都可以在 GPU 上运行(通常比 CPU 上运行速度快)。如果您使用 Colab,请通过 Edit > Notebook 来分配 GPU。
# 如果GPU可用,则将张量移动到GPU
if torch.cuda.is_available():
tensor = tensor.to('cuda')
print(f"Device tensor is stored on: {tensor.device}")
Try out some of the operations from the list. If you're familiar with the NumPy API, you'll find the Tensor API a breeze to use.
请尝试上述列表中的一些操作。如果您熟悉 NumPy API,您会发现 Tensor API 使用起来非常容易。
Standard numpy-like indexing and slicing (标准的类似 numpy 的索引和切片):
tensor = torch.ones(4, 4)
tensor[:,1] = 0
print(tensor)
tensor([[1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.]])
Joining tensors (张量连接)
You can use torch.cat
to concatenate a sequence of
tensors along a given dimension. See also
torch.stack,
another tensor joining op that is subtly different from torch.cat
.
您可以使用 torch.cat 沿着指定维度连接一系列张量。此外还有 torch.stack,是与 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.]])
Multiplying tensors (张量乘法)
# 计算对应位置元素之间的乘积
print(f"tensor.mul(tensor) \n {tensor.mul(tensor)} \n")
# 另一种语法:
print(f"tensor * tensor \n {tensor * tensor}")
tensor.mul(tensor) tensor([[1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.]]) tensor * tensor tensor([[1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.]])
This computes the matrix multiplication between two tensors
计算两个张量之间的矩阵乘积
print(f"tensor.matmul(tensor.T) \n {tensor.matmul(tensor.T)} \n")
# 另一种语法:
print(f"tensor @ tensor.T \n {tensor @ tensor.T}")
tensor.matmul(tensor.T) tensor([[3., 3., 3., 3.], [3., 3., 3., 3.], [3., 3., 3., 3.], [3., 3., 3., 3.]]) tensor @ tensor.T tensor([[3., 3., 3., 3.], [3., 3., 3., 3.], [3., 3., 3., 3.], [3., 3., 3., 3.]])
In-place operations (就地运算)
Operations that have a _
suffix are in-place.
For example: x.copy_(y)
, x.t_()
, will change x
.
具有 _ 后缀的运算是就地运算。例如: x.copy_(y) , 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 operations save some memory, but can be problematic when computing derivatives because of an immediate lossof history. Hence, their use is discouraged.
注意:就地运算可以节省一些内存,但计算导数时可能会因为立即失去历史信息而出现问题。因此,不建议使用。
Bridge with NumPy {#bridge-to-np-label} (桥接NumPy)¶
Tensors on the CPU and NumPy arrays can share their underlying memory locations, and changing one will change the other.
张量在 CPU 上和 NumPy 数组可以共享其底层内存位置,改变其中一个也会传递改变到另一个。
Tensor to NumPy array (张量转换为 NumPy 数组)¶
t = torch.ones(5)
print(f"t: {t}")
n = t.numpy()
print(f"n: {n}")
t: tensor([1., 1., 1., 1., 1.]) n: [1. 1. 1. 1. 1.]
A change in the tensor reflects in the NumPy array.
张量的变化反映传递到 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 (NumPy 数组转换为张量)¶
n = np.ones(5)
t = torch.from_numpy(n)
Changes in the NumPy array reflects in the tensor.
NumPy 数组的变化反映传递到张量中。
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.]
Comments NOTHING