神经网络-搭建小实战和Sequential的使用

CIFAR-10 model structure

在这里插入图片描述

通过已知参数(高、宽、dilation=1、kernel_size)推断stride和padding的大小

在这里插入图片描述

网络

import torch
from torch import nn

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=5, stride=1, padding=2)
        self.maxpool1 = nn.MaxPool2d(kernel_size=2)
        self.conv2 = nn.Conv2d(in_channels=32, out_channels=32, kernel_size=5, stride=1, padding=2)
        self.maxpool2 = nn.MaxPool2d(2)
        self.conv3 = nn.Conv2d(32, 64, 5,padding=2)
        self.maxpool3 = nn.MaxPool2d(2)
        self.flatten = nn.Flatten()
        self.linear1 = nn.Linear(1024, 64)
        self.linear2 = nn.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
tudui = Tudui()
print(tudui)

在这里插入图片描述

对网络进行检验

input = torch.ones((64, 3, 32, 32))
output = tudui(input)
print(output.shape) # torch.Size([64, 10])

在这里插入图片描述

线性层如果不知道输入特征是多少,注释掉线性层,查看输入特征(这里是1024)

# x = self.linear1(x)
# x = self.linear2(x)

在这里插入图片描述

使用nn.Sequential

import torch
from torch import nn

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.model1 = nn.Sequential(
            nn.Conv2d(in_channels=3, out_channels=32, kernel_size=5, stride=1, padding=2),
            nn.MaxPool2d(kernel_size=2),
            nn.Conv2d(in_channels=32, out_channels=32, kernel_size=5, stride=1, padding=2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, padding=2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(1024, 64),
            nn.Linear(64, 10)
        )
    def forward(self, x):
        x = self.model1(x)
        return x
tudui = Tudui()
print(tudui)
input = torch.ones((64, 3, 32, 32))
output = tudui(input)
print(output.shape) # torch.Size([64, 10])

在这里插入图片描述

可视化模型结构

writer = SummaryWriter('logs_seq')
writer.add_graph(tudui, input)  # 将模型的计算图添加到TensorBoard中, 这可以帮助你可视化整个模型的结构, 包括各个层之间的连接关系
writer.close()

在这里插入图片描述

相关推荐

  1. 使用torch.nn.Sequential构建神经网络

    2024-01-07 11:14:03       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-07 11:14:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-07 11:14:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-07 11:14:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-07 11:14:03       18 阅读

热门阅读

  1. 企业如何实现合理定岗定编?

    2024-01-07 11:14:03       34 阅读
  2. Centos 7 安装 ffmpeg

    2024-01-07 11:14:03       41 阅读
  3. 【华为OD真题 Python】石头剪刀布游戏

    2024-01-07 11:14:03       39 阅读
  4. MySQL 8.0中新增的功能(三)

    2024-01-07 11:14:03       38 阅读
  5. SQL Server 中 RAISERROR 的用法详解

    2024-01-07 11:14:03       33 阅读
  6. Elasticsearch地理位置数据索引

    2024-01-07 11:14:03       35 阅读
  7. 在oracle中如何删除表中数据

    2024-01-07 11:14:03       37 阅读
  8. SQL高级:事务

    2024-01-07 11:14:03       35 阅读
  9. vuex5种状态?

    2024-01-07 11:14:03       32 阅读