#卷积层
import torch
from torch.utils.data.dataloader import DataLoader
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.tensorboard import SummaryWriter
data_set=torchvision.datasets.CIFAR10(root="./data_set",train=False,transform=torchvision.transforms.ToTensor(),download=True)
data_load = DataLoader(data_set,batch_size=64)
class Tudui(nn.Module):
def __init__(self) -> None:
super().__init__()
self.conv1=Conv2d(in_channels=3,out_channels=6,kernel_size=3,stride=1,padding=0)
pass
def forward(self,x):
x=self.conv1(x)
return x
pass
pass
tudui=Tudui()
# print(tudui)
writer=SummaryWriter("conv2d")
step=0
for data in data_load:
# torch.Size([64, 3, 32, 32])
imgs,targets=data
# torch.Size([64, 6, 30, 30])
output=tudui(imgs)
# torch.Size([64, 6, 30, 30])-->torch.Size([128, 3, 30, 30])
output=torch.reshape(output,(-1,3,30,30))
writer.add_images("input",imgs,step)
writer.add_images("output",output,step)
step+=1
# print(imgs.shape)
# print(output.shape)
writer.close()
#池化层
import torchvision
from torch import nn
import torch
import torch.nn.functional as F
from torch.nn.modules import padding
from torch.nn import MaxPool2d
from torch.utils.data.dataloader import DataLoader
from torch.utils.tensorboard import SummaryWriter
data_set=torchvision.datasets.CIFAR10(root="./data_set",train=False,transform=torchvision.transforms.ToTensor(),download=True)
data_load = DataLoader(data_set,batch_size=64)
class Tudui(nn.Module):
def __init__(self):
super().__init__()
self.maxpool1=MaxPool2d(kernel_size=3,ceil_mode=True)
pass
def forward(self,input):
output=self.maxpool1(input)
return output
pass
# 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)
# kernel=torch.tensor([[1,2,1],
# [0,1,0],
# [2,1,0]])
# input=torch.reshape(input,(1,1,5,5))
# kernel=torch.reshape(kernel,(1,1,3,3))
# tudui=Tudui()
# output=tudui(input)
# print(output)
tudui=Tudui()
writer=SummaryWriter("pool")
step=0
for data in data_load:
imgs,targets=data
writer.add_images("input",imgs,step)
output=tudui(imgs)
writer.add_images("output",output,step)
step+=1
pass
writer.close()
#激活函数
import torchvision
from torch import nn
import torch
import torch.nn.functional as F
from torch.nn.modules import padding,ReLU,Sigmoid
from torch.nn import MaxPool2d
from torch.utils.data.dataloader import DataLoader
from torch.utils.tensorboard import SummaryWriter
data_set=torchvision.datasets.CIFAR10(root="./data_set",train=False,transform=torchvision.transforms.ToTensor(),download=True)
data_load = DataLoader(data_set,batch_size=64)
class Tudui(nn.Module):
def __init__(self):
super().__init__()
self.relu1=ReLU()
self.sigmoid1=Sigmoid()
pass
def forward(self,input):
output=self.sigmoid1(input)
return output
pass
tudui=Tudui()
writer=SummaryWriter("activate")
step=0
for data in data_load:
imgs,targets=data
writer.add_images("input",imgs,step)
output=tudui(imgs)
writer.add_images("output",output,step)
step+=1
pass
writer.close()
|