pytorch是:
1)可以使用GPU的Numpy
2)深度学习的框架
1.Tensors
Tensors
类似于Numpy
的ndarrays
,区别在于Tensor
可以使用GPU。
- 创建5*3的矩阵,未初始化
from __future__ import print_function
import torch
x = torch.empty(5, 3)
print(x)
----------------------------------------------
tensor([[ 5.0375e+28, 4.5625e-41, 5.0195e+28],
[ 4.5625e-41, -1.8338e+30, 3.0840e-41],
[-9.9826e+08, 4.5625e-41, -1.5343e+08],
[ 4.5625e-41, -1.0043e+09, 4.5625e-41],
[-2.0086e+08, 4.5625e-41, -9.5843e+08]])
- 创建随机初始化的矩阵
x = torch.rand(5, 3)
print(x)
-------------------------------------------------
tensor([[0.5814, 0.0997, 0.1744],
[0.2834, 0.9581, 0.9954],
[0.9372, 0.4401, 0.1696],
[0.1424, 0.5370, 0.9970],
[0.6686, 0.5558, 0.5354]])
-创建0初始化矩阵,并设定类型为long
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]])
- 从列表中创建
x = torch.tensor([5.5, 3])
print(x)
---------------------------------------------------
tensor([5.5000, 3.0000])
- 从已存在的tensor中创建,新的tensor将使用旧tensor的属性,除非新创建的指定了新的属性
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
------------------------------------------------------
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[-0.0970, 1.1034, -1.6941],
[-1.7651, -0.5884, -1.1931],
[ 1.0376, -0.8236, 0.8907],
[ 0.4683, -0.1217, 1.2467],
[ 0.4624, 0.4772, -1.0577]])
2.操作
- 加法
y = torch.rand(5, 3)
print(x + y)
-------------------------------
tensor([[ 0.4675, 1.7053, -1.2332],
[-1.6031, 0.1028, -0.6090],
[ 1.8545, -0.6408, 1.0778],
[ 0.9508, 0.7635, 2.1345],
[ 0.5693, 1.0205, -0.5476]])
--------------------------------
print(torch.add(x, y))
----------------------------------
tensor([[ 0.4675, 1.7053, -1.2332],
[-1.6031, 0.1028, -0.6090],
[ 1.8545, -0.6408, 1.0778],
[ 0.9508, 0.7635, 2.1345],
[ 0.5693, 1.0205, -0.5476]])
----------------------------------
# adds x to y
y.add_(x)
print(y)
----------------------------------
tensor([[ 0.4675, 1.7053, -1.2332],
[-1.6031, 0.1028, -0.6090],
[ 1.8545, -0.6408, 1.0778],
[ 0.9508, 0.7635, 2.1345],
[ 0.5693, 1.0205, -0.5476]])
- indexing
print(x[:, 1])
---------------------------------------------
tensor([ 1.1034, -0.5884, -0.8236, -0.1217, 0.4772])
- resizing
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())
--------------------------------------------------
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
- item 获得结果
x = torch.randn(1)
print(x)
print(x.item())
------------------------------------------------------
tensor([0.9551])
0.9551321864128113
3.Numpy桥梁
Torch Tensor和Numpy array会共享内存空间(如果Torch Tensor在CPU),改变其中一个另一个也会随着改变。
- 将tensor转换为ndarry
a = torch.ones(5)
print(a)
---------------------
tensor([1., 1., 1., 1., 1.])
---------------------
b = a.numpy()
print(b)
---------------------
[1. 1. 1. 1. 1.]
---------------------
a.add_(1)
print(a)
print(b)
---------------------
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
- 将ndarry转换为numpy
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)
4.CUDA Tensor
# 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 strings ``.to("cuda")``
z = x + y
print(z)
print(z.to("cpu", torch.double)) # ``.to`` can also change dtype together!
---------------------
tensor([1.9551], device='cuda:0')
tensor([1.9551], dtype=torch.float64)
参考:
https://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html#sphx-glr-beginner-blitz-tensor-tutorial-py