这是一个基于Python的科学计算包,主要针对两类人群:
Tensors类似于numpy的ndarray,但是带了一些附加的功能,例如可以使用GPU加速计算等等。
from __future__ import print_function
import torch
构建一个未初始化的5*3的矩阵:
x = torch.empty(5, 3)
print(x)
tensor([[-1.0593e-05, 4.5849e-41, 3.4723e-37],
[ 0.0000e+00, 0.0000e+00, 0.0000e+00],
[ 0.0000e+00, 0.0000e+00, 7.1941e+28],
[ 3.1036e+27, 0.0000e+00, 0.0000e+00],
[ 0.0000e+00, 0.0000e+00, 3.4568e-37]])
构建一个随机初始化的5*3的矩阵:
x = torch.rand(5, 3)
print(x)
tensor([[ 0.7556, 0.7558, 0.2999],
[ 0.7304, 0.3527, 0.1911],
[ 0.8654, 0.4880, 0.1987],
[ 0.5456, 0.9359, 0.2071],
[ 0.1025, 0.5249, 0.3758]])
构建一个初始化为零类型为long的5*3的矩阵:
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])
根据现有张量创建张量。这些方法将重用输入张量的属性,例如dtype,除非用户提供了新的值
print(x) #打印之前的x值
x = x.new_ones(5, 3, dtype=torch.double) # new_* 方法可以更改x的值,维度和类型
print(x)
x = torch.randn_like(x, dtype=torch.float) # 类型与值进行覆盖
print(x) # 不改变维度
tensor([ 5.5000, 3.0000])
tensor([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]], dtype=torch.float64)
tensor([[-1.4230, -0.7907, -0.0556],
[-0.9403, -0.2937, 1.9447],
[ 0.2958, 0.9914, -0.9550],
[ 1.2439, -0.1390, 0.2889],
[-0.1790, -0.0003, 0.5241]])
获取尺寸
print(x.size())
torch.Size([5, 3])
torch.Size 实际上是一个元组(tuple),因此它支持所有的元祖(tuple)的操作。
Pytorch具有100多种操作符(加减乘除,转置,索引,切片,等等),在这里我们以最简单的加法操作,了解Pytorch的操作方法。
y = torch.rand(5, 3)
print(x + y)
tensor([[-1.1514, -0.5880, -0.0083],
[-0.4967, 0.2964, 2.5860],
[ 0.7163, 1.0643, 0.0210],
[ 1.8021, 0.6697, 0.8263],
[ 0.3601, 0.3765, 1.3859]])
print(torch.add(x, y))
tensor([[-1.1514, -0.5880, -0.0083],
[-0.4967, 0.2964, 2.5860],
[ 0.7163, 1.0643, 0.0210],
[ 1.8021, 0.6697, 0.8263],
[ 0.3601, 0.3765, 1.3859]])
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
tensor([[-1.1514, -0.5880, -0.0083],
[-0.4967, 0.2964, 2.5860],
[ 0.7163, 1.0643, 0.0210],
[ 1.8021, 0.6697, 0.8263],
[ 0.3601, 0.3765, 1.3859]])
# adds x to y
y.add_(x)
print(y)
tensor([[-1.1514, -0.5880, -0.0083],
[-0.4967, 0.2964, 2.5860],
[ 0.7163, 1.0643, 0.0210],
[ 1.8021, 0.6697, 0.8263],
[ 0.3601, 0.3765, 1.3859]])
任何就地改变一个tensor的操作都以
_
为后缀。例如:x.copy_(y)
,x.t_()
,都会改变x。
Torch的Tensor和Numpy的数组会共享它们的底层存储位置,改变其中一个,另外一个也会改变。
a = torch.ones(5) # 创建一个torch张量
print(a)
b = a.numpy() # 将torch张量转化为numpy数组
print(b)
a.add_(1) # 就地改变torch张量的值
print(a) # a torch张量发生改变
print(b) # b numpy数组因为共享底层存储所以也同时改变
tensor([ 1., 1., 1., 1., 1.])
[1. 1. 1. 1. 1.]
tensor([ 2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
import numpy as np #导入numpy
a = np.ones(5) #创建numpy数组
b = torch.from_numpy(a) #numpy转化成torch张量
np.add(a, 1, out=a) #numpy数组数据加一
print(a) # numpy数组发生变化
print(b) # torch张量因为与numpy共享底层存储因此也发生变化
[2. 2. 2. 2. 2.]
tensor([ 2., 2., 2., 2., 2.], dtype=torch.float64)
可以使用.to方法将张量移动到任何设备上。
# 我们使用 ``torch.device`` 对象 将张量移入和移出GPU
if torch.cuda.is_available():
device = torch.device("cuda") # 一个CUDA设备对象
y = torch.ones_like(x, device=device) # 直接在GPU上创建一个张量对象
x = x.to(device) # 或者使用``.to("cuda")``
z = x + y
print(z)
print(z.to("cpu", torch.double)) # ``.to`` 将结果转回cpu存储,还可以改变数据类型
tensor([[-0.4230, 0.2093, 0.9444],
[ 0.0597, 0.7063, 2.9447],
[ 1.2958, 1.9914, 0.0450],
[ 2.2439, 0.8610, 1.2889],
[ 0.8210, 0.9997, 1.5241]], device='cuda:0')
tensor([[-0.4230, 0.2093, 0.9444],
[ 0.0597, 0.7063, 2.9447],
[ 1.2958, 1.9914, 0.0450],
[ 2.2439, 0.8610, 1.2889],
[ 0.8210, 0.9997, 1.5241]], dtype=torch.float64)
Deep Learning with PyTorch: A 60 Minute Blitz(https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html)