一、weight decay(权值衰减)的使用既不是为了提高你所说的收敛精确度也不是为了提高收敛速度,其最终目的是防止过拟合。在损失函数中,weight decay是放在正则项(regularization)前面的一个系数,正则项一般指示模型的复杂度,所以weight decay的作用是调节模型复杂度对损失函数的影响,若weight decay很大,则复杂的模型损失函数的值也就大。
![wx+b](https://i-blog.csdnimg.cn/blog_migrate/8686c016d5acd161af21e5c63af3cc7b.png)
1、提高梯度在网络中的流动。Normalization能够使特征全部缩放到[0,1],这样在反向传播时候的梯度都是在1左右,避免了梯度消失现象。
2、提升学习速率。归一化后的数据能够快速的达到收敛。
3、减少模型训练对初始化的依赖。
PyTorch 的数据处理模块是 torchvision.transform
,只对 PIL.Image 或维度为 (H, W, C) 的图片数据进行数据预处理。由于 OpenCV 读入图片的数据维度是 (H, W, C),所以不能直接使用 torchvision.transform
处理 OpenCV 的图片数据。
1. class torchvision.transforms.Compose(transforms)
输入一个 transform
列表,将多个 transform
组合使用。
transforms.Compose([transforms.CenterCrop(10),
transfroms.ToTensor()])
2. class torchvision.transforms.Scale(size, interpolation=2)
将输入的 PIL.Image
重新改变大小成给定的 size
,其中 size
是最小边长。举一个例子,如果原图的 height > width
,那么改变大小后的图片大小是 (size*height\width, size)
。
form torchvision import transforms
from PIL import Image
crop = transforms.Scale(12)
img = Image.open('test.jpg')
print type(img)
print img.size
croped_img = crop(img)
print type(crop_img)
print type(crop_img.size)
输出结果为:
<class 'PIL.PngImagePlugin.PngImageFile'>
(10, 10, 3)
<class 'PIL.Image.Image'>
(12, 12, 3)
3. class torchvision.transforms.CenterCrop(size)
将给定的 PIL.Image
进行中心切割,得到给定的 size
,size
可以是 tuple
,(target_height, target_width)
。size
也可以是一个 Integer
,在这种情况下,切出来的图片形状是正方形。
4. class torchvision.transforms.RandomCrop(size, padding=0)
切割中心点的位置随机选取。size
可以是 tuple
也可以是 Integer
。
5. class torchvision.transforms.RandomHorizontalFlip
随机水平翻转给定的 PIL.Image
,概率为 0.5。即:一半的概率翻转,一半的概率不翻转。
6. class torchvision.transforms.RandomSizedCrop(size, interpolation=2)
先将给定的 PIL.Image
随机切,然后再 resize
成给定的 size
大小
7. class torchvision.transforms.Pad(padding, fill=0)
将给定的 PIL.Image
的所有边给定的 pad value
填充。padding
:要填充多少像素,fill
: 用什么值填充
from torchvision import transforms
form PIL import Image
padding_img = transforms.Pad(padding=10, fill=0)
img = Image.open('test.jpg')
print type(img)
print img.size
padded_img = padding(img)
print type(padded_img)
print type(padded_img.size)
输出结果为:
<class 'PIL.PngImagePlugin.PngImageFile'>
(10, 10, 3)
<class 'PIL.Image.Image'>
(30, 30, 3) # 由于上下左右都要填充 10 个像素
8. class torchvision.transforms.Normalize(mean, std)
给定均值:mean = (R, G, B)
,方差:std = (R, G, B)
,将会把 Tensor 归一化。即:Normalized_image = (image - mean) / std
。
9. class torchvision.transforms.ToTensor
把一个取值范围是 [0, 255]
的 PIL.Image 或者 shape
为 (H, W, C)
的 numpy.ndarray,转换为形状为 (C, H, W)
,取值范围是 [0, 1]
的 torch.FloatTensor
data = np.random.randint(0, 255, size=300)
img = data.reshape(10, 10, 3)
print img.shape
img_tensor = transforms.ToTensor()(img) # 转换成 tensor
print img_tensor
输出结果为:
(10, 10, 3)
(3, 10, 10)
10. class torchvision.transforms.ToPILImage
将 shape
为 (C, H, W)
的 Tensor 或者 shape
为 (H, W, C)
的 numpy.ndarray 转换成 PIL.Image
,值不变。
11. class torchvision.transforms.Lambda(lambda)
通用变换,使用 lambda
作为转换器