当前位置: 首页 > 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().__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.lryc.cn/news/149677.html

相关文章:

  • 【面试经典150题】多数元素
  • c#垃圾回收(Garbage Collection)
  • vue 基于element-plus el-button封装按钮组件
  • smbus只能再python2.7下运行?不能再python3.8下运行吗?
  • python中is和==的区别
  • Viobot回环使用
  • React钩子函数之forward结合useImperativeHandle钩子的基本使用
  • c++中移动语义和完美转发
  • 【linux命令讲解大全】040. 文件操作:使用touch命令创建和更新文件
  • Redis之MoreKey问题及Scan命令解读
  • QA工具开发流程
  • JSON.toJSONString首字母大小写问题
  • ant-vue1.78版a-auto-complete表单自动搜索返回列表中的关键字标红
  • Elasticsearch 优化
  • spring boot的自动装配原理
  • 走进低代码平台| iVX-困境之中如何突破传统
  • 【UIPickerView案例03-点餐系统之随机点餐 Objective-C语言】
  • 论文阅读_扩散模型_SDXL
  • 云原生Kubernetes:二进制部署K8S多Master架构(三)
  • 任意文件读取和下载
  • mysql怎么查指定表的自增id?
  • 【C++设计模式】单一职责原则
  • Windows docker desktop 基于HyperV的镜像文件迁移到D盘
  • LM-INFINITE: SIMPLE ON-THE-FLY LENGTH GENERALIZATION FOR LARGE LANGUAGE MODELS
  • ShardingSphere——压测实战
  • 二分图-染色法-dfs
  • SQL优化案例教程0基础(小白必看)
  • webpack(一)模块化
  • 基于Java+SpringBoot+Vue前后端分离人力资源管理系统设计和实现
  • 安装配置mariadb