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

pytorch(6)——神经网络基本骨架nn.module的使用

1 神经网络框架

1.1 Module类的使用

NN (Neural network): 神经网络
Containers: 容器
Convolution Layers: 卷积层
Pooling layers: 池化层
Padding Layers: 填充层
Non-linear Activations (weighted sum, nonlinearity): 非线性激活
Non-linear Activations (other): 非线性激活
Normalization Layers: 归一化层

在这里插入图片描述

Containers 包括:
(1)Module:所有神经网络的基类

https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module

Class torch.nn.Module(*args, **kwargs)

import torch.nn as nn
import torch.nn.functional as Fclass Model(nn.Module):def __init__(self):super().__init__()self.conv1 = nn.Conv2d(1, 20, 5)self.conv2 = nn.Conv2d(20, 20, 5)def forward(self, inputX):x = F.relu(self.conv1(inputX))return F.relu(self.conv2(inputX))

forward函数内:relu()为激活函数,conv为卷积函数。输入inputX-> 卷积-> 非线性处理(relu)-> 卷积 ->非线性(relu)。

python代码:

from torch import nn
import torchclass MyNN(nn.Module):def __init__(self):super().__init__()def forward(self, inputX):outputX = inputX + 1return outputXmynn = MyNN()
x = torch.tensor(1.0)
output = mynn(x)
print(output)

输出结果:

tensor(2.)

1.2 二维卷积计算

在这里插入图片描述

二维卷积 conv2d()
输入和输出的矩阵类型都需要(N, C_{in}, H_{in}, W_{in})

输入图像1024x800,卷积核3x3,每次9个元素相乘后相加,不断向右移动并计算,移动到最右侧之后;然后向下移动并计算,移动到最下侧之后,完成卷积计算。

import torch
import torch.nn.functional as Finput = 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]])
input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))print("input:")
print(input)
print("kernel:")
print(kernel)output = F.conv2d(input, kernel, stride=1)
print("output:")
print(output)

输出结果:

input:
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:
tensor([[[[1, 2, 1],[0, 1, 0],[2, 1, 0]]]])
output:
tensor([[[[10, 12, 12],[18, 16, 16],[13,  9,  3]]]])

如果将步进stride修改为2。

output2 = F.conv2d(input, kernel, stride=2)
print("output2:")
print(output2)

输出结果为:

output2:
tensor([[[[10, 12],[13,  3]]]])

padding填充,将原图像的四周填充一圈0,这样的话,卷积计算的结果维度就会更大。
在这里插入图片描述

output3 = F.conv2d(input, kernel, stride=1, padding=1)
print("output3:")
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]]]])
http://www.lryc.cn/news/100688.html

相关文章:

  • 论文精读之BERT
  • 实战:Docker+Jenkins+Gitee构建CICD流水线
  • 7.25 Qt
  • P1420 最长连号
  • UVA-1354 天平难题 题解答案代码 算法竞赛入门经典第二版
  • 电机故障诊断(python程序,模型为CNN结合LSTM)
  • ubuntu 20.04 rtc时间显示问题探究
  • 数值分析第七章节 用Python实现非线性方程与方程组的数值解法
  • 利用MATLAB制作DEM山体阴影
  • ubuntu 使用 rsync 的 SSH 方式同步备份远程WEB服务器
  • 机器学习 | Python实现NARX模型预测控制
  • M5ATOMS3基础03给ROS1发一个问候(rosserial)
  • 基于Vue3实现鼠标按下某个元素进行移动,实时改变左侧或右侧元素的宽度,以及点击收起或展开的功能
  • 使用MyBatis(2)
  • 【FPGA/D6】
  • 【WebGIS实例】(10)Cesium开场效果(场景、相机旋转,自定义图片底图)
  • 【Spring】IOC的原理
  • AI加速游戏开发 亚马逊云科技适配3大场景,打造下一代游戏体验
  • C++ | 继承(基类,父类,超类),(派生类,子类)
  • Commands Of Hadoop
  • SQL-每日一题【620.有趣的电影】
  • linux 精华总结
  • Eureka 学习笔记2:客户端 DiscoveryClient
  • okhttp原理分析
  • freeswitch的mod_xml_curl模块
  • 高速数据采集专家-FMC140【产品手册】
  • 【SSM】知识集锦
  • Flowable-中间事件-信号中间抛出事件
  • 【算法基础:动态规划】5.3 计数类DP(整数拆分、分拆数)
  • 封装(Encapsulation)