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

Day52打卡 @浙大疏锦行

知识点回顾:

  1. 随机种子
  2. 内参的初始化
  3. 神经网络调参指南
    1. 参数的分类
    2. 调参的顺序
    3. 各部分参数的调整心得
import torch
import numpy as np
import os
import random# 全局随机函数
def set_seed(seed=42, deterministic=True):"""设置全局随机种子,确保实验可重复性参数:seed: 随机种子值,默认为42deterministic: 是否启用确定性模式,默认为True"""# 设置Python的随机种子random.seed(seed) os.environ['PYTHONHASHSEED'] = str(seed) # 确保Python哈希函数的随机性一致,比如字典、集合等无序# 设置NumPy的随机种子np.random.seed(seed)# 设置PyTorch的随机种子torch.manual_seed(seed) # 设置CPU上的随机种子torch.cuda.manual_seed(seed) # 设置GPU上的随机种子torch.cuda.manual_seed_all(seed)  # 如果使用多GPU# 配置cuDNN以确保结果可重复if deterministic:torch.backends.cudnn.deterministic = Truetorch.backends.cudnn.benchmark = False# 设置随机种子
set_seed(42)
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
import numpy as np# 设置设备
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")# 定义极简CNN模型(仅1个卷积层+1个全连接层)
class SimpleCNN(nn.Module):def __init__(self):super(SimpleCNN, self).__init__()# 卷积层:输入3通道,输出16通道,卷积核3x3self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1)# 池化层:2x2窗口,尺寸减半self.pool = nn.MaxPool2d(kernel_size=2)# 全连接层:展平后连接到10个输出(对应10个类别)# 输入尺寸:16通道 × 16x16特征图 = 16×16×16=4096self.fc = nn.Linear(16 * 16 * 16, 10)def forward(self, x):# 卷积+池化x = self.pool(self.conv1(x))  # 输出尺寸: [batch, 16, 16, 16]# 展平x = x.view(-1, 16 * 16 * 16)  # 展平为: [batch, 4096]# 全连接x = self.fc(x)  # 输出尺寸: [batch, 10]return x# 初始化模型
model = SimpleCNN()
model = model.to(device)# 查看模型结构
print(model)# 查看初始权重统计信息
def print_weight_stats(model):# 卷积层conv_weights = model.conv1.weight.dataprint("\n卷积层 权重统计:")print(f"  均值: {conv_weights.mean().item():.6f}")print(f"  标准差: {conv_weights.std().item():.6f}")print(f"  理论标准差 (Kaiming): {np.sqrt(2/3):.6f}")  # 输入通道数为3# 全连接层fc_weights = model.fc.weight.dataprint("\n全连接层 权重统计:")print(f"  均值: {fc_weights.mean().item():.6f}")print(f"  标准差: {fc_weights.std().item():.6f}")print(f"  理论标准差 (Kaiming): {np.sqrt(2/(16*16*16)):.6f}")# 改进的可视化权重分布函数
def visualize_weights(model, layer_name, weights, save_path=None):plt.figure(figsize=(12, 5))# 权重直方图plt.subplot(1, 2, 1)plt.hist(weights.cpu().numpy().flatten(), bins=50)plt.title(f'{layer_name} 权重分布')plt.xlabel('权重值')plt.ylabel('频次')# 权重热图plt.subplot(1, 2, 2)if len(weights.shape) == 4:  # 卷积层权重 [out_channels, in_channels, kernel_size, kernel_size]# 只显示第一个输入通道的前10个滤波器w = weights[:10, 0].cpu().numpy()plt.imshow(w.reshape(-1, weights.shape[2]), cmap='viridis')else:  # 全连接层权重 [out_features, in_features]# 只显示前10个神经元的权重,重塑为更合理的矩形w = weights[:10].cpu().numpy()# 计算更合理的二维形状(尝试接近正方形)n_features = w.shape[1]side_length = int(np.sqrt(n_features))# 如果不能完美整除,添加零填充使能重塑if n_features % side_length != 0:new_size = (side_length + 1) * side_lengthw_padded = np.zeros((w.shape[0], new_size))w_padded[:, :n_features] = ww = w_padded# 重塑并显示plt.imshow(w.reshape(w.shape[0] * side_length, -1), cmap='viridis')plt.colorbar()plt.title(f'{layer_name} 权重热图')plt.tight_layout()if save_path:plt.savefig(f'{save_path}_{layer_name}.png')plt.show()# 打印权重统计
print_weight_stats(model)# 可视化各层权重
visualize_weights(model, "Conv1", model.conv1.weight.data, "initial_weights")
visualize_weights(model, "FC", model.fc.weight.data, "initial_weights")# 可视化偏置
plt.figure(figsize=(12, 5))# 卷积层偏置
conv_bias = model.conv1.bias.data
plt.subplot(1, 2, 1)
plt.bar(range(len(conv_bias)), conv_bias.cpu().numpy())
plt.title('卷积层 偏置')# 全连接层偏置
fc_bias = model.fc.bias.data
plt.subplot(1, 2, 2)
plt.bar(range(len(fc_bias)), fc_bias.cpu().numpy())
plt.title('全连接层 偏置')plt.tight_layout()
plt.savefig('biases_initial.png')
plt.show()print("\n偏置统计:")
print(f"卷积层偏置 均值: {conv_bias.mean().item():.6f}")
print(f"卷积层偏置 标准差: {conv_bias.std().item():.6f}")
print(f"全连接层偏置 均值: {fc_bias.mean().item():.6f}")
print(f"全连接层偏置 标准差: {fc_bias.std().item():.6f}")

@浙大疏锦行


文章转载自:

http://YpMJDtE2.ptwqf.cn
http://vtsVwM3o.ptwqf.cn
http://iIh4wFkb.ptwqf.cn
http://xD1kUcPM.ptwqf.cn
http://JSJudhaq.ptwqf.cn
http://5dHO7LJF.ptwqf.cn
http://EskaojkQ.ptwqf.cn
http://nh1sBXO7.ptwqf.cn
http://CzBjietV.ptwqf.cn
http://bekl5gJr.ptwqf.cn
http://nGAnIUEI.ptwqf.cn
http://aGq4m5Zf.ptwqf.cn
http://ALInFAmG.ptwqf.cn
http://YNLmw6xs.ptwqf.cn
http://3ZJjTk7a.ptwqf.cn
http://XDg97AoD.ptwqf.cn
http://fJgRyWsy.ptwqf.cn
http://qPbMn5r3.ptwqf.cn
http://sOp8dh2B.ptwqf.cn
http://zQ4yWpmj.ptwqf.cn
http://XadO7mMI.ptwqf.cn
http://eYDWdjVD.ptwqf.cn
http://g1zc8XzM.ptwqf.cn
http://lMOvxyYy.ptwqf.cn
http://iXYUpiVH.ptwqf.cn
http://rjPy5Frs.ptwqf.cn
http://nOYaTDRD.ptwqf.cn
http://KsKgGSRd.ptwqf.cn
http://tlE39zkn.ptwqf.cn
http://UqXCwklU.ptwqf.cn
http://www.dtcms.com/a/246329.html

相关文章:

  • 机器学习与深度学习21-信息论
  • 短剧系统开发:打造高效、创新的短视频娱乐平台 - 从0到1的完整解决方案
  • 利用Anything LLM和内网穿透工具在本地搭建可远程访问的AI知识库系统(1)
  • 不同环境的配置文件
  • 无感无刷电机的过零点检测电路多图对比
  • Netty从入门到进阶(四)
  • strncpy_s与_TRUNCATE
  • Jinja2 模板在 Python 和 LLM 提示词编辑器中的应用
  • 如何搭建反向海淘代购系统?
  • Cursor 编辑器中的 Notepad 功能使用指南
  • 网络安全攻防领域证书
  • 黑群晖NAS部署DeepSeek模型与内网穿透实现本地AI服务
  • FastJSON 1.2.83版本升级指南:安全加固与性能优化实践
  • BERT vs BART vs T5:预训练语言模型核心技术详解
  • mysql 的卸载- Windows 版
  • Kotlin 中的继承/实现
  • 【Git】面对发布或重要节点,Git如何打Tag?
  • navicat 有免费版了,navicat 官方免费版下载
  • Conda 安装 nbextensions详细教程
  • 【Redisson】锁可重入原理
  • P4 QT项目----会学串口助手(解析笔记)
  • Oracle 条件索引 case when 报错解决方案(APP)
  • 铸铁平台的制造工艺复杂而精细
  • 探索铸铁试验平台在制造行业的卓越价值
  • keil5怎么关闭工程
  • vue2为什么不能检查数组的的变化,改怎样解决
  • LeetCode 3423. Maximum Difference Between Adjacent Elements in a Circular Array
  • 【Zephyr 系列 20】BLE 模块产线测试系统设计:快速校验、参数写入、自动识别的完整方案
  • 数字签名CA数字证书
  • 树莓派5实现串口通信教程