0%

Pytorch基础

Pytorch-基本语法

导入

1
2
3
# 加上后,即使是python2也能使用print()而不是 print xxx
from __future__ import print_function
import torch

基本元素操作

创建矩阵

创建空矩阵

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

tensor([[-1.1210e-14, 3.0767e-41, 3.3631e-44],

​ [ 0.0000e+00, nan, 6.4460e-44],

​ [ 1.1578e+27, 1.1362e+30, 7.1547e+22],

​ [ 4.5828e+30, 1.2121e+04, 7.1846e+22],

​ [ 9.2198e-39, 7.0374e+22, 0.0000e+00]])

创建一个随机初始化的矩阵

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

tensor([[0.0465, 0.3854, 0.8354],

​ [0.5556, 0.2372, 0.0594],

​ [0.5800, 0.0649, 0.2107],

​ [0.1946, 0.7404, 0.8338],

​ [0.2988, 0.4238, 0.3945]])

创建一个全零矩阵,并指定数据元素的类型为long

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

tensor([[0, 0, 0],

​ [0, 0, 0],

​ [0, 0, 0],

​ [0, 0, 0],

​ [0, 0, 0]])

直接通过数据创建张量

1
2
x = torch.tensor([2.5, 3.5])
print(x)

tensor([2.5000, 3.5000])

通过一个已有的张量创建相同尺寸的新张量

1
2
3
4
x = x.new_ones(5, 3, dtype=torch.double)
print(x)
y = torch.randn_like(x, dtype=torch.float)
print(y)

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

​ [1., 1., 1.],

​ [1., 1., 1.],

​ [1., 1., 1.],

​ [1., 1., 1.]], dtype=torch.float64)

tensor([[-0.0495, -1.1731, 0.3187],

​ [ 0.9321, -0.5554, -1.7926],

​ [ 0.0627, 1.7919, 1.4413],

​ [-0.8785, -0.9865, -1.0847],

​ [-0.8606, -1.4447, -1.7759]])

得到张量尺寸

1
print(x.size())

torch.Size([5, 3])

Pytorch-基本运算操作

加法操作

第一种:

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

tensor([[1.7637, 1.4502, 1.5603],

​ [1.9027, 1.6812, 1.0642],

​ [1.1892, 1.8056, 1.2032],

​ [1.7226, 1.7720, 1.8164],

​ [1.9689, 1.1140, 1.1994]], dtype=torch.float64)

第二种:

1
torch.add(x, y)

第三种:

1
2
result = torch.empty(5, 3)
torch.add(x, y, out=result)

第四种(原地置换):

1
2
y.add_(x)
print(y)

对于减法(sub)、乘法(mul)、除法(div)等也是类似

用类似于Numpy的方式对张量进行操作

1
print(x[:, 1])

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

改变张量的形状

1
2
3
4
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)
print(x.size(), y.size(), z.size())

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

如果张量中只有一个元素,可以用.item()将值取出

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

tensor([-0.8954]) -0.8954272270202637

Torch Tensor 和 Numpy array之间的相互转换

Torch Tensor和Numpy array共享底层的内存空间,因此改变其中一个值,另一个也会随之被改变

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

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

将Torch Tensor转换为Numpy array

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

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

对其中一个进行加法操作,另一个也会改变

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

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

[2. 2. 2. 2. 2.]

将Numpy array 转换为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)

[2. 2. 2. 2. 2.]

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

Cuda Tensor:Tensors可以用.to()将其移动到任意设备上

1
2
3
4
5
6
7
8
9
10
11
12
13
# 如果有GPU和CUDA
if torch.cuda.is_available():
# 定义一个设备对象,这里指定成CUDA,即使用GPU
device = torch.device("cuda")
# 直接在GPU上创建一个Tensor
y = torch.ones_like(x, device=device)
# 将在CPU上的张量移动到GPU
x = x.to(device)
# x和y都在GPU上,才能支持加法运算
z = x + y
print(z)
# 也可以将z移动到CPU上
print(z.to("cpu", torch.double))

tensor([0.8778], device='cuda:0')

tensor([0.8778], dtype=torch.float64)