神经网络入门

神经网络的基本骨架

1. nn.Module的使用

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

eg:

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

class 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->卷积->非线性

代码示例:

python 复制代码
import torch
from torch import nn

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

    def forward(self, input):
        output = input+1	# 实现输出加1
        return output

kun = Kun()
x = torch.tensor(1.0)
output = kun(x)
print(output)   # tensor(2.)

2. 卷积

conv2可选参数

卷积计算过程示意:

python 复制代码
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)
python 复制代码
torch.Size([5, 5])
torch.Size([3, 3])

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

所以需要调整

python 复制代码
input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))
python 复制代码
output = F.conv2d(input, kernel, stride=1)
print(output)

--------------------------------------------------------------------------
tensor([[[[10, 12, 12],
          [18, 16, 16],
          [13,  9,  3]]]])

stride可以选择移动的步长

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

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

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

示例代码:

python 复制代码
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)
相关推荐
l0sgAi3 分钟前
vLLM在RTX50系显卡上部署大模型-使用wsl2
linux·人工智能
DDliu4 分钟前
花半个月死磕提示词后,我发现:真正值钱的不是模板,是这套可复用的结构化思维
人工智能
腾讯云开发者4 分钟前
AI 浪潮下的锚与帆:工程师文化的变与不变 | 架构师夜生活
人工智能
JoernLee4 分钟前
机器学习算法:支持向量机SVM
人工智能·算法·机器学习
杰尼橙子9 分钟前
深度解读Karpathy说的Software 3.0时代,感觉是个人的机会很大的时代呀
人工智能·openai
我爱一条柴ya33 分钟前
【AI大模型】线性回归:经典算法的深度解析与实战指南
人工智能·python·算法·ai·ai编程
Qiuner39 分钟前
【源力觉醒 创作者计划】开源、易用、强中文:文心一言4.5或是 普通人/非AI程序员 的第一款中文AI?
人工智能·百度·开源·文心一言·gitcode
未来之窗软件服务1 小时前
chrome webdrive异常处理-session not created falled opening key——仙盟创梦IDE
前端·人工智能·chrome·仙盟创梦ide·东方仙盟·数据调式
AI街潜水的八角1 小时前
深度学习图像分类数据集—蘑菇识别分类
人工智能·深度学习·分类
飞睿科技2 小时前
乐鑫代理商飞睿科技,2025年AI智能语音助手市场发展趋势与乐鑫芯片解决方案分析
人工智能