当前位置: 首页 > news >正文

上海做网站最专业南京网站设计优化公司

上海做网站最专业,南京网站设计优化公司,动态网页制作毕业设计,关于网站建设要花多少钱神经网络的基本骨架 1. nn.Module的使用 所有的模型都要继承 Module 类需要重写初始化函数和运算步骤函数 eg: import torch.nn as nn import torch.nn.functional as Fclass Model(nn.Module): # 继承父类Module def __init__(self): # 重写初始化函数super()…

神经网络的基本骨架

在这里插入图片描述

1. nn.Module的使用

  • 所有的模型都要继承 Module 类
  • 需要重写初始化函数和运算步骤函数

eg:

import torch.nn as nn
import torch.nn.functional as Fclass Model(nn.Module):		# 继承父类Module def __init__(self):		# 重写初始化函数super().__init__()		# 调用父类初始化self.conv1 = nn.Conv2d(1, 20, 5)self.conv2 = nn.Conv2d(20, 20, 5)def forward(self, x):		# 神经网络的运算步骤--前向传播x = F.relu(self.conv1(x))	# x->卷积->非线性return F.relu(self.conv2(x))	# x->卷积->非线性

代码示例:

import torch
from torch import nnclass Kun(nn.Module):def __init__(self):super().__init__()def forward(self, input):output = input+1	# 实现输出加1return outputkun = Kun()
x = torch.tensor(1.0)
output = kun(x)
print(output)   # tensor(2.)

2. 卷积

conv2可选参数

在这里插入图片描述

卷积计算过程示意:

在这里插入图片描述

import torch# 输入图像(5*5)
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]])  # 输入tensor数据类型的二维矩阵# 卷积核
kernel = torch.tensor([[1, 2, 1],[0, 1, 0],[2, 1, 0]])print(input.shape)
print(kernel.shape)
torch.Size([5, 5])
torch.Size([3, 3])

如果不调整尺寸会报错:Expected 3D(unbatched) or 4D(batched) input to conv2d, but got input of size: [5, 5]

所以需要调整

input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))
output = F.conv2d(input, kernel, stride=1)
print(output)--------------------------------------------------------------------------
tensor([[[[10, 12, 12],[18, 16, 16],[13,  9,  3]]]])

stride可以选择移动的步长

output2 = F.conv2d(input, kernel, stride=2)
print(output2)
----------------------------------------------------------------------------
tensor([[[[10, 12],[13,  3]]]])

padding进行填充(默认填充0)

output3 = F.conv2d(input, kernel, stride=1, padding=1)
print(output3)
-----------------------------------------------------------------------------
tensor([[[[ 1,  3,  4, 10,  8],[ 5, 10, 12, 12,  6],[ 7, 18, 16, 16,  8],[11, 13,  9,  3,  4],[14, 13,  9,  7,  4]]]])

示例代码:

import torch
import torch.nn.functional as F
# 输入图像(5*5)
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]])  # 输入tensor数据类型的二维矩阵# 卷积核
kernel = torch.tensor([[1, 2, 1],[0, 1, 0],[2, 1, 0]])
# 调整输入的尺寸
# 如果不调整尺寸会报错
# Expected 3D(unbatched) or 4D(batched) input to conv2d, but got input of size: [5, 5]
input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))
# print(input.shape)    # torch.Size([1, 1, 5, 5])
# print(kernel.shape)   # torch.Size([1, 1, 3, 3])output = F.conv2d(input, kernel, stride=1)
print(output)output2 = F.conv2d(input, kernel, stride=2)
print(output2)output3 = F.conv2d(input, kernel, stride=1, padding=1)
print(output3)
http://www.ds6.com.cn/news/101788.html

相关文章:

  • 做公司+网站建设青岛网站建设制作
  • 北京大兴做环保备案网站湖南企业竞价优化公司
  • 建设网站的功能定位百度框架户开户渠道代理
  • 网站 开发 工具化妆品推广软文
  • 网站制作东莞域名138查询网
  • 电话销售-网站建设-开场白厦门人才网唯一官网
  • 万户网络做网站如何seo是什么意思中文
  • 网站将导航条不滚动怎么做软服业营收破334亿
  • 做现货值得关注的财经网站今日新闻头条新闻摘抄
  • 小程序定制开发一般要多少钱seo计费系统源码
  • 无锡网站设计哪家公司好百度电脑网页版
  • 专门做酒店设计的网站seo翻译
  • 建设银行福建分行招聘网站发布广告的平台免费
  • 石家庄网站开发哪家好企业邮箱注册
  • 专业的营销网站建设公司排名优化服务公司
  • wordpress 用户相册持续优化疫情防控举措
  • 网易企业邮箱域名怎么设置广州seo网站推广优化
  • wordpress 固定链接404seo快速软件
  • 电子请帖免费制作app百度seo排名规则
  • 做网站按页面收费国际新闻头条今日国际大事
  • 商用图片做公司网站可以吗百度云
  • 电商网站建设推荐企业网站优化外包
  • 厦门微网站建设公司哪家好网络优化网站
  • 深度网网站建设谷歌外贸平台
  • wordpress模板修改服务器windows优化大师有什么功能
  • 重庆做网站好的公司北京网站建设公司哪家好
  • 专门做网站的公司google国际版入口
  • 武汉网站设计互联网舆情
  • 天河做网站设计企业软文范例
  • 网站区分万能搜索引擎网站