P17-18:神经网络基本骨架以及卷积层(Pytorch小土堆学习笔记)

官方文档在docs->torch.nn->Containers->Module

import torch.nn as nn
import torch.nn.functional as F

class Model(nn.Module):
    def __init__(self):                                             #初始化
        super().__init__()                                         #继承父类torch.nn的初始化
        self.conv1 = nn.Conv2d(1, 20, 5)                #卷积模型1
        self.conv2 = nn.Conv2d(20, 20, 5)              #卷积模型2

    def forward(self, x):
        x = F.relu(self.conv1(x))                               #对x卷积1然后非线性         
        return F.relu(self.conv2(x))                          #对下x`卷积然后非线性

forward在定义不同神经网络的时候需要重写 

补充一下卷积以及在python中实现的过程(conv2d这个函数的使用)

这个是torch.nn.functional里面的conv2d参数说明

input:输入,但要注意输入的格式(minibatch,channels,h,w),通常情况下在输入之前需要将图像通过reshape转换成我们如上形式

weight:卷积核(kernel) 

bias:偏置,对卷积后的结果是否加上一个常数,通常可以设置为true

stride:步长,卷积每一次移动的格子数(下面是以stride=1的例子)

padding:周围是否填充0

这个是torch.nn中conv2d说明

这是卷积的原理(本来是动图) 

 

 

一个具体的例子: 

 

 

 原理如上几张图所示,下面是实现的代码

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]])
input2 = torch.reshape(input,[1,1,5,5])  #此函数是将输入类型进行转换
kernel2 = torch.reshape(kernel,(1,1,3,3))
print(input.shape)
print(input2.shape)

output = F.conv2d(input2,kernel2,stride=1)
print(output)

解释一下reshape函数(1,1,5,5):one batch_size, one channel, matrix size(5*5)

如果不进行转换直接输入会出现下面的报错

RuntimeError: weight should have at least three dimensions

最终编译结果: 

torch.Size([5, 5])
torch.Size([1, 1, 5, 5])
tensor([[[[10, 12, 12],
          [18, 16, 16],
          [13,  9,  3]]]])

第二个例子:(上一个例子用的是nn.funcational.conv2d,下面这个例子用的是nn.conv2d;这个例子对神经网络进行了封装,在面对更复杂的卷积网络场景的时候更适合)

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

dataset2 = torchvision.datasets.CIFAR10("./dataset2",train=False,transform=torchvision.transforms.ToTensor(),
                                      download=True)
dataloader = DataLoader(dataset2,batch_size=64)

class qiqi(nn.Module):
    def __init__(self):
        super(qiqi, 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

KiKi = qiqi()
writer = SummaryWriter("nn_conv2")
step=0
for data in dataloader:
    imgs,targets = data
    output =KiKi(imgs)
    print(imgs.shape)
    print(output.shape)
    #torch.Size([64,6,32,32])
    writer.add_images("input",imgs,step)
    #torch.size([64,6,30,30] --> [xxx,3,30,30])
    output = torch.reshape(output,(-1,3,30,30))
    writer.add_images("output",output,step)
    step=step+1

 qiqi这个卷积网络就是对输入做卷积,然后要求输入3通道输出6通道

在tensorboard上的样子:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值