Dataloder
import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
#准备测试集
test_data = torchvision.datasets.CIFAR10(root='./CIFAR10', train=False, transform=torchvision.transforms.ToTensor())
test_loder = DataLoader(dataset =test_data, batch_size=64, shuffle=True, drop_last=False)
writer = SummaryWriter("dataloder")
step = 0
for data in test_loder:
imgs, targets = data
writer.add_images('test_loder', imgs, step)
step = step + 1
writer.close()
nn.Module
import torch.nn as nn
import torch.nn.functional as F
import torch
class Zzx(nn.Module):
def __init__(self):
super(Zzx, self).__init__()
def forward(self,input):
output = input + 1
return output
zzx = Zzx()
x = torch.tensor(1)
output = zzx(x)
print(output)
nn.conv2d
import torchvision
from torch.utils.data import DataLoader
import torch.nn as nn
from torch.nn import Conv2d
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10(root='./CIFAR10',train=False,transform=torchvision.transforms.ToTensor(),download=False)
dataloder = DataLoader(dataset, batch_size=64,)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, 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
tudui = Tudui()
step = 0
writer = SummaryWriter("conv2d")
for data in dataloder:
imgs , targets = data
output = tudui(imgs)
print(imgs.shape)
print(output.shape)
#torch.size([64,3,32,32])
writer.add_images("input",imgs,)
# torch.size([64,6,30,30])
output =torch.reshape(output,(-1,3,30,30)) #彩色图像是三个channel,6个显示不了,所以需要reshape reshape中的-1是自动匹配其他系数
writer.add_images("output",output)
step = step + 1
nn.maxpool
import torch.nn as nn
import torch
import torchvision
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10(root='./CIFAR10',train = False,
transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64,)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.maxpool = MaxPool2d(kernel_size=3, ceil_mode=True)
def forward(self,x):
x = self.maxpool(x)
return x
tudui = Tudui()
writer = SummaryWriter("logs_maxpooling")
step = 0
for data in dataloader:
imgs , tragets = data
writer.add_images("input", imgs, step)
output =tudui(imgs)
writer.add_images("output",output,step)
step = step + 1
writer.close()
nn.ReLU
import torch
import torch.nn as nn
import torchvision
from torch.nn import ReLU, Sigmoid
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10(root='./CIFAR10',train=False,
transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64, drop_last=True)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.relu1 = ReLU()
self.sigmoid1 =Sigmoid()
def forward(self,x):
#x = self.relu1(x)
x = self.sigmoid1(x)
return x
tudui = Tudui()
writer = SummaryWriter("logs_Sigmoid1")
step = 0
for data in dataloader:
imgs, targets = data
writer.add_images("input1", imgs, global_step=step)
output = tudui(imgs)
writer.add_images("output1", output, step)
step += 1
writer.close()
if want same width and length of this image after Conv2d padding=(kernel_size-1)/2 if stride=1
交叉熵函数计算公式