PyTorch 60 分钟入门教程:PyTorch 深度学习官方入门中文教程
什么是 PyTorch?
PyTorch 是一个基于 Python 的科学计算包,主要定位两类人群:
- NumPy 的替代品,可以利用 GPU 的性能进行计算。
- 深度学习研究平台拥有足够的灵活性和速度
开始学习
Tensors (张量)
Tensors 类似于 NumPy 的 ndarrays ,同时 Tensors 可以使用 GPU 进行计算。
- from __future__ import print_function
- import torch
构造一个5x3矩阵,不初始化。
- 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)
输出:
- 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。
- 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.2183, 0.4477, -0.4053],
- [ 1.7353, -0.0048, 1.2177],
- [-1.1111, 1.0878, 0.9722],
- [-0.7771, -0.2174, 0.0412],
- [-2.1750, 1.3609, -0.3322]])
获取它的维度信息:
- print(x.size())
输出:
- torch.Size([5, 3])
注意
torch.Size
是一个元组,所以它支持左右的元组操作。
操作
在接下来的例子中,我们将会看到加法操作。
加法: 方式 1
- y = torch.rand(5, 3)
- print(x + y)
Out:
- tensor([[-0.1859, 1.3970, 0.5236],
- [ 2.3854, 0.0707, 2.1970],
- [-0.3587, 1.2359, 1.8951],
- [-0.1189, -0.1376, 0.4647],
- [-1.8968, 2.0164, 0.1092]])
加法: 方式2
- print(torch.add(x, y))
Out:
- tensor([[-0.1859, 1.3970, 0.5236],
- [ 2.3854, 0.0707, 2.1970],
- [-0.3587, 1.2359, 1.8951],
- [-0.1189, -0.1376, 0.4647],
- [-1.8968, 2.0164, 0.1092]])
加法: 提供一个输出 tensor 作为参数
- result = torch.empty(5, 3)
- torch.add(x, y, out=result)
- print(result)
Out:
- tensor([[-0.1859, 1.3970, 0.5236],
- [ 2.3854, 0.0707, 2.1970],
- [-0.3587, 1.2359, 1.8951],
- [-0.1189, -0.1376, 0.4647],
- [-1.8968, 2.0164, 0.1092]])
加法: in-place
- # adds x to y
- y.add_(x)
- print(y)
Out:
- tensor([[-0.1859, 1.3970, 0.5236],
- [ 2.3854, 0.0707, 2.1970],
- [-0.3587, 1.2359, 1.8951],
- [-0.1189, -0.1376, 0.4647],
- [-1.8968, 2.0164, 0.1092]])
Note
注意
任何使张量会发生变化的操作都有一个前缀 ''。例如:x.copy
(y)
, x.t_()
, 将会改变 x
.
你可以使用标准的 NumPy 类似的索引操作
- print(x[:, 1])
Out:
- tensor([ 0.4477, -0.0048, 1.0878, -0.2174, 1.3609])
改变大小:如果你想改变一个 tensor 的大小或者形状,你可以使用 torch.view
:
- 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())
Out:
- torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
如果你有一个元素 tensor ,使用 .item() 来获得这个 value 。
- x = torch.randn(1)
- print(x)
- print(x.item())
Out:
- tensor([ 0.9422])
- 0.9422121644020081
http://pytorchchina.com/2018/12/11/pytorch-windows-install-1/
PyTorch Mac 安装教程
http://pytorchchina.com/2018/12/11/pytorch-mac-install/
PyTorch Linux 安装教程
http://pytorchchina.com/2018/12/11/pytorch-linux-install/

http://pytorchchina.com/2018/06/25/what-is-pytorch/
PyTorch 60 分钟入门教程:PyTorch 深度学习官方入门中文教程的更多相关文章
- PyTorch 60 分钟入门教程
PyTorch 60 分钟入门教程:PyTorch 深度学习官方入门中文教程 http://pytorchchina.com/2018/06/25/what-is-pytorch/ PyTorch 6 ...
- 知识图谱与机器学习 | KG入门 -- Part1-b 图深度学习
介绍 我们正在定义一种新的机器学习方法,专注于一种新的范式 -- Data Fabric. 在上一篇文章中,我们对机器学习给出了新的定义: 机器学习是一种自动发现Data Fabric中隐藏的&quo ...
- 深度学习动手入门:GitHub上四个超棒的TensorFlow开源项目
作者简介:akshay pai,数据科学工程师,热爱研究机器学习问题.Source Dexter网站创办人. TensorFlow是Google的开源深度学习库,你可以使用这个框架以及Python编程 ...
- 深度学习开发环境搭建教程(Mac篇)
本文将指导你如何在自己的Mac上部署Theano + Keras的深度学习开发环境. 如果你的Mac不自带NVIDIA的独立显卡(例如15寸以下或者17年新款的Macbook.具体可以在"关 ...
- (转)Deep Learning深度学习相关入门文章汇摘
from:http://farmingyard.diandian.com/post/2013-04-07/40049536511 来源:十一城 http://elevencitys.com/?p=18 ...
- PyTorch 60 分钟入门教程:数据并行处理
可选择:数据并行处理(文末有完整代码下载) 作者:Sung Kim 和 Jenny Kang 在这个教程中,我们将学习如何用 DataParallel 来使用多 GPU. 通过 PyTorch 使用多 ...
- pytorch入门--土堆深度学习快速入门教程
工具函数 dir函数,让我们直到工具箱,以及工具箱中的分隔区有什么东西 help函数,让我们直到每个工具是如何使用的,工具的使用方法 示例:在pycharm的console环境,输入 import t ...
- 深度学习之入门Pytorch(1)------基础
目录: Pytorch数据类型:Tensor与Storage 创建张量 tensor与numpy数组之间的转换 索引.连接.切片等 Tensor操作[add,数学运算,转置等] GPU加速 自动求导: ...
- Pyplot教程(深度学习入门3)
源地址:http://matplotlib.org/users/pyplot_tutorial.html .caret, .dropup > .btn > .caret { border- ...
随机推荐
- 【JDBC】Servlet实例
import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.Dri ...
- 我的java web之路(配置开发环境)
这个学期学习java web开发,当然不能只靠上机的时间来练习,于是,便在自己的电脑上安装一系列的软件... 1.JDK的安装.首先,下载Java JDK,可以到sun公司网站下载.一定要下载适合自己 ...
- mybatis插件 mybatis插件-------从dao快速定位到mapper的sql语句
步骤一:打开settings,点击plugins 快捷键ctrl+alt+s打开settings 步骤二.点击ClearCase Integration,并点击下面中间的按钮(browse repos ...
- zoj 2722 Head-to-Head Match(两两比赛)
Head-to-Head Match Time Limit: 2 Seconds Memory Limit: 65536 KB Our school is planning to hold ...
- gitHub网站上常见英语翻译2
repositories资料库 compilers with rich code analysis APIs.编译器具有丰富的代码分析API. plugins插件 With a variety of ...
- [POJ2446] Chessboard(二分图最大匹配-匈牙利算法)
传送门 把所有非障碍的相邻格子彼此连一条边,然后求二分图最大匹配,看 tot * 2 + k 是否等于 n * m 即可. 但是连边不能重复,比如 a 格子 和 b 格子 相邻,不能 a 连 b ,b ...
- Hibernate 批处理(batch inserts, updates and deletes)
总结:hibernate在进行批量处理不给力的主要原因就是Session中存在缓存,而hibernate的机制就是通过session中的一级缓存去同步数据库,所以当进行批量处理时,缓存中保存的数据量很 ...
- [转]GitHub 优秀的 Android 开源项目
GitHub 优秀的 Android 开源项目 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageV ...
- Linux内核设计与实现——读书笔记2:进程管理
1.进程: (1)处于执行期的程序,但不止是代码,还包括各种程序运行时所需的资源,实际上进程是正在执行的 程序的实时结果. (2)程序的本身并不是进程,进程是处于执行期的程序及其相关资源的总称. (3 ...
- 【转载】ubuntu16.04 无线/Wifi 上网速度慢的解决方法
原文链接:http://tieba.baidu.com/p/4737599703[侵删] 一直以为是域名解析的问题,可也觉得不像.今天在百度搜索“ubuntu16.04域名解析慢”的时候无意中看到了h ...