3.1 PyTorch 学习

本节主要是学习PyTorch相关的学习,主要是基础的学习路线,包括简单的实例笔记等。

帮助文档见PyTorch官网

一、张量(tensor)和变量(Variable)

PyTorch的官方介绍是一个拥有强力GPU加速的张量和动态构建网络的库,其主要构建是张量,所以可以把PyTorch当做Numpy来用,Pytorch的很多操作好比Numpy都是类似的,但是其能够在GPU上运行,所以有着比Numpy快很多倍的速度。

import torch
import numpy as np

numpy_tensor = np.random.randn(3, 4)

pytorch_tensor1 = torch.Tensor(numpy_tensor)
pytorch_tensor2 = torch.from_numpy(numpy_tensor)

print(pytorch_tensor1)
print(pytorch_tensor2)

输出结果:

pytorch_tensor1:

tensor([[ 1.3511,  0.2016, -0.9728,  0.7997],
        [-1.0706, -0.0768, -1.3627, -0.8809],
        [-0.6040, -0.0030,  0.4871,  0.6634]])

使用以上两种方法进行转换的时候,会直接将Numpy ndarray的数据类型转换为对应的Pytorch Tensor数据类型,同时我们也可以使用下面的方法将pytorch tensor转换为numpy ndarray

需要注意GPU上的Tensor不能直接转换为Numpy ndarray,需要使用.cpu()先将GPU上的Tensor转到CPU上 PyTorch Tensor 使用GPU加速可以使用下面两种方法将Tensor放到GPU上.

  • 使用第一种方式将tensor放到GPU上的时候会将数据类型转换成定义的类型。

  • 而使用第二种方式能够直接将tensor放到GPU上,类型跟之前保持一致。

推荐在定义tensor的时候就明确数据类型,然后直接使用第二种方法将tensor放到GPU上

我的测试代码:

二、Torch的数据类型

2.1 torch.Tensor

torch.Tensor是一种包含单一数据类型元素的多维矩阵。

Torch定义了七种CPU tensor类型和八种GPU tensor类型

Data tyoe

CPU tensor

GPU tensor

32-bit floating point

torch.FloatTensor

torch.cuda.FloatTensor

64-bit floating point

torch.DoubleTensor

torch.cuda.DoubleTensor

16-bit floating point

N/A

torch.cuda.HalfTensor

8-bit integer (unsigned)

torch.ByteTensor

torch.cuda.ByteTensor

8-bit integer (signed)

torch.CharTensor

torch.cuda.CharTensor

16-bit integer (signed)

torch.ShortTensor

torch.cuda.ShortTensor

32-bit integer (signed)

torch.IntTensor

torch.cuda.IntTensor

64-bit integer (signed)

torch.LongTensor

torch.cuda.LongTensor

torch.Tensor是默认的tensor类型(torch.FlaotTensor)的简称。

一个张量tensor可以从Python的list或序列构建:

一个空张量tensor可以通过规定其大小来构建:

三、 Torch 的多种数学操作

3.1 Torch

torch包含了多维张量的数据结构以及基于其上的多种数学操作。另外,它也提供了多种工具,其中一些可以更有效地对张量和任意类型进行序列化。

它有CUDA 的对应实现,可以在NVIDIA GPU上进行张量运算(计算能力>=2.0)。

3.2 张量 Tensors

3.2.1 torch.is_tensor[source]

如果obj 是一个pytorch张量,则返回True

参数: obj (Object) – 判断对象

3.2.2 torch.is_storage

如何obj 是一个pytorch storage对象,则返回True

参数: input (Object) – 判断对象

3.2.3 torch.set_default_tensor_type[source]

3.2.4 torch.numel

返回input 张量中的元素个数

例子:

3.2.5 torch.set_printoptions[source]

设置打印选项。 完全参考自 Numpy。

参数:

  • precision – 浮点数输出的精度位数 (默认为8 )

  • threshold – 阈值,触发汇总显示而不是完全显示(repr)的数组元素的总数 (默认为1000)

  • edgeitems – 汇总显示中,每维(轴)两端显示的项数(默认值为3)

  • linewidth – 用于插入行间隔的每行字符数(默认为80)。Thresholded matricies will ignore this parameter.

  • profile – pretty打印的完全默认值。 可以覆盖上述所有选项 (默认为short, full)

3.3 创建操作 Creation Ops

3.3.1 torch.eye

返回一个2维张量,对角线位置全1,其它位置全0

参数:

  • n (int ) – 行数

  • m (int, optional) – 列数.如果为None,则默认为n

  • out (Tensor, optinal) - Output tensor

返回值: 对角线位置全1,其它位置全0的2维张量

返回值类型: Tensor

例子:

更多参考Pytorch 文档

Last updated

Was this helpful?