书籍

​https://github.com/zergtant/pytorch-handbook​​ Pytorch指南

​PyTorch 中文手册(pytorch handbook) - Pytorch中文手册​

​https://github.com/yunjey/pytorch-tutorial​​ Pytorch指南3

​Practical Deep Learning for Coders - Practical Deep Learning​

吴恩达老师的 ​​deeplearning.ai​​ 课程板书


示例

​GitHub - espectre/Kaggle_Dogs_vs_Cats_PyTorch: kaggle competition: Dogs_vs_Cats_PyTorch Presentation(Getting started with PyTorch)​

​9 个超酷的深度学习案例 - 走看看​

​GitHub - jcjohnson/neural-style: Torch implementation of neural style algorithm​


基础

​Python字符串的处理​


Tensor


import torch
a = torch.tensor([1, 2, 3, 4])
b = torch.tensor([1, 2, 3, 4])
print(a.equal(b))

# 返回结果:True


import torch
c = torch.tensor([1, 2, 2, 3])
d = torch.tensor([2, 2, 3, 3])
print(c.eq(d))

# 返回结果:tensor([0, 1, 0, 1], dtype=torch.uint8)

​torch.max()使用讲解​

​Pytorch笔记torch.max() - 知乎​


函数

​Pytorch autograd,backward详解 - 知乎​

​python魔法方法之__setattr__() - 知乎​

class Module:
def __setattr__(self, name: str, value: Union[Tensor, 'Module']) -> None:
def remove_from(*dicts_or_sets):
for d in dicts_or_sets:
if name in d:
if isinstance(d, dict):
del d[name]
else:
d.discard(name)

params = self.__dict__.get('_parameters')
if isinstance(value, Parameter):
if params is None:
raise AttributeError(
"cannot assign parameters before Module.__init__() call")
remove_from(self.__dict__, self._buffers, self._modules, self._non_persistent_buffers_set)
self.register_parameter(name, value)
elif params is not None and name in params:
if value is not None:
raise TypeError("cannot assign '{}' as parameter '{}' "
"(torch.nn.Parameter or None expected)"
.format(torch.typename(value), name))
self.register_parameter(name, value)
else:
modules = self.__dict__.get('_modules')
if isinstance(value, Module):
if modules is None:
raise AttributeError(
"cannot assign module before Module.__init__() call")
remove_from(self.__dict__, self._parameters, self._buffers, self._non_persistent_buffers_set)
modules[name] = value
elif modules is not None and name in modules:
if value is not None:
raise TypeError("cannot assign '{}' as child module '{}' "
"(torch.nn.Module or None expected)"
.format(torch.typename(value), name))
modules[name] = value
else:
buffers = self.__dict__.get('_buffers')
if buffers is not None and name in buffers:
if value is not None and not isinstance(value, torch.Tensor):
raise TypeError("cannot assign '{}' as buffer '{}' "
"(torch.Tensor or None expected)"
.format(torch.typename(value), name))
buffers[name] = value
else:
object.__setattr__(self, name, value)

从上可以看出,Module中对__setattr__函数进行了重载,它会收集里面的Parameter,Module,Tensor,分别放在_parameters,_modules,_buffers中,便于后续操作里面的参数,模块。


卷积


​PyTorch中的nn.Conv1d与nn.Conv2d​

​Pytorch.nn.conv2d 过程验证(单,多通道卷积过程) - 知乎​


​PyTorch中torch.linspace的详细用法 - 知乎​

​Python __new__()方法详解​


torchvision


torchvision.transforms.ToTensor :把一个取值范围是[0,255]的PIL.Image或者shape为(H,W,C)的numpy.ndarray,转换成形状为[C,H,W],取值范围是[0,1.0]的torch.FloadTensor

torchvision.transforms.ToPILImage:将shape为(C,H,W)的Tensor或shape为(H,W,C)的numpy.ndarray转换成PIL.Image,值不变。

torchvision.transforms.Normalize(mean, std):用给定的均值和标准差分别对每个通道的数据进行正则化。具体来说,给定均值(M1,…,Mn),给定标准差(S1,…,Sn),其中n是通道数(一般是3),对每个通道进行如下操作:
output[channel] = (input[channel] - mean[channel]) / std[channel]

比如:原来的tensor是三个维度的,值在[0,1]之间,经过变换之后就到了[-1,1]
计算如下:((0,1)-0.5)/0.5=(-1,1)


PIL 库

​Python 学习笔记之—— PIL 库 - 知乎​

​Pillow(PIL)入门教程(非常详细)​

from PIL import Image

# Numpy 转 Image
img = Image.fromarray(array)


# Image 转 Numpy
array = Image.fromarray(img)

matplotlib.pyplot

​Matplotlib Pyplot | 菜鸟教程​

​matplotlib.pyplot的使用总结大全(入门加进阶) - 知乎​