赞
踩
今天分享的学习内容主要就是神经网络里面的知识啦,用到的框架就是torch
在这里我也是对自己做一个学习记录,如果不符合大家的口味,大家划走就可以啦
可能没有什么文字或者原理上的讲解,基本上都是代码,但是我还是想说,如果基础不是很好,认认真真敲一遍,会有不一样的感受!!
在这里还有一篇相关内容的补充,大家也可以看一看:
由浅入深,走进深度学习(补充篇:神经网络基础)-CSDN博客
由浅入深,走进深度学习(补充篇:神经网络结构层基础)-CSDN博客
主要内容:
目录
正片开始
内容六 卷积原理、卷积层、卷积层处理图片
- import torch
- import torch.nn.functional as F
-
- input = torch.tensor([[1, 2, 0, 3, 1],
- [0, 1, 2, 3, 1],
- [1, 2, 1, 0, 0],
- [5, 6, 2, 2, 1],
- [3, 2, 3, 5, 1]])
-
- kernel = torch.tensor([[1, 2, 1],
- [2, 3, 1],
- [3, 0, 1]])
-
- print(input.shape)
- print(kernel.shape)
-
- input = torch.reshape(input, (1, 1, 5, 5))
- kernel = torch.reshape(kernel, (1, 1, 3, 3))
- print(input.shape)
- print(input)
- print(kernel.shape)
- print(kernel)
-
- output = F.conv2d(input, kernel, stride = 1)
- print(output.shape)
- print(output)
-
-
- import torch
- import torch.nn.functional as F
-
- input = torch.tensor([[1, 2, 0, 3, 1],
- [0, 1, 2, 3, 1],
- [1, 2, 1, 0, 0],
- [5, 6, 2, 2, 1],
- [3, 2, 3, 5, 1]])
-
- kernel = torch.tensor([[1, 2, 1],
- [2, 3, 1],
- [3, 0, 1]])
-
- print(input.shape)
- print(kernel.shape)
-
- input = torch.reshape(input, (1, 1, 5, 5))
- kernel = torch.reshape(kernel, (1, 1, 3, 3))
- print(input.shape)
- print(input)
- print(kernel.shape)
- print(kernel)
-
- output = F.conv2d(input, kernel, stride = 2)
- print(output.shape)
- print(output)
-
-
- # 步幅、填充原理
- # 步幅:卷积核经过输入特征图的采样间隔。设置步幅的目的:希望减小输入参数的数目,减少计算量
- # 填充:在输入特征图的每一边添加一定数目的行列。设置填充的目的:希望每个输入方块都能作为卷积窗口的中心,或使得输出的特征图的长、宽 = 输入的特征图的长、宽。
- # 一个尺寸 a * a 的特征图,经过 b * b 的卷积层,步幅(stride)= c,填充(padding)= d,若d等于0,也就是不填充,输出的特征图的尺寸 =(a-b)/ c+1;若d不等于0,也就是填充,输出的特征图的尺寸 =(a+2d-b)/ c+1
- import torch
- import torch.nn.functional as F
-
- input = torch.tensor([[1, 2, 0, 3, 1],
- [0, 1, 2, 3, 1],
- [1, 2, 1, 0, 0],
- [5, 6, 2, 2, 1],
- [3, 2, 3, 5, 1]])
-
- kernel = torch.tensor([[1, 2, 1],
- [2, 3, 1],
- [3, 0, 1]])
-
- print(input.shape)
- print(kernel.shape)
-
- input = torch.reshape(input, (1, 1, 5, 5))
- kernel = torch.reshape(kernel, (1, 1, 3, 3))
- print(input.shape)
- print(input)
- print(kernel.shape)
- print(kernel)
-
- output = F.conv2d(input, kernel, stride = 1, padding = 1) # 周围只填充一层
- print(output.shape)
- print(output)
-
-
- # 内容六 卷积层
- # Conv1d代表一维卷积,Conv2d代表二维卷积,Conv3d代表三维卷积
- # kernel_size在训练过程中不断调整,定义为3就是3 * 3的卷积核,实际我们在训练神经网络过程中其实就是对kernel_size不断调整
-
- import torch
- from torch import nn
- from torch.nn import Conv2d
- from torch.utils.data import DataLoader
- import torchvision
-
- # dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
- # dataloader = DataLoader(dataset, batch_size = 64)
-
- class net(nn.Module):
- def __init__(self):
- super(net, self).__init__()
- self.conv1 = Conv2d(in_channels = 3, out_channels = 6, kernel_size = 3, stride = 1, padding = 0) # 彩色图像输入为3层,我们想让它的输出为6层,选3 * 3 的卷积
-
- def forward(self, x):
- x = self.conv1
-
- return x
-
-
- model = net()
- print(model)
-
-
- # 卷积层处理图片
- import torch
- from torch import nn
- from torch.nn import Conv2d
- from torch.utils.data import DataLoader
- import torchvision
-
- dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
- dataloader = DataLoader(dataset, batch_size = 64)
-
- class net(nn.Module):
- def __init__(self):
- super(net, self).__init__()
- self.conv1 = Conv2d(in_channels = 3, out_channels = 6, kernel_size = 3, stride = 1, padding = 0)
-
- def forward(self, x):
- x = self.conv1(x)
- return x
-
- model = net()
- for data in dataloader:
- img, targets = data
- output = model(img)
- # print(img.shape)
- # print(output.shape) # 输入为3通道32×32的64张图片
- # print(targets.shape) # 输出为6通道30×30的64张图片
内容七 最大池化层
- # 最大池化层有时也被称为下采样 dilation为空洞卷积
- # Ceil_model为当超出区域时,只取最左上角的值
- # 池化使得数据由5 * 5 变为3 * 3,甚至1 * 1的,这样导致计算的参数会大大减小。例如1080P的电影经过池化的转为720P的电影、或360P的电影后,同样的网速下,视频更为不卡
- import torch
- from torch import nn
- from torch.nn import MaxPool2d
-
- input = torch.tensor([[3, 4, 6, 1, 8],
- [4, 0, 8, 0, 1],
- [1, 2, 4, 5, 1],
- [2, 3, 1, 5, 1],
- [3, 3, 1, 5, 0]], dtype = torch.float32)
-
- input = torch.reshape(input, (-1, 1, 5, 5))
- print(input.shape)
-
- class net(nn.Module):
- def __init__(self):
- super(net, self).__init__()
- self.maxpool = MaxPool2d(kernel_size = 3, ceil_mode = True)
-
- def forward(self, x):
- x = self.maxpool(x)
- return x
-
- model = net()
- output = model(input)
- print(output.shape)
- print(output)
-
-
-
- import torch
- import torchvision
- from torch import nn
- from torch.nn import MaxPool2d
- from torch.utils.data import DataLoader
-
- dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
- dataloader = DataLoader(dataset, batch_size = 64)
-
- class net(nn.Module):
- def __init__(self):
- super(net, self).__init__()
- self.maxpool = MaxPool2d(kernel_size = 3, ceil_mode = True)
-
- def forward(self, x):
- x = self.maxpool(x)
- return x
-
- model = net()
- epoch = 0
-
- for data in dataloader:
- img, tagets = data
- # print('input', img, epoch)
- output = model(img)
- # print('output', output, epoch)
- epoch = epoch + 1
内容八 非线性激活
- # inplace为原地替换,若为True,则变量的值被替换。若为False,则会创建一个新变量,将函数处理后的值赋值给新变量,原始变量的值没有修改
- import torch
- from torch import nn
- from torch.nn import ReLU
-
- input = torch.tensor([[1, -2],
- [-0.7, 3]])
-
- input = torch.reshape(input, (-1, 1, 2, 2))
- print(input.shape)
-
- class net(nn.Module):
- def __init__(self):
- super(net, self).__init__()
- self.relu = ReLU()
-
- def forward(self, x):
- x = self.relu(x)
- return x
-
- model = net()
- output = model(input)
- print(output.shape)
- print(output)
- print(output[0][0][1][1])
-
-
-
- import torch
- import torchvision
- from torch import nn
- from torch.nn import ReLU, Sigmoid
- from torch.utils.data import DataLoader
-
- dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
- dataloader = DataLoader(dataset, batch_size = 64)
-
- class net(nn.Module):
- def __init__(self):
- super(net, self).__init__()
- self.relu = ReLU()
- self.sigmoid = Sigmoid()
-
- def forward(self, x):
- x1 = self.relu(x)
- x2 = self.sigmoid(x1)
- return x2
-
- model = net()
- epoch = 0
-
- for data in dataloader:
- imgs, targets = data
- output = model(imgs)
- # print(output.shape)
- epoch = epoch + 1
内容九 线性层以及其他层
- # 线性拉平
- import torch
- import torchvision
- from torch import nn
- from torch.nn import ReLU, Sigmoid
- from torch.utils.data import DataLoader
-
- dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
- dataloader = DataLoader(dataset, batch_size = 64)
-
- for data in dataloader:
- imgs, targets = data
- # print(imgs.shape)
- output = torch.reshape(imgs, (1, 1, 1, -1))
- # print(output.shape)
-
-
- # 线性层
- import torch
- import torchvision
- from torch import nn
- from torch.nn import Linear
- from torch.utils.data import DataLoader
-
- dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
- dataloader = DataLoader(dataset, batch_size = 64, drop_last=True)
- # drop_last=True:如果设置为 True,则当数据集的大小不能被 batch_size 整除时,会丢弃最后一个不足一个批次的数据
- # drop_last=False:如果设置为 False(也是默认值),则当数据集的大小不能被 batch_size 整除时,最后一个批次会包含剩下的样本,可能少于 batch_size
- class net(nn.Module):
- def __init__(self):
- super(net, self).__init__()
- self.linear = Linear(196608, 10)
-
- def forward(self, x):
- x = self.linear(x)
- return x
-
- model = net()
- epoch = 0
-
- for data in dataloader:
- imgs, targets = data
- # print(imgs.shape)
- imgs_reshape = torch.reshape(imgs, (1, 1, 1, -1)) # 方法一 拉平
- # print(imgs_reshape.shape)
- output = model(imgs_reshape)
- # print(output.shape)
- # epoch = epoch + 1
-
- # 线性层
- import torch
- import torchvision
- from torch import nn
- from torch.nn import Linear
- from torch.utils.data import DataLoader
-
- dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
- dataloader = DataLoader(dataset, batch_size = 64, drop_last=True)
- # drop_last=True:如果设置为 True,则当数据集的大小不能被 batch_size 整除时,会丢弃最后一个不足一个批次的数据
- # drop_last=False:如果设置为 False(也是默认值),则当数据集的大小不能被 batch_size 整除时,最后一个批次会包含剩下的样本,可能少于 batch_size
- class net(nn.Module):
- def __init__(self):
- super(net, self).__init__()
- self.linear = Linear(196608, 20)
-
- def forward(self, x):
- x = self.linear(x)
- return x
-
- model = net()
- epoch = 0
-
- for data in dataloader:
- imgs, targets = data
- # print(imgs.shape)
- imgs_flatten = torch.flatten(imgs) # 方法二 拉平展为一维
- # print(imgs_flatten.shape)
- output = model(imgs_flatten)
- # print(output.shape)
- # epoch = epoch + 1
内容十 实战,搭建一个小型的神经网络
- # 把网络结构放在Sequential里面,好处就是代码写起来比较简介、易懂
- # 可以根据神经网络每层的尺寸,根据下图的公式计算出神经网络中的参数
- import torch
- import torchvision
- from torch import nn
- from torch.nn import Linear, Conv2d, MaxPool2d, Flatten
- from torch.utils.data import DataLoader
-
- # dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
- # dataloader = DataLoader(dataset, batch_size = 64, drop_last=True)
-
- class net(nn.Module):
- def __init__(self):
- super(net, self).__init__()
- self.conv1 = Conv2d(in_channels = 3, out_channels = 32, kernel_size = 5, stride = 1, padding = 2)
- self.maxpool1 = MaxPool2d(kernel_size = 2, ceil_mode = True)
- self.conv2 = Conv2d(in_channels = 32, out_channels = 32, kernel_size = 5, stride = 1, padding = 2)
- self.maxpool2 = MaxPool2d(kernel_size = 2, ceil_mode = True)
- self.conv3 = Conv2d(in_channels = 32, out_channels = 64, kernel_size = 5, stride = 1, padding = 2)
- self.maxpool3 = MaxPool2d(kernel_size = 2, ceil_mode = True)
- self.flatten = Flatten()
- self.linear1 = Linear(1024, 64)
- self.linear2 = Linear(64, 10)
-
- def forward(self, x):
- x = self.conv1(x)
- print(x.shape)
- x = self.maxpool1(x)
- print(x.shape)
- x = self.conv2(x)
- print(x.shape)
- x = self.maxpool2(x)
- print(x.shape)
- x = self.conv3(x)
- print(x.shape)
- x = self.maxpool3(x)
- print(x.shape)
- x = self.flatten(x)
- print(x.shape)
- x = self.linear1(x)
- print(x.shape)
- x = self.linear2(x)
- print(x.shape)
- return x
-
- model = net()
- print(model)
-
- input = torch.ones((64, 3, 32, 32))
- output = model(input)
- print(output.shape)
- # Sequential神经网络
- import torch
- import torchvision
- from torch import nn
- from torch.nn import Linear, Conv2d, MaxPool2d, Flatten, Sequential
- from torch.utils.data import DataLoader
-
- class net(nn.Module):
- def __init__(self):
- super(net, self).__init__()
- self.model = Sequential(
- Conv2d(in_channels = 3, out_channels = 32, kernel_size = 5, stride = 1, padding = 2),
- MaxPool2d(kernel_size = 2, ceil_mode = True),
- Conv2d(in_channels = 32, out_channels = 32, kernel_size = 5, stride = 1, padding = 2),
- MaxPool2d(kernel_size = 2, ceil_mode = True),
- Conv2d(in_channels = 32, out_channels = 64, kernel_size = 5, stride = 1, padding = 2),
- MaxPool2d(kernel_size = 2, ceil_mode = True),
- Flatten(),
- Linear(1024, 64),
- Linear(64, 10))
-
- def forward(self, x):
- x = self.model(x)
- return x
-
- model = net()
- print(model)
-
- input = torch.ones((64, 3, 32, 32))
- output = model(input)
- print(output.shape)
注:上述内容参考b站up主“我是土堆”的视频!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。