《Deep Learning Series》
  • 深耕系列之深度学习笔记
  • 第一章 Linux学习环境相关配置
    • 1.1 Ubuntu18下有道词典的配置
    • 1.2 Ubuntu18 安装Gitbook
    • 1.3 Ubuntu18 git命令使用总结
    • 1.4 Latex 排版使用笔记
    • 1.5 Ubuntu下常用工具软件配置安装
    • 1.6 win10+ubuntu双系统修复ubuntu启动引导
    • 1.7 gitbook 插件等相关设置
    • 1.8 深度学习环境搭建
    • 1.9 hexo 实现本地图片加载
    • 1.10 hexo网页定制
    • 1.11 sublime text3插件介绍
    • 1.12 vsftpd.conf文件配置
    • 1.13 mysql 笔记
    • 1.14 ubuntu16_18安装peek工具录制gif
    • 1.15 ubuntu下goldendict有道爬虫小程序
    • 1.16 ubuntu18升级后部分应用不能中文输入的问题
    • 1.17 ubuntu下安装有道词典
    • 1.18 opencv 安装
    • 1.19 gym_gazabe安装配置
    • 1.20 docker 基础
    • 1.21 docker_配置权限问题
    • 1.22 jupyternotebook使用
  • 第二章 深度学习相关基础算法
    • 2.1 马尔科夫链
      • 2.1.1 马尔科夫简单模型预测实战笔记
      • 2.1.2 最大熵模型
      • 2.1.3 隐马尔科夫HMM
    • 2.2 矩阵相关基础知识
    • 2.3 线性回归
    • 2.4 决策树
    • 2.5 梯度下降和最小二乘法
    • 2.6 递归算法与迭代算法
    • 2.7 神经网络浅学笔记
    • 2.8 强化学习经验回放
    • 2.9 K近邻算法
    • 2.10 朴素贝叶斯法
    • 2.11 极大似然估计
    • 2.12 logistic regression
  • 第三章 深度学习框架学习
    • 3.1 PyTorch 学习
      • 3.1.2 Pytorch 之MNIST手写字识别分类
    • 3.2 tensorflow学习笔记
      • 3.2.1 tensorflow之MNIST
    • 3.3 matplotlib函数
    • 3.4 numpy函数
  • 第四章 ROS机器人
    • ROS室内仿真环境.md
    • ros and gazebo and gym_gazebo安装
    • ubuntu16 安装gym-gazebo
    • gym-gazebo安装后的测试
    • 基于DQN的gym_gazebo运行代码演示
  • 项目开发
    • Library占座小工具使用手册
  • 附录
    • Python 相关笔记
      • Python 帮助文档检索方法
      • Module篇使用future
    • Git 相关配置
      • git-推送新的文章到github其他分支上
      • gitignre 配置
      • gitignre 配置
      • Hexo 每次写好后deploy博客
      • MFC Socket 通信
      • python之tkinter入坑Pack
      • ubuntu 中安装sublime_text3
      • ubuntu18-正确-安装ShadowSocket
      • vultr+freenom实现主机域名的绑定.md
      • 值得收藏的网站
      • 搜索技巧
      • 第一篇博文
      • 简单的方法,越过付费获取在线的log设计.md
      • 网页设计基础笔记.md
      • 解决Chrome67版本以后不能离线安装插件的情况.md
    • 嵌入式相关笔记
      • STM32串口通信配置
      • STM32复位及通过函数判断是何种条件出发的复位
Powered by GitBook
On this page
  • 一、张量(tensor)和变量(Variable)
  • 二、Torch的数据类型
  • 三、 Torch 的多种数学操作
  • 3.1 Torch
  • 3.2 张量 Tensors
  • 3.3 创建操作 Creation Ops

Was this helpful?

  1. 第三章 深度学习框架学习

3.1 PyTorch 学习

Previous第三章 深度学习框架学习Next3.1.2 Pytorch 之MNIST手写字识别分类

Last updated 5 years ago

Was this helpful?

本节主要是学习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]])
pytorch_tensor2:

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]], dtype=torch.float64)

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

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)

# 如果pytorch tensor在cpu上
numpy_array1 = pytorch_tensor1.numpy()
numpy_array2 = pytorch_tensor2.cpu().numpy()

print(numpy_array1)
print(numpy_array2)
numpy_array1:

[[ 0.9646071   1.0680387  -1.4145772  -1.1733457 ]
 [ 0.14683424  0.15183815  0.3256755   2.5129247 ]
 [-1.0027096   0.02551154 -0.60790646 -0.22400694]]

 numpy_array2:

[[-2.09633392 -2.08986247  0.02169762  0.15833546]
 [ 1.24929483 -1.3953018   1.03153148 -0.06309232]
 [ 0.24348084 -1.42512446  1.45863934  0.92882537]]

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

# 第一种方式是定义cuda数据类型
dtype = torch.cuda.FloatTensor
gpu_tensor = torch.randn(10,20).type(dtype)

# 第二种方式更简单,推荐使用
gpu_tensor = torch.randn(10,20).cuda(0) # 将tensor放到第一个GPU上
gpu_tensor = torch.randn(10,20).cuda(1) # 将tensor放到第二个GPU上
  • 使用第一种方式将tensor放到GPU上的时候会将数据类型转换成定义的类型。

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

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

我的测试代码:

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)

# 如果pytorch tensor在cpu上
numpy_array1 = pytorch_tensor1.numpy()
numpy_array2 = pytorch_tensor2.cpu().numpy()

print(numpy_array1)
print(numpy_array2)

print("输出数据模式1:")
# 第一种方式是定义cuda数据类型
dtype = torch.cuda.FloatTensor
gpu_tensor1 = torch.randn(3,4).type(dtype)

print(gpu_tensor1)

gpu_tensor2 = torch.randn(3,4).cuda(0) # 将tensor放到第一个GPU上

print("输出数据模式2:")
print(gpu_tensor2)

二、Torch的数据类型

2.1 torch.Tensor

torch.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或序列构建:

>>> torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
1 2 3
4 5 6
[torch.FloatTensor of size 2x3]

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

>>> torch.IntTensor(2, 4).zero_()
0 0 0 0
0 0 0 0
[torch.IntTensor of size 2x4]

三、 Torch 的多种数学操作

3.1 Torch

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

3.2 张量 Tensors

3.2.1 torch.is_tensor[source]

torch.is_tensor(obj)

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

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

3.2.2 torch.is_storage

torch.is_storage(obj)

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

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

3.2.3 torch.set_default_tensor_type[source]

torch.set_default_tensor_type(t)

3.2.4 torch.numel

torch.numel(input)->int

返回input 张量中的元素个数

例子:

>>> a = torch.randn(1,2,3,4,5)
>>> torch.numel(a)
120
>>> a = torch.zeros(4,4)
>>> torch.numel(a)
16

3.2.5 torch.set_printoptions[source]

torch.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None)

设置打印选项。 完全参考自 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

torch.eye(n, m=None, out=None)

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

参数:

  • n (int ) – 行数

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

  • out (Tensor, optinal) - Output tensor

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

返回值类型: Tensor

例子:

>>> torch.eye(3)
 1  0  0
 0  1  0
 0  0  1
[torch.FloatTensor of size 3x3]

Torch定义了:

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

更多

PyTorch官网
七种CPU tensor类型和八种GPU tensor类型
torch
参考Pytorch 文档