什么是 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
PyTorch windows 安装教程:两行代码搞定 PyTorch 安装
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/
PyTorch QQ群

http://pytorchchina.com/2018/06/25/what-is-pytorch/

PyTorch 60 分钟入门教程:PyTorch 深度学习官方入门中文教程的更多相关文章

  1. PyTorch 60 分钟入门教程

    PyTorch 60 分钟入门教程:PyTorch 深度学习官方入门中文教程 http://pytorchchina.com/2018/06/25/what-is-pytorch/ PyTorch 6 ...

  2. 知识图谱与机器学习 | KG入门 -- Part1-b 图深度学习

    介绍 我们正在定义一种新的机器学习方法,专注于一种新的范式 -- Data Fabric. 在上一篇文章中,我们对机器学习给出了新的定义: 机器学习是一种自动发现Data Fabric中隐藏的&quo ...

  3. 深度学习动手入门:GitHub上四个超棒的TensorFlow开源项目

    作者简介:akshay pai,数据科学工程师,热爱研究机器学习问题.Source Dexter网站创办人. TensorFlow是Google的开源深度学习库,你可以使用这个框架以及Python编程 ...

  4. 深度学习开发环境搭建教程(Mac篇)

    本文将指导你如何在自己的Mac上部署Theano + Keras的深度学习开发环境. 如果你的Mac不自带NVIDIA的独立显卡(例如15寸以下或者17年新款的Macbook.具体可以在"关 ...

  5. (转)Deep Learning深度学习相关入门文章汇摘

    from:http://farmingyard.diandian.com/post/2013-04-07/40049536511 来源:十一城 http://elevencitys.com/?p=18 ...

  6. PyTorch 60 分钟入门教程:数据并行处理

    可选择:数据并行处理(文末有完整代码下载) 作者:Sung Kim 和 Jenny Kang 在这个教程中,我们将学习如何用 DataParallel 来使用多 GPU. 通过 PyTorch 使用多 ...

  7. pytorch入门--土堆深度学习快速入门教程

    工具函数 dir函数,让我们直到工具箱,以及工具箱中的分隔区有什么东西 help函数,让我们直到每个工具是如何使用的,工具的使用方法 示例:在pycharm的console环境,输入 import t ...

  8. 深度学习之入门Pytorch(1)------基础

    目录: Pytorch数据类型:Tensor与Storage 创建张量 tensor与numpy数组之间的转换 索引.连接.切片等 Tensor操作[add,数学运算,转置等] GPU加速 自动求导: ...

  9. Pyplot教程(深度学习入门3)

    源地址:http://matplotlib.org/users/pyplot_tutorial.html .caret, .dropup > .btn > .caret { border- ...

随机推荐

  1. DB2表空间

    https://www.ibm.com/developerworks/cn/data/library/techarticles/dm-0902yuancg/ 临时表空间的使用 (sorts or jo ...

  2. js模式(一):单例模式

    function Universe(){ var instance; Universe = function (){ return instance; } Universe.prototype = t ...

  3. XHR2:js异步上传

    http://dev.opera.com/articles/xhr2/ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 ...

  4. Leetcode 260.只出现一次的数字III

    只出现一次的数字III 给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次. 找出只出现一次的那两个元素. 示例 : 输入: [1,2,1,3,2,5] 输出: [3,5 ...

  5. [Go]指针操作

    指针类型比较常见 type Dog struct { name string } func (dog *Dog) SetName (name string){ dog.name = name } 对于 ...

  6. SPOJ FAVDICE 数学期望

    题目大意: 一个有n面的色子抛掷多少次能使所有面都能被抛到过,求期望值 总面数为n,当已经抛到过 i 个不同面时,我们抛出下一个不同面的概率为 (n-i)/n,那么抛的次数为 n/(n-i) 将所有抛 ...

  7. 一个SAM的样例

    \[s=abcbacbcb\\ \begin{split} p \quad& fa \quad& Substrings \quad& Right \\ 1 \quad& ...

  8. 洛谷P3973 - [TJOI2015]线性代数

    Portal Description 给定一个\(n\times n\)的矩阵\(B\)和一个\(1×n\)的矩阵\(C\).求出一个\(1×n\)的01矩阵\(A\),使得\(D=(A×B-C)×A ...

  9. HDU 3527 SPY

    http://poj.org/problem?id=3615 基础题 狂STL #include <bits/stdc++.h> using namespace std; set<s ...

  10. mac上storm standalone安装

    一.安装storm 下载storm http://storm.apache.org/downloads.html export STORM_HOME=/Users/huangjiahong/Docum ...