1 - tensor_tutorial (tensor教程)

原文地址:
https://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html

What is PyTorch

这是一个基于Python的科学计算包,目标是两组优势:

  • NumPy的替代品,可以使用GPU的强大功能
  • 深入学习研究平台,提供最大的灵活性和速度

    Getting Started

Tensor

Tensor与NumPy的多维数组相似,添加的是Tensor也可以用于GPU加速计算。

1
2
from __future__ import print_function
import torch

构建一个未初始化的5*3的矩阵:

1
2
x = torch.empty(5, 3)
print(x)

输出:

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

创建一个随机初始化矩阵

1
2
x = torch.rand(5, 3)
print(x)

输出:

1
2
3
4
5
tensor([[0.2799, 0.2665, 0.5734],
[0.7835, 0.6617, 0.8958],
[0.9766, 0.6714, 0.2334],
[0.6510, 0.0141, 0.6451],
[0.6124, 0.3446, 0.6923]])

创建一个全零矩阵并且类型为long:

1
2
x = torch.zeros(5, 3, dtype=torch.long)
print(x)

输出:

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

根据数据直接创建一个tensor

1
2
x = torch.tensor([5.5, 3])
print(x)

输出:

1
tensor([5.5000, 3.0000])

或者根据现有tensor创建一个tensor。
这些方法将重用输入tensor的属性,如dtype,除非用户提供新的值

1
2
3
4
5
x = x.new_ones(5, 3, dtype=torch.double)      # new_* methods 获得大小
print(x)

x = torch.randn_like(x, dtype=torch.float) # 重新赋予类型
print(x) # 结果拥有相同的大小

输出:

1
2
3
4
5
6
7
8
9
10
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[ 0.3567, -1.2192, -1.5638],
[-0.9681, 0.5553, 0.1525],
[ 0.6320, -0.5874, 0.6034],
[-0.5114, -0.6262, -0.0601],
[-0.0572, 0.9120, 0.9557]])

获取它的大小:

1
print(x.size())

输出:

1
torch.Size([5, 3])

注意:

“torch.Size”实际上是一个元组,所以它支持所有的元组操作。

有多个语法操作。在下面的例子中,我们将看一看加法操作。

  • 语法1
1
2
y = torch.rand(5, 3)
print(x + y)

输出:

1
2
3
4
5
tensor([[ 0.5408, -0.8138, -1.3310],
[-0.0024, 0.6934, 0.3962],
[ 1.2138, -0.5800, 1.2939],
[ 0.1962, -0.5142, 0.7489],
[ 0.6374, 1.1835, 1.3452]])

  • 语法2
1
print(torch.add(x, y))

输出:

1
2
3
4
5
tensor([[ 0.5408, -0.8138, -1.3310],
[-0.0024, 0.6934, 0.3962],
[ 1.2138, -0.5800, 1.2939],
[ 0.1962, -0.5142, 0.7489],
[ 0.6374, 1.1835, 1.3452]])
  • 作为参数提供一个输出tensor
1
2
3
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)

输出:

1
2
3
4
5
tensor([[ 0.5408, -0.8138, -1.3310],
[-0.0024, 0.6934, 0.3962],
[ 1.2138, -0.5800, 1.2939],
[ 0.1962, -0.5142, 0.7489],
[ 0.6374, 1.1835, 1.3452]])

  • 在原位置上操作
1
2
3
# adds x to y
y.add_(x)
print(y)

输出:

1
2
3
4
5
tensor([[ 0.5408, -0.8138, -1.3310],
[-0.0024, 0.6934, 0.3962],
[ 1.2138, -0.5800, 1.2939],
[ 0.1962, -0.5142, 0.7489],
[ 0.6374, 1.1835, 1.3452]])

注意:

任何对一个tensor进行变异的操作都是用_
例如:“x.copy_(y)’ ‘,’ ‘ x.t_()’ ‘,将改变“x”。

You can use standard NumPy-like indexing with all bells and whistles!

1
print(x[:, 1])

输出:

1
tensor([-1.2192,  0.5553, -0.5874, -0.6262,  0.9120])

调整大小:如果你想要调整/重塑张量,你可以使用 torch.view:

1
2
3
4
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8) # the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())

输出:

1
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])

如果你有一个元素tensor,使用.item()来获得作为Python的数值

1
2
3
x = torch.randn(1)
print(x)
print(x.item())

输出:

1
2
tensor([0.6536])
0.653590977191925

Read later:

100+ Tensor operations, including transposing, indexing, slicing, mathematical operations, linear algebra, random numbers, etc., are described here http://pytorch.org/docs/torch_.

NumPy Bridge

将一个Torch Tensor转换成一个NumPy数组,反之亦然。

Torch Tensor和NumPy数组将共享它们的底层内存位置,而改变一个则会改变另一个。

将一个Torch Tensor转换为Numpy数组:

1
2
a = torch.ones(5)
print(a)

输出:

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

1
2
b = a.numpy()
print(b)

输出:

1
[1. 1. 1. 1. 1.]

看看numpy数组是如何在值中改变的。

1
2
3
a.add_(1)
print(a)
print(b)

输出:

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

将一个Numpy数组转换为Torch Tensor,看看如何改变np数组自动改变了Torch Tensor

1
2
3
4
5
6
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)

输出:

1
2
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)

CPU上所有的Tensors,除了CharTensor 都支持跟NumPy之间的相互转换。

CUDA Tensors

Tensor可以使用.to方法移动到任何设备上。

1
2
3
4
5
6
7
8
9
# let us run this cell only if CUDA is available
# We will use ``torch.device`` objects to move tensors in and out of GPU
if torch.cuda.is_available():
device = torch.device("cuda") # a CUDA device object
y = torch.ones_like(x, device=device) # directly create a tensor on GPU
x = x.to(device) # or just use strings ``.to("cuda")``
z = x + y
print(z)
print(z.to("cpu", torch.double)) # ``.to`` can also change dtype together!

输出:

1
2
tensor([1.6536], device='cuda:0')
tensor([1.6536], dtype=torch.float64)