什么是 PyTorch?
PyTorch 是一个基于 Python 的科学计算包,主要定位两类人群:
- NumPy 的替代品,可以利用 GPU 的性能进行计算。
- 深度学习研究平台拥有足够的灵活性和速度
开始学习
Tensors (张量)
Tensors 类似于 NumPy 的 ndarrays ,同时 Tensors 可以使用 GPU 进行计算。
from __future__ import print_function import torch
构造一个5×3矩阵,不初始化。
x = torch.empty(5, 3) print(x)
输出:
tensor(1.00000e-04 * [[-0.0000, 0.0000, 1.5135], [ 0.0000, 0.0000, 0.0000], [ 0.0000, 0.0000, 0.0000], [ 0.0000, 0.0000, 0.0000], [ 0.0000, 0.0000, 0.0000]])
构造一个随机初始化的矩阵:
x = torch.rand(5, 3) print(x)
输出:
tensor([[ 0.6291, 0.2581, 0.6414], [ 0.9739, 0.8243, 0.2276], [ 0.4184, 0.1815, 0.5131], [ 0.5533, 0.5440, 0.0718], [ 0.2908, 0.1850, 0.5297]])
构造一个矩阵全为 0,而且数据类型是 long.
Construct a matrix filled zeros and of dtype long: x = torch.zeros(5, 3, dtype=torch.long) print(x)