Tensors类似与numpy的多维数组,除此之外,Tensors可以使用GPU加速计算.
from __future__ import print_function
import torch
创建一个5x3的矩阵,但不初始化:
x = torch.empty()
print(x)
Out:
tensor([[1.0790e-35, 4.5574e-41, 2.2442e-34],
[4.5574e-41, 2.5431e+30, 5.5073e+11],
[5.2563e+05, 3.2856e+04, 9.9340e+27],
[2.5431e+30, 6.2080e+26, 5.2501e+05],
[1.4810e+20, 9.7033e+24, 4.8976e-04]])
创建一个随机初始化的矩阵:
x = torch.rand(5, 3)
print(x)
Out:
tensor([[0.3235, 0.9115, 0.9074],
[0.9965, 0.9881, 0.7702],
[0.9278, 0.8675, 0.6744],
[0.4457, 0.4910, 0.1366],
[0.6894, 0.4388, 0.4079]])
创建一个数据类型dtype为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]])
直接用数据创建一个张量:
x = torch.tensor([5.5, 3])
print(x)
Out:
tensor([5.5000, 3.0000])
或者从已存在的张量中创建一个新的张量.这种方法会复用输入张量的属性,例如数据类型dtype,除非用户提供新的属性值:
x = x.new_ones(5, 3, dtype=torch.double) # new_* methods take in sizes
print(x)
x = torch.randn_like(x, dtype=torch.float) # override dtype!
print(x) # result has the same size
Out:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[ 1.6738, -0.1486, -0.6143],
[ 0.1894, -0.7618, -1.2741],
[-0.2055, -0.1476, -0.7594],
[ 0.7300, -1.6400, -0.1834],
[-1.4198, -0.4612, -0.3066]])
获取x的size:
print(x.size())
Out:
torch.Size([5, 3])
- 提示:
torch.Size事实上是一个元组,因此它支持所有的元组操作.
有许多操作的语法. 下面我们来看看加法操作.
加法: 语法1
y = torch.rand(5, 3)
print(x + y)
Out:
tensor([[ 2.1095e+00, 1.9451e-01, -3.0300e-01],
[ 5.2001e-01, -3.2870e-02, -9.1198e-01],
[ 5.2994e-01, 2.4504e-01, -7.6717e-04],
[ 1.2301e+00, -1.2432e+00, 1.2883e-01],
[-1.3974e+00, -1.6369e-01, 4.7470e-01]])
加法: 语法2
print(torch.add(x, y))
Out:
tensor([[ 2.1095e+00, 1.9451e-01, -3.0300e-01],
[ 5.2001e-01, -3.2870e-02, -9.1198e-01],
[ 5.2994e-01, 2.4504e-01, -7.6717e-04],
[ 1.2301e+00, -1.2432e+00, 1.2883e-01],
[-1.3974e+00, -1.6369e-01, 4.7470e-01]])
加法:将输出向量作为参数
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
Out:
tensor([[ 2.1095e+00, 1.9451e-01, -3.0300e-01],
[ 5.2001e-01, -3.2870e-02, -9.1198e-01],
[ 5.2994e-01, 2.4504e-01, -7.6717e-04],
[ 1.2301e+00, -1.2432e+00, 1.2883e-01],
[-1.3974e+00, -1.6369e-01, 4.7470e-01]])
加法:in-place
# adds x to y
y.add_(x)
print(y)
Out:
tensor([[ 2.1095e+00, 1.9451e-01, -3.0300e-01],
[ 5.2001e-01, -3.2870e-02, -9.1198e-01],
[ 5.2994e-01, 2.4504e-01, -7.6717e-04],
[ 1.2301e+00, -1.2432e+00, 1.2883e-01],
[-1.3974e+00, -1.6369e-01, 4.7470e-01]])
- 提示:
任何用in-place方式改变张量的操作都带有_
后置符号. 例如:x.copy_(y)
,x.t_()
,将改变x
.
我们可以使用带有任何附加修饰的类numpy索引!
print(x[:, 1])
Out:
tensor([-0.1486, -0.7618, -0.1476, -1.6400, -0.4612])
Resizing:如果要对张量做resize/reshape操作,可以使用torch.view
:
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())
Out:
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
从一个单元素张量中获得Python numpy 值可以使用.item
x = torch.randn(1)
print(x)
print(x.item())
Out:
tensor([1.3638])
1.363796353340149
后续:
100 +张量操作,包括置换、索引、切片、数学运算、线性代数、随机数字,等等,可以从这里获得.
转换 Torch Tensor 到 NumPy 数组,或转换 NumPy 数组 到Torch Tensor .
Torch Tensor 和 Numpy 数组共享底层的内存位置,改变任何一个都将改变另一个.
a = torch.ones(5)
print(a)
Out:
tensor([1.,1.,1.,1.,1.])
b = a.numpy()
print(b)
Out:
[1. 1. 1. 1. 1]
改变a
时,看b
如何改变.
a.add_(1)
print(a)
print(b)
Out:
tensor([2.,2.,2.,2.,2.])
[2. 2. 2. 2. 2.]
看看怎样自动的通过改变numpy数组使Torch Tensor发生改变.
import numpy as np
a = np.ones(5)
b = torch.form_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
Out:
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
所有除了字符张量外的CPU中的张量均支持转换到Numpy类型,反之亦真.
Tensors可以被转移到任意的设备,通过.to
方法.
# 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 string ".to("cuda")"
z = x + y
print(z)
print(z.to("cpu",torch.double)) # ".to" can also change dtype together!
Out:
tensor([2.3638], device='cuda:0')
tensor([2.3638], dtype=torch.float64)
pytorch官方网站