\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Yuxuejie0912} \pdfinfo{ /Title (pytorch.pdf) /Creator (Cheatography) /Author (Yuxuejie0912) /Subject (Pytorch入门 Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A31A3A} \definecolor{LightBackground}{HTML}{F9F0F2} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Pytorch入门 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Yuxuejie0912} via \textcolor{DarkBackground}{\uline{cheatography.com/123584/cs/23342/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Yuxuejie0912 \\ \uline{cheatography.com/yuxuejie0912} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 18th June, 2020.\\ Updated 18th June, 2020.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Pytorch是什么?}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{PyTorch是一个基于 Torch 的 Python \seqsplit{开源机器学习库。具有强大的} GPU \seqsplit{加速的张量计算(运算机制类似于} \seqsplit{NumPy),包含自动求导系统,简洁优雅,大佬支持,新手友好。}} \tn % Row Count 5 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{\seqsplit{Pytorch神经网络示例}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\{\{bt\}\}import torch\{\{nl\}\}import torch.nn as nn\{\{nl\}\}from torch.autograd import Variable\{\{nl\}\}import torch.nn.functional as F} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{model = \seqsplit{nn.Sequential(nn.Linear(2}, 10), nn.ReLU(), \{\{nl\}\} nn.Linear(10, 1), nn.Sigmoid() ) \# 搭建网络} \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{if torch.cuda.is\_available():\{\{nl\}\} ~~~~~model = model.cuda() \seqsplit{\#判断GPU的cuda计算是否可用}} \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{optimzer = \seqsplit{torch.optim.SGD(model.parameters()}, lr=0.05) \# 优化器\{\{nl\}\}loss\_func = nn.MSELoss() \# 损失函数} \tn % Row Count 12 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{out = model(x)\{\{nl\}\}loss = loss\_func(out, y) \{\{nl\}\} optimzer.zero\_grad() \# 清除梯度 \{\{nl\}\} loss.backward() \# 反向传播\{\{nl\}\} optimzer.step() \# 梯度更新} \tn % Row Count 16 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Tensor and Variable}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{Tensor运算机制类似于Numpy,支持GPU加速}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{Variable是对Tensor的封装,支持神经网络图计算,具备三个属性} .data, .grad, .grad\_fn} \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{x\_tensor = torch.randn(5, 5)\{\{nl\}\}x\_var\_regular = Variable(x\_tensor, requires\_grad=True)\{\{nl\}\}x\_var\_volatile = Variable(x\_tensor, volatile=True)} \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{自动求导}} \{\{nl\}\}自动构造反向求导流图,通过requires\_grad或volatile判断Variable参数是否需要更新,常用于冻结模型或inference阶段,节省内存} \tn % Row Count 11 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{torch.nn.functional}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{torch.nn:可看作对nn.functional的类包装,同时继承了nn}.Module相关属性和方法,适合构建模型;\{\{nl\}\}torch.nn.functional:直接使用def function( \seqsplit{)定义,使用灵活,但需要手动传入weight,不利于代码复用。}} \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{conv = nn.Conv2d(3, 64, 3, 2, 1)} \tn % Row Count 7 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{output = \seqsplit{nn.functional.conv2d(inputs}, weight, bias, padding=1)} \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{torch.nn}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{专为神经网络设计的模块化接口}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\{\{bt\}\}{\bf{激活函数}}~~~~torch.nn.ReLU(inplace=False)} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\{\{bt\}\}{\bf{损失函数}}~~~~torch.nn.BCELoss(weight=None, size\_average=True, reduce=None, reduction='mean')} \tn % Row Count 6 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\{\{bt\}\}{\bf{全连接层}}~~~~torch.nn.Linear(in\_features, out\_features, bias=True)} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\{\{bt\}\}{\bf{卷积层}}~~~~~~~torch.nn.Conv2d(in\_channels, out\_channels, kernel\_size, stride=1, padding=0, dilation=1, groups=1, bias=True)} \tn % Row Count 12 (+ 4) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{卷积层代码示例:} \tn % Row Count 13 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{input = Variable(torch.randn(32, 3, 28, 28)) \{\{nl\}\}conv1 = nn.Conv2d(in\_channels=3, out\_channels=10, kernel\_size=3, stride=1, padding=1)\{\{nl\}\}conv2 = \seqsplit{nn.Conv2d(in\_channels=10}, out\_channels=128, kernel\_size=3, stride=1, padding=1)} \tn % Row Count 18 (+ 5) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\{\{bt\}\}{\bf{标准化层}}~~~~torch.nn.BatchNorm2d(num\_features, eps=1e-05, momentum=0.1, affine=True)} \tn % Row Count 21 (+ 3) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\{\{bt\}\}{\bf{池化层}}~~~~~~~torch.nn.MaxPool2d(kernel\_size, stride=None, padding=0, dilation=1, return\_indices=False, ceil\_mode=False)} \tn % Row Count 25 (+ 4) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{torch.nn.AvgPool2d(kernel\_size}, stride=None, padding=0, ceil\_mode=False, \seqsplit{count\_include\_pad=True)}} \tn % Row Count 27 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\{\{bt\}\}{\bf{Dropout层}}~~torch.nn.Dropout(p=0.5, inplace=False)} \tn % Row Count 29 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\{\{bt\}\}{\bf{插值函数}} \seqsplit{~~~~torch.nn.Upsample(size=None}, scale\_factor=None, mode='nearest', align\_corners=None)} \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{torch.nn (cont)}} \tn % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\{\{bt\}\}{\bf{容器}} \seqsplit{~~~~torch.nn.Module~~~~torch.nn.Sequential(*} args)} \tn % Row Count 3 (+ 3) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{nn.Module}}是所有神经网络的基类,定义任一网络应继承该类。\{\{nl\}\}model.modules \#返回一个包含当前模型所有模块的迭代器\{\{nl\}\}model.state\_dict() \#返回字典,保存module的所有状态\{\{nl\}\}model.forward() \#前向计算,自动调用,所有子类必须重写\{\{nl\}\}model.train() \#模型在train和evaluation模式的切换\{\{nl\}\}model.eval() \seqsplit{\#仅当存在BN层和dropout层时有影响}} \tn % Row Count 12 (+ 9) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{nn.Sequential(* args)}} \seqsplit{是一个时序容器,modules} \seqsplit{会以他们传入的顺序被添加到容器中。}} \tn % Row Count 15 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{数据预处理}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{class torchvision.transforms \seqsplit{\#实现数据增广变换}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\# \seqsplit{使用Compose将变换操作串联起来}} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{transforms.Compose({[} \seqsplit{transforms.CenterCrop(224)}, \{\{nl\}\}transforms.RandomHorizontalFlip(),\{\{nl\}\}transforms.ToTensor(),\{\{nl\}\} transforms.Normalize(0.5, 0.2) {]})} \tn % Row Count 7 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{模型的保存和加载}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{torch.save(\{ 'epoch': epoch, \{\{nl\}\} 'model\_state\_dict': model.state\_dict(), \{\{nl\}\} \seqsplit{'optimizer\_state\_dict':} optimizer.state\_dict(), \{\{nl\}\} 'loss': loss, ... \}, PATH)} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{checkpoint = torch.load(PATH) \{\{nl\}\}model.load\_state\_dict(checkpoint{[}'model\_state\_dict'{]}) \{\{nl\}\}optimizer.load\_state\_dict(checkpoint{[}'optimizer\_state\_dict'{]}) \{\{nl\}\}epoch = checkpoint{[}'epoch'{]} loss = checkpoint{[}'loss'{]}} \tn % Row Count 9 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{模型优化 torch.optim}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{torch.optim.SGD(model.parameters()}, lr = 0.01, momentum=0.9)} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{torch.optim.RMSprop(model.parameters()}, lr=0.01, alpha=0.99, eps=1e-08, weight\_decay=0, momentum=0, centered=False)} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{torch.optim.Adam(model.parameters()}, lr=0.001, betas=(0.9, 0.999), eps=1e-08, weight\_decay=0, amsgrad=False)} \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{optimizer.zero\_grad() \# \seqsplit{pytorch梯度backward累积而不是替换,每批次清零}} \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{optimizer.step() \# 模型更新} \tn % Row Count 11 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{数据集}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# 使用pytorch内置数据集\{\{nl\}\}import torchvision.datasets as datasets\{\{nl\}\}cifar10 = datasets.CIFAR10()\{\{nl\}\}torch.utils.data.DataLoader(cifar10, \seqsplit{batch\_size=args.batchSize}, shuffle=True, \seqsplit{num\_workers=args.nThreads)}} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\# 封装自定义数据集必须继承Dataset类\{\{nl\}\}from torch.utils.data.dataset import Dataset} \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{预训练模型}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import torchvision.models as models \{\{nl\}\}resnet18 = models.resnet18( )\{\{nl\}\} alexnet = models.alexnet(pretrained=True)\{\{nl\}\}vgg19 = \seqsplit{models.vgg19(pretrained=True)}} \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{可视化}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{TensorboardX}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{from tensorboardX import SummaryWriter\{\{nl\}\}with \seqsplit{SummaryWriter(comment='LeNet')} as w: \{\{nl\}\}~~~~~~~w.add\_graph(model, (input, ))\{\{nl\}\}tensorboard -{}-logdir runs} \tn % Row Count 5 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{pytorchviz}} github项目} \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{https://github.com/szagoruyko/pytorchviz}} \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}