深度学习pycharm debug

深度学习中,Debug 是定位并解决代码逻辑错误(如张量维度不匹配)、训练异常(如 Loss 波动)、数据问题(如标签错误)的关键手段,通过打印维度、可视化梯度等方法确保模型正常运行、优化性能,贯穿开发全流程。

直接上实例以经典错误shape报错为例:

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

# 模拟图像数据
x = torch.randn(8, 3, 64, 64)  # [B, C, H, W],batch size = 8

# 模拟标签(分类任务)
labels = torch.randint(0, 5, (8,))  # 5 类问题,标签是 [8]

# 模型定义
class BuggyNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1)
        self.conv2 = nn.Conv2d(16, 32, kernel_size=3, padding=1)
        self.pool = nn.AdaptiveAvgPool2d((4, 4))  # 变成 [B, 32, 4, 4]
        self.linear = nn.Linear(32, 5)  # ❌ 故意设置错误 in_features

    def forward(self, x):
        x = F.relu(self.conv1(x))        # [B, 16, 64, 64]
        x = F.relu(self.conv2(x))        # [B, 32, 64, 64]
        x = self.pool(x)                 # [B, 32, 4, 4]
        x = self.linear(x)               # ❌ 错误! x 是 4D,Linear 接受 2D 或 3D
        return x

model = BuggyNet()
criterion = nn.CrossEntropyLoss()

# 前向传播
outputs = model(x)                  # 会报错
loss = criterion(outputs, labels)  # 不会执行到这里

首先设置断点:

然后进行debug右击:

然后会出现控制台:

会出现变量和变量的信息(shape,值):

然后我们进行单步:

然后变量开始变化,当单步到24行时:

此刻x的shape是(8,32,4,4)但是在这个linear层

复制代码
self.linear = nn.Linear(32, 5)  # ❌ 故意设置错误 in_features

期望输入是32,不仅维度不相同channel也不相同,所以继续单步会报错:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (1024x4 and 32x5)

然后我们根据错误进行操作将x展平并且修改linear的输入:

x = x.view(x.size(0), -1) # [8, 32*4*4] = [8, 512]

self.linear = nn.Linear(512, 5) # ✅ 修复后的定义

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

# 模拟图像数据
x = torch.randn(8, 3, 64, 64)  # [B, C, H, W],batch size = 8

# 模拟标签(分类任务)
labels = torch.randint(0, 5, (8,))  # 5 类问题,标签是 [8]

# 模型定义
class BuggyNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1)
        self.conv2 = nn.Conv2d(16, 32, kernel_size=3, padding=1)
        self.pool = nn.AdaptiveAvgPool2d((4, 4))  # 变成 [B, 32, 4, 4]
        self.linear = nn.Linear(512, 5)  # 此处修改

    def forward(self, x):
        x = F.relu(self.conv1(x))        # [B, 16, 64, 64]
        x = F.relu(self.conv2(x))        # [B, 32, 64, 64]
        x = self.pool(x)                 # [B, 32, 4, 4]
        x = x.view(x.size(0), -1)        # 此处修改
        x = self.linear(x)               
        return x

model = BuggyNet()
criterion = nn.CrossEntropyLoss()

# 前向传播
outputs = model(x)                  
loss = criterion(outputs, labels)  

然后我们这样就不会报错了。

很多时候缝合模块时就是经常遇见shape问题,耐性一点关注输入输出shape这样就可以轻松解决问题。

相关推荐
波动几何23 分钟前
因果动力学架构技能cda
人工智能
Lucas_coding26 分钟前
【Claude Code Router】 Claude Code 兼容 OpenAI 格式 API, Claude code 接入本地部署模型
人工智能·python
jinanwuhuaguo28 分钟前
(第二十七篇)OpenClaw四月的演化风暴:OpenClaw 2026年4月全版本更新的文明级解读
大数据·人工智能·架构·kotlin·openclaw
测试员周周30 分钟前
【AI测试系统】第5篇:从 Archon 看 AI 工程化落地:为什么"确定性编排+AI 弹性智能"是终局?
人工智能·python·测试
RxGc36 分钟前
微软AI Agent框架深度测评:Microsoft Agent Framework 1.0 vs OpenClaw/Claude企业级能力对比
人工智能·agent
随便写写36 分钟前
第四章 智能体经典范式构建
人工智能
穿过生命散发芬芳37 分钟前
基于CodeBuddy Agent智能体平台构建自己第一个SKILL——相机推荐
人工智能
格林威41 分钟前
工业视觉项目:如何与客户有效沟通验收标准?
人工智能·数码相机·计算机视觉·视觉检测·机器视觉·工业相机·视觉项目
zhuiyisuifeng41 分钟前
AI新闻配图革命:GPTimage2镜像官网重塑时效与成本
人工智能·gpt
码云数智-大飞1 小时前
本地部署大模型:隐私安全与多元优势一站式解读
运维·网络·人工智能