基础
Containers 神经网络的骨架,往里面添加不同的内容,组成神经网络
import torch
from torch import nn
class Felix(nn.Module):
def __init__(self):
super().__init__()
def forward(self,input):
output=input+1
return output
felix=Felix()
x=torch.tensor(1.0)
output=felix(x)
print(output)
卷积层
TORCH.NN.FUNCTIONAL.CONV2D
卷积层参数
Stride :卷积核在输入图像上,每次走几步。可以是单个数(横向和纵向一样),也可以是元组(横向,纵向)
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,2,3,1,1],
[2,1,0,1,1]])#几个中括号,就是几维矩阵
kernel=torch.tensor([[1,2,1],
[0,1,0],
[2,1,0]])
# print(input.shape)torch.Size([4, 5])
# print(kernel.shape)torch.Size([3, 3])尺寸大小不对,需要变换
input=torch.reshape(input,(1,1,5,5))#只有一张图,所以batch为1,灰度图像为1
kernel=torch.reshape(kernel,(1,1,3,3))
# print(input.shape)torch.Size([1, 1, 5, 5])
# print(kernel.shape)torch.Size([1, 1, 3, 3])
output=F.conv2d(input,kernel,stride=1)
print(output)
输出结果
padding:会在输入图像的左右两边进行天成有多大,默认不进行填充=0。填充的地方默认为0.
output3=F.conv2d(input,kernel,stride=1,padding=1)
print(output3)
其中1d代表是一维卷积,2d代表的是二维卷积,3d代表的是三维卷积
in_channels:输入通道数
out_channels:输出通道数
kernel_size:卷积核的大小
stride:
padding:
dilation=1:卷积核的距离
groups=1一般为1
bias:True,对卷积后的结果是否加上常数
padding_mode:填充方式,一般为zeros
前五个比较常用
公式
代码
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset=torchvision.datasets.CIFAR10('dataset',train=False,transform=torchvision.transforms.ToTensor(),download=True)
dataloader=DataLoader(dataset,batch_size=64)
class Felix(nn.Module):#创建神经网络
def __init__(self):
super(Felix,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
felix=Felix()
print(felix)
writer=SummaryWriter('logs')
step=0
for data in dataloader:
imgs,targers=data
output=felix(imgs)
# print(output.shape)
# print(imgs.shape)
writer.add_images('input',imgs,step)
output=torch.reshape(output,(-1,3,30,30))#填-1是因为不知道目标图像的该数值是多少
writer.add_images('output',output,step)
step=step+1
池化层
kernel_size:取最大值的窗口
stride:和卷积层差不多,默认值为kernel_size
dilation:空洞卷积
ceil_mode:设置为True时,设置为ceil模式(向上取整),否则为floor(向下取整)
最大池化
作用:提取特征但减少数据量
import torch
from torch import nn
from torch.nn import MaxPool2d
input=torch.tensor([[1,2,0,3,1],
[0,1,2,3,1],
[1,2,1,0,0],
[5,2,3,1,1],
[2,1,0,1,1]],dtype=torch.float32)#变成浮点数
input=torch.reshape(input,(-1,1,5,5))
# print(input.shape)torch.Size([1, 1, 5, 5])
class Felix(nn.Module):
def __init__(self):
super(Felix,self).__init__()
self.maxpool1=MaxPool2d(kernel_size=3,ceil_mode=True)
def forward(self,input):
output=self.maxpool1(input)
return output
felix=Felix()
output=felix(input)
print(output)
结果
将ceil_mode设置为False,结果如下
self.maxpool1=MaxPool2d(kernel_size=3,ceil_mode=False)
利用Tensorboard进行显示
import torch
import torchvision.datasets
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset=torchvision.datasets.CIFAR10('dataset',train=False,download=False,transform=torchvision.transforms.ToTensor())
dataloader=DataLoader(dataset,batch_size=64)
class Felix(nn.Module):
def __init__(self):
super(Felix,self).__init__()
self.maxpool1=MaxPool2d(kernel_size=3,ceil_mode=False)
def forward(self,input):
output=self.maxpool1(input)
return output
felix=Felix()
writer=SummaryWriter('logs_maxpool')
step=0
for data in dataloader:
imgs,targets=data
writer.add_images('input',imgs,step)
output=felix(imgs)
writer.add_images('output',output,step)
step=step+1
writer.close()
padding Layers
对输入图像进行填充的方式,但几乎用不到
非线性激活
RELU
inplace 替换,是否对原变量进行替换。一般设置为False,保留原始数据,防止数据丢失。
import torch
from torch import nn
from torch.nn import ReLU
input=torch.tensor([[1,-0.5],
[-1,3]])
input=torch.reshape(input,(-1,1,2,2))
# print(output.shape)torch.Size([1, 1, 2, 2])
class Felix(nn.Module):
def __init__(self):
super(Felix,self).__init__()
self.relu1=ReLU()
def forward(self,input):
output=self.relu1(input)
return output
felix=Felix()
output=felix(input)
print(output)
即大于一输出,小于一设置为0
sigmoid
利用Tensorboard进行显示
import torchvision.datasets
from torch import nn
from torch.nn import ReLU, Sigmoid
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset=torchvision.datasets.CIFAR10('dataset',train=False,download=False,transform=torchvision.transforms.ToTensor())
dataloader=DataLoader(dataset,batch_size=64)
class Felix(nn.Module):
def __init__(self):
super(Felix,self).__init__()
self.sigmoid1=Sigmoid()
def forward(self,input):
output=self.sigmoid1(input)
return output
felix=Felix()
writer=SummaryWriter('logs_sigmoid')
step=0
for data in dataloader:
imgs,targets=data
writer.add_images('input',imgs,global_step=step)
output=felix(imgs)
writer.add_images('output',output,step)
step+=1
writer.close()
线性层及其它层介绍
线性层
bias:偏置
flatten() :变成一行
reshape:功能更强大
for data in dataloader:
imgs,targets=data
print(imgs.shape)
output=torch.flatten(imgs)#把输入展开成一行
print(output.shape)
output=felix(output)
print(output.shape)
Sequential
其他
图像方面提供很多网络,可以直接调用
搭建网络模型
CIFAR 10 model
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear
class Felix(nn.Module):
def __init__(self):
super(Felix,self).__init__()
self.conv1=Conv2d(3,32,5,padding=2)#padding利用公式计算得出
self.maxpool1=MaxPool2d(2)
self.conv2=Conv2d(32,32,5,padding=2)
self.maxpool2=MaxPool2d(2)
self.conv3=Conv2d(32,64,5,padding=2)
self.maxpool3=MaxPool2d(2)
self.flatten=Flatten()
self.linear1=Linear(1024,64)
self.linear2=Linear(64,10)
def forward(self,x):
x=self.conv1(x)
x=self.maxpool1(x)
x=self.conv2(x)
x=self.maxpool2(x)
x=self.conv3(x)
x=self.maxpool3(x)
x=self.flatten(x)
x=self.linear1(x)
x=self.linear2(x)
return x
felix=Felix()
print(felix)
检查网络正确性
input=torch.ones((64,3,32,32))
output=felix(input)
print(output.shape)
结果正确
使用Sequential
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
class Felix(nn.Module):
def __init__(self):
super(Felix,self).__init__()
self.model1=Sequential(
Conv2d(3,32,5,padding=2),
MaxPool2d(2),
Conv2d(32, 32, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, padding=2),
MaxPool2d(2),
Flatten(),
Linear(1024, 64),
Linear(64, 10)
)
def forward(self,x):
x=self.model1(x)
return x
felix=Felix()
print(felix)
input=torch.ones((64,3,32,32))
output=felix(input)
print(output.shape)
利用Tensorboard
writer=SummaryWriter('logs_seq')
writer.add_graph(felix,input)
writer.close()
如图所示,双击网络可以查看结构