import torch torch.manual_seed(0) torch.cuda.manual_seed(0) print(torch.__version__) 输出pytorch的版本号
创建Tensor
创建一个5x3的未初始化的Tensor
:
1. x = torch.empty(5, 3) 2. print(x) 3. 输出为:tensor([[0.0000e+00, 1.0842e-19, 1.6162e+22], 4. [2.8643e-42, 5.6052e-45, 0.0000e+00], 5. [0.0000e+00, 0.0000e+00, 0.0000e+00], 6. [0.0000e+00, 0.0000e+00, 0.0000e+00], 7. [0.0000e+00, 1.0842e-19, 1.3314e+22]])
创建一个5x3的随机初始化的Tensor
:
1. x = torch.rand(5, 3) 2. print(x) 3. 输出为: 4. tensor([[0.4963, 0.7682, 0.0885], 5. [0.1320, 0.3074, 0.6341], 6. [0.4901, 0.8964, 0.4556], 7. [0.6323, 0.3489, 0.4017], 8. [0.0223, 0.1689, 0.2939]])
创建一个5x3的long型全0的Tensor
:
1. x = torch.zeros(5, 3, dtype=torch.long) 2. print(x) 3. 输出为: 4. tensor([[0, 0, 0], 5. [0, 0, 0], 6. [0, 0, 0], 7. [0, 0, 0], 8. [0, 0, 0]])
直接根据数据创建:
1. x = torch.tensor([5.5, 3]) 2. print(x) 3. 输出为: 4. tensor([5.5000, 3.0000])
还可以通过现有的Tensor
来创建,此方法会默认重用输入Tensor
的一些属性。
1. x = x.new_ones(5, 3, dtype=torch.float64) # 返回的tensor默认具有相同的torch.dtype和torch.device 2. print(x) 3. 4. x = torch.randn_like(x, dtype=torch.float) # 指定新的数据类型 5. print(x) 6. 7. 输出为: 8. tensor([[1., 1., 1.], 9. [1., 1., 1.], 10. [1., 1., 1.], 11. [1., 1., 1.], 12. [1., 1., 1.]], dtype=torch.float64) 13. tensor([[ 0.6035, 0.8110, -0.0451], 14. [ 0.8797, 1.0482, -0.0445], 15. [-0.7229, 2.8663, -0.5655], 16. [ 0.1604, -0.0254, 1.0739], 17. [ 2.2628, -0.9175, -0.2251]])
我们可以通过shape
或者size()
来获取Tensor
的形状:
1. print(x.size()) 2. print(x.shape) 3. 输出为: 4. torch.Size([5, 3]) 5. torch.Size([5, 3])
注意:返回的torch.Size其实就是一个tuple, 支持所有tuple的操作。
操作
算术操作
- 加法形式一
1. y = torch.rand(5, 3) 2. print(x + y) 3. 输出为: 4. tensor([[ 1.3967, 1.0892, 0.4369], 5. [ 1.6995, 2.0453, 0.6539], 6. [-0.1553, 3.7016, -0.3599], 7. [ 0.7536, 0.0870, 1.2274], 8. [ 2.5046, -0.1913, 0.4760]])
- 加法形式二
1. print(torch.add(x, y)) 2. 输出为: 3. tensor([[ 1.3967, 1.0892, 0.4369], 4. [ 1.6995, 2.0453, 0.6539], 5. [-0.1553, 3.7016, -0.3599], 6. [ 0.7536, 0.0870, 1.2274], 7. [ 2.5046, -0.1913, 0.4760]]) 8. result = torch.empty(5, 3) 9. torch.add(x, y, out=result) 10. print(result) 11. 输出为: 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]])
- 加法形式三、inplace
1. # adds x to y 2. y.add_(x) 3. print(y) 4. 输出为: 5. tensor([[ 1.3967, 1.0892, 0.4369], 6. [ 1.6995, 2.0453, 0.6539], 7. [-0.1553, 3.7016, -0.3599], 8. [ 0.7536, 0.0870, 1.2274], 9. [ 2.5046, -0.1913, 0.4760]])
注:PyTorch操作inplace版本都有后缀"_", 例如
x.copy_(y), x.t_()
索引
1. y = x[0, :] 2. y += 1 3. print(y) 4. print(x[0, :]) # 源tensor也被改了 5. 6. 输出为: 7. tensor([1.6035, 1.8110, 0.9549]) 8. tensor([1.6035, 1.8110, 0.9549])
改变形状
用view()
来改变Tensor
的形状:
1. y = x.view(15) 2. z = x.view(-1, 5) # -1所指的维度可以根据其他维度的值推出来 3. print(x.size(), y.size(), z.size()) 4. 输出为: 5. torch.Size([5, 3]) torch.Size([15]) torch.Size([3, 5])
1. x += 1 2. print(x) 3. print(y) # 也加了1 4. 输出为: 5. tensor([[2.6035, 2.8110, 1.9549], 6. [1.8797, 2.0482, 0.9555], 7. [0.2771, 3.8663, 0.4345], 8. [1.1604, 0.9746, 2.0739], 9. [3.2628, 0.0825, 0.7749]]) 10. tensor([2.6035, 2.8110, 1.9549, 1.8797, 2.0482, 0.9555, 0.2771, 3.8663, 0.4345, 11. 1.1604, 0.9746, 2.0739, 3.2628, 0.0825, 0.7749])
如果不想共享内存,推荐先用clone
创造一个副本然后再使用view
。
1. x_cp = x.clone().view(15) 2. x -= 1 3. print(x) 4. print(x_cp) 5. 输出为: 6. tensor([[ 1.6035, 1.8110, 0.9549], 7. [ 0.8797, 1.0482, -0.0445], 8. [-0.7229, 2.8663, -0.5655], 9. [ 0.1604, -0.0254, 1.0739], 10. [ 2.2628, -0.9175, -0.2251]]) 11. tensor([2.6035, 2.8110, 1.9549, 1.8797, 2.0482, 0.9555, 0.2771, 3.8663, 0.4345, 12. 1.1604, 0.9746, 2.0739, 3.2628, 0.0825, 0.7749])
另外一个常用的函数就是item()
, 它可以将一个标量Tensor
转换成一个Python number:
1. x = torch.randn(1) 2. print(x) 3. print(x.item()) 4. 输出为: 5. tensor([2.3466]) 6. 2.3466382026672363
广播机制
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. 输出为: 7. tensor([[1, 2]]) 8. tensor([[1], 9. [2], 10. [3]]) 11. tensor([[2, 3], 12. [3, 4], 13. [4, 5]])
Tensor
和NumPy相互转换
Tensor
转NumPy
1. a = torch.ones(5) 2. b = a.numpy() 3. print(a, b) 4. 5. a += 1 6. print(a, b) 7. b += 1 8. print(a, b) 9. 输出为: 10. tensor([1., 1., 1., 1., 1.]) [1. 1. 1. 1. 1.] 11. tensor([2., 2., 2., 2., 2.]) [2. 2. 2. 2. 2.] 12. 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. 6. a += 1 7. print(a, b) 8. b += 1 9. print(a, b) 10. 输出为; 11. [1. 1. 1. 1. 1.] tensor([1., 1., 1., 1., 1.], dtype=torch.float64) 12. [2. 2. 2. 2. 2.] tensor([2., 2., 2., 2., 2.], dtype=torch.float64) 13. [3. 3. 3. 3. 3.] tensor([3., 3., 3., 3., 3.], dtype=torch.float64)
1. # 用torch.tensor()转换时不会共享内存 2. c = torch.tensor(a) 3. a += 1 4. print(a, c) 5. 输出为: 6. [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()还可以同时更改数据类型
参考文献
[1]《动手深度学习》李沐