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)

python 复制代码
import torch.nn as nn
import torch.nn.functional as F

class 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代码:

python 复制代码
from torch import nn
import torch


class MyNN(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, inputX):
        outputX = inputX + 1
        return outputX

mynn = 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个元素相乘后相加,不断向右移动并计算,移动到最右侧之后;然后向下移动并计算,移动到最下侧之后,完成卷积计算。

python 复制代码
import torch
import torch.nn.functional as F

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]])
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)

输出结果:

python 复制代码
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。

python 复制代码
output2 = F.conv2d(input, kernel, stride=2)
print("output2:")
print(output2)

输出结果为:

python 复制代码
output2:
tensor([[[[10, 12],
          [13,  3]]]])

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

python 复制代码
output3 = F.conv2d(input, kernel, stride=1, padding=1)
print("output3:")
print(output3)

输出的结果:

python 复制代码
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]]]])
相关推荐
JD技术委员会29 分钟前
Rust 语法噪音这么多,是否适合复杂项目?
开发语言·人工智能·rust
liruiqiang0534 分钟前
机器学习 - 投票感知器
人工智能·算法·机器学习
刘什么洋啊Zz4 小时前
MacOS下使用Ollama本地构建DeepSeek并使用本地Dify构建AI应用
人工智能·macos·ai·ollama·deepseek
奔跑草-5 小时前
【拥抱AI】GPT Researcher 源码试跑成功的心得与总结
人工智能·gpt·ai搜索·deep research·深度检索
禁默5 小时前
【第四届网络安全、人工智能与数字经济国际学术会议(CSAIDE 2025】网络安全,人工智能,数字经济的研究
人工智能·安全·web安全·数字经济·学术论文
boooo_hhh7 小时前
深度学习笔记16-VGG-16算法-Pytorch实现人脸识别
pytorch·深度学习·机器学习
AnnyYoung7 小时前
华为云deepseek大模型平台:deepseek满血版
人工智能·ai·华为云
INDEMIND7 小时前
INDEMIND:AI视觉赋能服务机器人,“零”碰撞避障技术实现全天候安全
人工智能·视觉导航·服务机器人·商用机器人
慕容木木8 小时前
【全网最全教程】使用最强DeepSeekR1+联网的火山引擎,没有生成长度限制,DeepSeek本体的替代品,可本地部署+知识库,注册即可有750w的token使用
人工智能·火山引擎·deepseek·deepseek r1
南 阳8 小时前
百度搜索全面接入DeepSeek-R1满血版:AI与搜索的全新融合
人工智能·chatgpt