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)
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)
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)
self.maxpool1=MaxPool2d(kernel_size=3,ceil_mode=False)
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()
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)
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()
reshape:功能更强大
for data in dataloader:
imgs,targets=data
print(imgs.shape)
output=torch.flatten(imgs)#把输入展开成一行
print(output.shape)
output=felix(output)
print(output.shape)
图像方面提供很多网络,可以直接调用
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)
writer=SummaryWriter('logs_seq')
writer.add_graph(felix,input)
writer.close()