PyTorch是深度学习的主流框架之一,新手入门相对容易。PyTorch是一个开源的Python机器学习库,其前身是2002年诞生于纽约大学 的Torch。它是美国Facebook公司使用python语言开发的一个深度学习的框架,2017年1月,Facebook人工智能研究院(FAIR)在GitHub上开源了PyTorch。
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# PyTorch登场"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Torch是什么?一个火炬!其实跟Tensorflow中Tensor是一个意思,当做是能在GPU中计算的矩阵就可以啦!如果没玩过Tensorflow,Numpy总用过吧,也可以当做是ndarray的GPU版!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![title](./img/1.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## PyTorch的飞速发展\n",
"- 框架好不好用,咱们来看图说话!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![title](./img/3.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![title](./img/4.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"PyTorch可以说是现阶段主流的深度学习框架了,武林盟主之争大概是这个历史。。。15年底之前Caffe是老大哥,随着Tensorflow的诞生,霸占江湖数载,19年起无论从学术界还是工程界PyTorch已经霸占了半壁江山!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 安装PyTorch:使用PIP的方法比较简单"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"CPU版本安装:pip install torch==1.3.0+cpu torchvision==0.4.1+cpu -f https://download.pytorch.org/whl/torch_stable.html\n",
"\n",
"GPU版本安装:pip install torch===1.3.0 torchvision===0.4.1 -f https://download.pytorch.org/whl/torch_stable (默认是CUDA10版本)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1.3.0'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import torch\n",
"torch.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 基本使用方法"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"创一个矩阵,有木有觉得很爽,如果用tensorflow的同学可能会这么觉得。。。"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[1.0469e-38, 9.3674e-39, 9.9184e-39],\n",
" [8.7245e-39, 9.2755e-39, 8.9082e-39],\n",
" [9.9184e-39, 8.4490e-39, 9.6429e-39],\n",
" [1.0653e-38, 1.0469e-38, 4.2246e-39],\n",
" [1.0378e-38, 9.6429e-39, 9.2755e-39]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = torch.empty(5, 3)\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"来个随机值试试水"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[0.1452, 0.4816, 0.4507],\n",
" [0.1991, 0.1799, 0.5055],\n",
" [0.6840, 0.6698, 0.3320],\n",
" [0.5095, 0.7218, 0.6996],\n",
" [0.2091, 0.1717, 0.0504]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = torch.rand(5, 3)\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"初始化一个全零的矩阵"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[0, 0, 0],\n",
" [0, 0, 0],\n",
" [0, 0, 0],\n",
" [0, 0, 0],\n",
" [0, 0, 0]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = torch.zeros(5, 3, dtype=torch.long)\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"直接传入数据"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([5.5000, 3.0000])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = torch.tensor([5.5, 3])\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"感觉是不是跟Numpy差不多,其实这些框架的基本操作基本都是大同小异"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[ 0.5424, -1.1208, 2.2218],\n",
" [ 0.2297, -0.0828, 1.6972],\n",
" [-3.1776, -0.4144, 0.4833],\n",
" [ 1.2763, -0.7263, -0.9817],\n",
" [-0.6833, 0.1368, 0.4485]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = x.new_ones(5, 3, dtype=torch.double) \n",
"\n",
"x = torch.randn_like(x, dtype=torch.float) \n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"展示矩阵大小"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([5, 3])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.size()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 基本计算方法"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[ 0.6497, -0.5561, 2.2990],\n",
" [ 0.5333, 0.4522, 2.1114],\n",
" [-2.4560, 0.1690, 1.2198],\n",
" [ 2.0695, -0.5944, -0.3466],\n",
" [-0.2388, 0.5630, 0.8880]])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y = torch.rand(5, 3)\n",
"x + y"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[ 0.6497, -0.5561, 2.2990],\n",
" [ 0.5333, 0.4522, 2.1114],\n",
" [-2.4560, 0.1690, 1.2198],\n",
" [ 2.0695, -0.5944, -0.3466],\n",
" [-0.2388, 0.5630, 0.8880]])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"torch.add(x, y)#一样的也是加法"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 索引"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([-1.1208, -0.0828, -0.4144, -0.7263, 0.1368])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[:, 1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### view操作可以改变矩阵维度"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])\n"
]
}
],
"source": [
"x = torch.randn(4, 4)\n",
"y = x.view(16)\n",
"z = x.view(-1, 8) \n",
"print(x.size(), y.size(), z.size())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 与Numpy的协同操作"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1., 1., 1., 1., 1.], dtype=float32)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = torch.ones(5)\n",
"b = a.numpy()\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([1., 1., 1., 1., 1.], dtype=torch.float64)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"a = np.ones(5)\n",
"b = torch.from_numpy(a)\n",
"b"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}