什么是pytorch??

Pytorch是基于python的科学计算包,为两类受众提供服务

  • 作为Numpy的替换,让你可以使用GPU的算力
  • 作为一个深度学习计算平台提供最大的计算灵活性与速度

开始体验pytorch的基础功能

Tensor:

tensor与Numpy的高维数据概念类似,可以在GPU上进行计算

import torch

建立一个5*3的未初始化的tensor

x=torch.empty(5,3)
print(x)
out:
tensor(1.00000e-07 *
       [[-0.0000,  0.0000,  0.0000],
        [ 0.0000,  9.4713,  0.0000],
        [ 9.4201,  0.0000,  0.0000],
        [ 0.0000, -0.0000,  0.0000],
        [-0.0000,  0.0000, -0.0000]])

建立一个随机初始化的tensor

x=torch.rand(5,3)
print(x)
out:
tensor([[ 0.7816, 0.8146, 0.9424],

[ 0.0888, 0.5530, 0.9181],

[ 0.8362, 0.1937, 0.0084],

[ 0.2004, 0.2818, 0.8674],

[ 0.6464, 0.4978, 0.8994]])

建立一个tensor用0填充并使用long类型

x=torch.zeros(5,3,dtype=torch.long)
print(x)
out:

tensor([[ 0, 0, 0],

[ 0, 0, 0],

[ 0, 0, 0],

[ 0, 0, 0],

[ 0, 0, 0]])

直接从数据创建tensor

torch.tensor([5.5,3])
print(x)
out:

tensor([ 5.5000, 3.0000])

我们也可以基于现有的tensor建立新的tensor,这样新的tensor会复用之前的属性,比如类型等

x=torch.new_ones(5,3,dtype=torch.double)
print(x)
x=torch.randn_like(x,dtype=torch.float)
print(x)


out:


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

[ 1., 1., 1.],

[ 1., 1., 1.],

[ 1., 1., 1.],

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

tensor([[ 0.3648, 0.5067, 1.1720],

[-1.3361, 0.9999, 0.4133],

[-0.2699, 0.7601, -1.1138],

[-1.8955, -0.4079, 1.0827],

[-0.0156, 0.3810, 1.2646]])

获得tensor尺寸

print(x.size())
out:

torch.Size([5, 3])

运算

y=torch.rand(5,3)
#torch.add(x,y) print(x+y)
out:

tensor([[ 1.0363, 0.8469, 1.1758],

[ 1.5991, 0.8271, 1.2000],

[ 0.9036, 1.1352, 1.4293],

[ 1.3676, 0.8430, 0.7633],

[ 1.3159, 1.4635, 1.9067]])

提供输出变量作为参数

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

out:
tensor([[ 1.0363,  0.8469,  1.1758],
        [ 1.5991,  0.8271,  1.2000],
        [ 0.9036,  1.1352,  1.4293],
        [ 1.3676,  0.8430,  0.7633],
        [ 1.3159,  1.4635,  1.9067]])

同时提供了内建函数,内建函数会影响变量本身的值,如x.copy_(y),x.t_()会影响x的值

y.add_(x)
print(y)

out:
tensor([[ 1.0363,  0.8469,  1.1758],
        [ 1.5991,  0.8271,  1.2000],
        [ 0.9036,  1.1352,  1.4293],
        [ 1.3676,  0.8430,  0.7633],
        [ 1.3159,  1.4635,  1.9067]])

你可以使用标准Numpy的索引的所有写法!!

print(x[:,1])
out:
tensor([ 0.2492,  0.7801,  0.5992,  0.8164,  0.6371])

调整形状:如果你想重新调整tensor的维度,可以使用tensor.view

x=torch.randn(4,4)
y=torch.view(16)
z=torch.view(-1,8)
print(x.size(),y.size(),z.size())
out:
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])

如果你有一个只包含一个元素的tensor,可以使用.item()来得到python形式的值

x=torch.randn(1)
print(x)
print(x.item())
out:
tensor([ 0.4210])
0.4210202693939209

Numpy桥

torch的tensor与Numpy数组之间的转换是很轻松的

torch的tensor与Numpy的数组共享同一块内存空间,改变一者会影响另一个

将torch tensor转换为Numpy数组:

a=torch.ones(5)
print(a)
b=a.numpy()
print(b) out: tensor([ 1., 1., 1., 1., 1.])
[1. 1. 1. 1. 1.]

观察numpy数组b的变化

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

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

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

将Numpy数组转换为torch tensor:

观察numpy数组如何引起torch tensor的变化

import numpy as np
a=no.ones(5)
b=torch.from_numpy(a)
np.add(a,1,out=a) print(a) print(b)
out:
array([2., 2., 2., 2., 2.])
tensor([ 2., 2., 2., 2., 2.], dtype=torch.float64)

除了CharTensor外CPU上的tensor都可以转换为numpy并返回

CUDA Tensor

if torch.cuda.is_available():
    device=torch.device("cuda")  #一个CUDA设备目标
    y=torch.ones_like(x,device=device)  #直接在GPU上建立变量
    x=x.to(device)
    z=x+y
    print(z)
    print(z.to("cpu",torch.double))

out:
tensor([[ 1.4325, -0.1697, 2.2435],
[ 0.6878, 0.9155, 1.4876]], dtype=torch.float64)