在PyTorch中,torch.Tensor是存储和变换数据的主要工具。如果之前用过NumPy,会发现Tensor和NumPy的多维数组非常类似。然而,Tensor提供GPU计算和自动求梯度等更多功能,这些使Tensor更加适合深度学习

“tensor”,张量可以看作是一个多维数组。标量可以看作是0维张量,向量可以看作1维张量,矩阵可以看作是二维张量。

创建Tensor

  1. import torch
  2. #创建一个5*3的未初始化的Tensor
  3. x = torch.empty(5,3)
  4. print(x)
  5. tensor([[1.7753e+28, 7.4389e+34, 1.8040e+28],
  6. [3.3840e-12, 7.5555e+31, 1.4605e-19],
  7. [2.7517e+12, 7.5338e+28, 7.6286e-19],
  8. [3.0313e+32, 9.1042e-12, 6.2609e+22],
  9. [4.7428e+30, 3.5833e-14, 0.0000e+00]])
  10. #创建一个5*3的随机初始化的Tensor
  11. x = torch.rand(5,3)
  12. print(x)
  13. tensor([[0.9999, 0.4164, 0.5664],
  14. [0.0104, 0.3030, 0.2805],
  15. [0.7643, 0.3122, 0.3903],
  16. [0.9240, 0.0910, 0.8578],
  17. [0.3053, 0.0977, 0.9361]])
  18. #创建一个5x3的long型全0的Tensor
  19. x = torch.zeros(5,3,dtype = torch.long)
  20. print(x)
  21. tensor([[0, 0, 0],
  22. [0, 0, 0],
  23. [0, 0, 0],
  24. [0, 0, 0],
  25. [0, 0, 0]])
  26. #还可以直接根据数据创建
  27. x = torch.tensor([5.5,3])
  28. print(x)
  29. tensor([5.5000, 3.0000])
  30. #还可以通过现有的Tensor来创建,此方法会默认重用输入Tensor的一些属性,例如数据类型,除非自定义数据类型
  31. x = torch.tensor([5.5,3])
  32. x = x.new_ones(5, 3, dtype=torch.float64) # 返回的tensor默认具有相同的torch.dtype和torch.device
  33. print(x)
  34. x = torch.randn_like(x, dtype=torch.float) # 指定新的数据类型
  35. print(x)
  36. tensor([[1., 1., 1.],
  37. [1., 1., 1.],
  38. [1., 1., 1.],
  39. [1., 1., 1.],
  40. [1., 1., 1.]], dtype=torch.float64)
  41. tensor([[ 0.6035, 0.8110, -0.0451],
  42. [ 0.8797, 1.0482, -0.0445],
  43. [-0.7229, 2.8663, -0.5655],
  44. [ 0.1604, -0.0254, 1.0739],
  45. [ 2.2628, -0.9175, -0.2251]])
  46. #可以通过shape或者size()来获取Tensor的形状
  47. print(x.size())
  48. print(x.shape)
  49. torch.Size([5, 3])
  50. torch.Size([5, 3]) #返回的torch.Size其实就是一个tuple, 支持所有tuple的操作

Tensor基本操作

加法

  1. y = torch.rand(5, 3)
  2. print(x + y)
  3. print(torch.add(x, y))
  4. #指定输出
  5. result = torch.empty(5, 3)
  6. torch.add(x, y, out=result)
  7. print(result)
  8. #inplace——PyTorch操作inplace版本都有后缀_, 例如x.copy_(y), x.t_()
  9. # adds x to y
  10. y.add_(x)
  11. print(y)
  12. tensor([[ 1.3967, 1.0892, 0.4369],
  13. [ 1.6995, 2.0453, 0.6539],
  14. [-0.1553, 3.7016, -0.3599],
  15. [ 0.7536, 0.0870, 1.2274],
  16. [ 2.5046, -0.1913, 0.4760]])

索引

可以使用类似NumPy的索引操作来访问Tensor的一部分,需要注意的是:索引出来的结果与原数据共享内存,也即修改一个,另一个会跟着修改

  1. y = x[0, :]
  2. y += 1
  3. print(y)
  4. print(x[0, :]) # 源tensor也被改了
  5. tensor([1.6035, 1.8110, 0.9549])
  6. tensor([1.6035, 1.8110, 0.9549])

改变形状

  1. y = x.view(15)
  2. z = x.view(-1, 5) # -1所指的维度可以根据其他维度的值推出来
  3. print(x.size(), y.size(), z.size())
  4. torch.Size([5, 3]) torch.Size([15]) torch.Size([3, 5])
  5. #注意view()返回的新Tensor与源Tensor虽然可能有不同的size,但是是共享data的,也即更改其中的一个,另外一个也会跟着改变。view仅仅是改变了对这个张量的观察角度,内部数据并未改变
  6. x += 1
  7. print(x)
  8. print(y) # 也加了1
  9. tensor([[1.6035, 1.8110, 0.9549],
  10. [1.8797, 2.0482, 0.9555],
  11. [0.2771, 3.8663, 0.4345],
  12. [1.1604, 0.9746, 2.0739],
  13. [3.2628, 0.0825, 0.7749]])
  14. tensor([1.6035, 1.8110, 0.9549, 1.8797, 2.0482, 0.9555, 0.2771, 3.8663, 0.4345,
  15. 1.1604, 0.9746, 2.0739, 3.2628, 0.0825, 0.7749])
  16. #如果我们想返回一个真正新的副本(即不共享data内存)该怎么办呢?Pytorch还提供了一个reshape()可以改变形状,但是此函数并不能保证返回的是其拷贝,所以不推荐使用。先用clone创造一个副本然后再使用view
  17. x_cp = x.clone().view(15)
  18. x -= 1
  19. print(x)
  20. print(x_cp)
  21. tensor([[ 0.6035, 0.8110, -0.0451],
  22. [ 0.8797, 1.0482, -0.0445],
  23. [-0.7229, 2.8663, -0.5655],
  24. [ 0.1604, -0.0254, 1.0739],
  25. [ 2.2628, -0.9175, -0.2251]])
  26. tensor([1.6035, 1.8110, 0.9549, 1.8797, 2.0482, 0.9555, 0.2771, 3.8663, 0.4345,
  27. 1.1604, 0.9746, 2.0739, 3.2628, 0.0825, 0.7749])
  28. #使用clone还有一个好处是会被记录在计算图中,即梯度回传到副本时也会传到源Tensor
  29. #item()它可以将一个标量Tensor转换成一个Python number
  30. x = torch.randn(1)
  31. print(x)
  32. print(x.item())
  33. tensor([2.3466])
  34. 2.3466382026672363

广播机制

当对两个形状不同的Tensor按元素运算时,可能会触发广播机制:先适当复制元素使这两个Tensor形状相同后再按元素运算

  1. x = torch.arange(1, 3).view(1, 2)
  2. print(x)
  3. y = torch.arange(1, 4).view(3, 1)
  4. print(y)
  5. print(x + y)
  6. tensor([[1, 2]])
  7. tensor([[1],
  8. [2],
  9. [3]])
  10. tensor([[2, 3],
  11. [3, 4],
  12. [4, 5]])
  13. #由于x和y分别是1行2列和3行1列的矩阵,如果要计算x + y,那么x中第一行的2个元素被广播(复制)到了第二行和第三行,而y中第一列的3个元素被广播(复制)到了第二列,如此,就可以对2个3行2列的矩阵按元素相加

运算的内存开销

索引操作是不会开辟新内存的,而像y = x + y这样的运算是会新开内存的,y指向新内存。为了演示这一点,可以使用Python自带的id函数:如果两个实例的ID一致,那么它们所对应的内存地址相同;反之则不同

  1. x = torch.tensor([1, 2])
  2. y = torch.tensor([3, 4])
  3. id_before = id(y)
  4. y = y + x
  5. print(id(y) == id_before) # False

如果想指定结果到原来的y的内存,可以使用前面介绍的索引来进行替换操作。在下面的例子中,把x + y的结果通过[ :]写进y对应的内存中

  1. x = torch.tensor([1,2])
  2. y = torch.tensor([3,4])
  3. id_before = id(y)
  4. y[:] = y + x
  5. print(id(y) == id_before) # True

还可以使用运算符全名函数中的out参数或者自加运算符+=(也即add()达到上述效果,例如torch.add(x, y, out = y)和y += x (y.add(x))

  1. x = torch.tensor([1, 2])
  2. y = torch.tensor([3, 4])
  3. id_before = id(y)
  4. torch.add(x, y, out=y) # y += x, y.add_(x)
  5. print(id(y) == id_before) # True

注:虽然view返回的Tensor与源Tensor是共享data的,但是依然是一个新的Tensor(因为Tensor除了包含data外还有一些其他属性),二者id(内存地址)并不一致

Tensor和Numpy的相互转换

我们很容易用numpy()和from numpy()将Tensor和NumPy中的数组相互转换。但是需要注意的一点是: 这两个函数所产生的的Tensor和NumPy中的数组共享相同的内存(所以他们之间的转换很快),改变其中一个时另一个也会改变!

还有一个常用的将NumPy中的array转换成Tensor的方法就是torch.tensor(), 此方法总是会进行数据拷贝(就会消耗更多的时间和空间),所以返回的Tensor和原来的数据不再共享内存

Tensor转Numpy

  1. a = torch.ones(5)
  2. b = a.numpy()
  3. print(a, b)
  4. a += 1
  5. print(a, b)
  6. b += 1
  7. print(a, b)
  8. tensor([1., 1., 1., 1., 1.]) [1. 1. 1. 1. 1.]
  9. tensor([2., 2., 2., 2., 2.]) [2. 2. 2. 2. 2.]
  10. tensor([3., 3., 3., 3., 3.]) [3. 3. 3. 3. 3.]

Numpy转Tensor

  1. import numpy as np
  2. a = np.ones(5)
  3. b = torch.from_numpy(a)
  4. print(a, b)
  5. a += 1
  6. print(a, b)
  7. b += 1
  8. print(a, b)
  9. [1. 1. 1. 1. 1.] tensor([1., 1., 1., 1., 1.], dtype=torch.float64)
  10. [2. 2. 2. 2. 2.] tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
  11. [3. 3. 3. 3. 3.] tensor([3., 3., 3., 3., 3.], dtype=torch.float64)

所有在CPU上的Tensor(除了CharTensor)都支持与NumPy数组相互转换。
此外上面提到还有一个常用的方法就是直接用torch.tensor()将NumPy数组转换成Tensor

  1. c = torch.tensor(a)
  2. a += 1
  3. print(a, c)
  4. [4. 4. 4. 4. 4.] tensor([3., 3., 3., 3., 3.], dtype=torch.float64)

Tensor on GPU

  1. # 以下代码只有在PyTorch GPU版本上才会执行
  2. if torch.cuda.is_available():
  3. device = torch.device("cuda") # GPU
  4. y = torch.ones_like(x, device=device) # 直接创建一个在GPU上的Tensor
  5. x = x.to(device) # 等价于 .to("cuda")
  6. z = x + y
  7. print(z)
  8. print(z.to("cpu", torch.double)) # to()还可以同时更改数据类型