2021-09-16 pytorch

 

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

交叉熵函数计算公式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值