python第35天打卡

三种不同的模型可视化方法:推荐torchinfo打印summary+权重分布可视化

python 复制代码
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
from torchinfo import summary
import numpy as np

# 1. 定义示例模型
class CNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv_layers = nn.Sequential(
            nn.Conv2d(3, 16, kernel_size=3),
            nn.ReLU(),
            nn.MaxPool2d(2),
            nn.Conv2d(16, 32, kernel_size=3),
            nn.ReLU(),
            nn.MaxPool2d(2)
        )
        self.fc_layers = nn.Sequential(
            nn.Linear(32*6*6, 128),
            nn.ReLU(),
            nn.Linear(128, 10)
        )

    def forward(self, x):
        x = self.conv_layers(x)
        x = torch.flatten(x, 1)
        x = self.fc_layers(x)
        return x

model = CNN()

# 2. 模型摘要可视化 (使用torchinfo)
print("\n=== 模型结构摘要 ===")
summary(
    model, 
    input_size=(1, 3, 32, 32),  # (batch, channels, height, width)
    col_names=["input_size", "output_size", "num_params", "kernel_size"],
    verbose=1
)

# 3. 权重分布可视化
def plot_weight_distribution(model):
    plt.figure(figsize=(12, 6))
    
    # 收集所有权重
    all_weights = []
    for name, param in model.named_parameters():
        if 'weight' in name:
            flattened = param.detach().cpu().numpy().flatten()
            all_weights.extend(flattened)
    
    # 绘制直方图
    plt.hist(all_weights, bins=150, alpha=0.7, color='blue', edgecolor='black')
    plt.title('Model Weights Distribution')
    plt.xlabel('Weight Value')
    plt.ylabel('Frequency (log scale)')
    plt.yscale('log')
    plt.grid(True, alpha=0.3)
    plt.show()

print("\n=== 权重分布图 ===")
plot_weight_distribution(model)

# 4. 逐层权重可视化 (额外方法)
def plot_layer_weights(model):
    plt.figure(figsize=(15, 10))
    
    for i, (name, param) in enumerate(model.named_parameters()):
        if 'weight' in name:
            plt.subplot(3, 3, i+1)
            layer_weights = param.detach().cpu().numpy().flatten()
            plt.hist(layer_weights, bins=100, alpha=0.7)
            plt.title(f'{name} weight distribution')
            plt.grid(True, alpha=0.2)
    
    plt.tight_layout()
    plt.show()

print("\n=== 逐层权重分布 ===")
plot_layer_weights(model)

# 5. 权重矩阵可视化 (额外方法)
def visualize_weight_matrix(model):
    plt.figure(figsize=(15, 4))
    
    # 获取第一个卷积层的权重
    conv1_weight = model.conv_layers[0].weight.detach().cpu()
    
    # 归一化权重用于显示
    min_val = torch.min(conv1_weight)
    max_val = torch.max(conv1_weight)
    normalized_weights = (conv1_weight - min_val) / (max_val - min_val)
    
    # 绘制权重矩阵
    for i in range(16):  # 显示前16个卷积核
        plt.subplot(2, 8, i+1)
        plt.imshow(normalized_weights[i].permute(1, 2, 0))  # CHW -> HWC
        plt.axis('off')
        plt.title(f'Kernel {i+1}')
    
    plt.suptitle('First Conv Layer Kernels', fontsize=16)
    plt.tight_layout()
    plt.show()

print("\n=== 卷积核可视化 ===")
visualize_weight_matrix(model)

@浙大疏锦行

相关推荐
我先去打把游戏先5 分钟前
ESP32开发指南(基于IDF):连接AWS,乐鑫官方esp-aws-iot-master例程实验、跑通
开发语言·笔记·单片机·物联网·学习·云计算·aws
极客数模25 分钟前
2025年(第六届)“大湾区杯”粤港澳金融数学建模竞赛准备!严格遵循要求,拿下大奖!
大数据·python·数学建模·金融·分类·图论·boosting
逻极36 分钟前
Rust数据类型(上):标量类型全解析
开发语言·后端·rust
倔强青铜三38 分钟前
苦练Python第73天:玩转对象持久化,pickle模块极速入门
人工智能·python·面试
Zhangzy@40 分钟前
Rust 编译优化选项
android·开发语言·rust
百锦再43 分钟前
第2章 第一个Rust程序
java·开发语言·后端·rust·eclipse·tomcat·hibernate
Zhangzy@44 分钟前
Rust 中的注释与文档注释实践指南
开发语言·后端·rust
像风一样自由20201 小时前
使用 Rust 开发图片切分工具:从零到发布的完整指南
开发语言·后端·rust
程序员三藏1 小时前
Postman持久化保存/设置断言详解
自动化测试·软件测试·python·测试工具·职场和发展·接口测试·postman
java1234_小锋1 小时前
PyTorch2 Python深度学习 - 卷积神经网络(CNN)介绍实例 - 使用MNIST识别手写数字示例
python·深度学习·cnn·pytorch2