2025年4月2日 星期三 乙巳(蛇)年 正月初三 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > 人工智能

关于神经网络输入输出的计算

时间:01-17来源:作者:点击数:49

简单记个笔记

为什么网络模型有固定的输入维度呢?

在这里插入图片描述

计算公式

在这里插入图片描述
在这里插入图片描述
  • # 写网络架构:两种方法
  • import torch
  • from torch import nn
  • from torch.nn import Conv2d,MaxPool2d,Flatten,Linear,Sequential
  • # pytorch的nn.module 的时候从使用说明上可以知道其标准输入为 [B, C, H, W]
  • # 方法1 直接搭建
  • class MyModule(nn.Module):
  • def __init__(self):
  • # super(MyModule, self).__init__()
  • super().__init__()
  • self.conv1 = nn.Conv2d(3,32,5,padding=2)
  • self.maxpool1 = nn.MaxPool2d(2)
  • self.conv2 = nn.Conv2d(32,32,5,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
  • # 方法2 Sequantial
  • class MyModule2(nn.Module):
  • def __init__(self):
  • super().__init__()
  • self.model1 = Sequential(
  • Conv2d(3,32,5,padding=2),
  • MaxPool2d(2),
  • Conv2d(32,32,5,padding=2),
  • nn.MaxPool2d(2),
  • Conv2d(32, 64, 5, padding= 2),
  • MaxPool2d(2),
  • Flatten(),
  • Linear(1024,64),
  • Linear(64, 10)
  • )
  • def forward(self, x):
  • x = self.model1(x)
  • return x
  • mymodule = MyModule()
  • mymodule2 = MyModule2()
  • print(mymodule) #打印网络
  • # 测试网络
  • input = torch.ones((64,3,32,32))
  • print(input)
  • output = mymodule2(input) ## 出错 output 是none
  • print(output.shape)
  • print("over")
在这里插入图片描述

输出

  • MyModule(
  • (conv1): Conv2d(3, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
  • (maxpool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  • (conv2): Conv2d(32, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
  • (maxpool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  • (conv3): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
  • (maxpool3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  • (flatten): Flatten(start_dim=1, end_dim=-1)
  • (Linear1): Linear(in_features=1024, out_features=64, bias=True)
  • (Linear2): Linear(in_features=64, out_features=10, bias=True)
  • )
在这里插入图片描述
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门