PyKAN项目中数据归一化参数提取的技术解析
2025-05-14 11:46:49作者:蔡丛锟
在机器学习项目中,数据预处理是构建高效模型的关键步骤之一。PyKAN项目中的create_dataset函数提供了数据归一化功能,但很多开发者在使用过程中会遇到如何获取归一化参数的问题。本文将深入分析这一问题,并提供专业的技术解决方案。
数据归一化的重要性
数据归一化是机器学习预处理中的标准操作,它通过将特征缩放到相似的数值范围来帮助模型更好地学习。在PyKAN项目中,create_dataset函数实现了两种归一化方式:
- 输入数据归一化(normalize_input)
- 标签数据归一化(normalize_label)
归一化通常采用Z-score标准化方法,即对数据进行减去均值再除以标准差的处理。这种处理能够使数据服从标准正态分布,有利于神经网络的训练。
归一化参数获取的技术实现
PyKAN原生的create_dataset函数在归一化处理后,默认不返回归一化参数。这对于需要后续使用这些参数进行预测或评估的场景带来了不便。我们可以通过修改函数实现来获取这些关键参数。
技术实现要点
- 参数存储:在归一化过程中,将计算得到的均值和标准差保存到字典中
- 可选返回:通过
return_stats参数控制是否返回归一化统计量 - 设备兼容性:确保统计量与数据在同一设备上(CPU/GPU)
代码实现解析
def create_dataset(f, n_var=2, ranges=[-1,1], train_num=1000, test_num=1000,
normalize_input=False, normalize_label=False,
return_stats=False, device='cpu', seed=0):
# 初始化随机种子
np.random.seed(seed)
torch.manual_seed(seed)
# 处理输入范围
if len(np.array(ranges).shape) == 1:
ranges = np.array(ranges * n_var).reshape(n_var,2)
else:
ranges = np.array(ranges)
# 生成训练和测试数据
train_input = torch.zeros(train_num, n_var)
test_input = torch.zeros(test_num, n_var)
for i in range(n_var):
train_input[:,i] = torch.rand(train_num,)*(ranges[i,1]-ranges[i,0])+ranges[i,0]
test_input[:,i] = torch.rand(test_num,)*(ranges[i,1]-ranges[i,0])+ranges[i,0]
# 计算标签
train_label = f(train_input)
test_label = f(test_input)
# 归一化辅助函数
def normalize(data, mean, std):
return (data-mean)/std
# 存储统计量
stats = {}
# 输入归一化处理
if normalize_input:
mean_input = torch.mean(train_input, dim=0, keepdim=True)
std_input = torch.std(train_input, dim=0, keepdim=True)
train_input = normalize(train_input, mean_input, std_input)
test_input = normalize(test_input, mean_input, std_input)
stats['mean_input'] = mean_input
stats['std_input'] = std_input
# 标签归一化处理
if normalize_label:
mean_label = torch.mean(train_label, dim=0, keepdim=True)
std_label = torch.std(train_label, dim=0, keepdim=True)
train_label = normalize(train_label, mean_label, std_label)
test_label = normalize(test_label, mean_label, std_label)
stats['mean_label'] = mean_label
stats['std_label'] = std_label
# 构建返回数据集
dataset = {
'train_input': train_input.to(device),
'test_input': test_input.to(device),
'train_label': train_label.to(device),
'test_label': test_label.to(device)
}
# 根据参数决定返回内容
if return_stats and (normalize_input or normalize_label):
return dataset, stats
return dataset
实际应用场景
获取归一化参数在实际项目中有多种重要用途:
- 预测阶段的数据处理:对新数据进行与训练数据相同的归一化处理
- 结果反归一化:将模型输出的归一化结果转换回原始量纲
- 模型部署:在生产环境中保持与训练一致的数据处理流程
- 模型解释:理解特征在原始尺度上的重要性
最佳实践建议
- 参数保存:建议将归一化参数与模型一起保存,确保部署时的一致性
- 数据泄露防范:只使用训练数据计算归一化参数,避免使用测试数据
- 异常值处理:对于存在极端值的数据,考虑使用RobustScaler等更健壮的归一化方法
- 多设备支持:确保归一化参数与模型在同一设备上,避免设备不匹配问题
总结
通过修改PyKAN的create_dataset函数,我们实现了归一化参数的提取功能,为机器学习项目的全流程提供了更好的支持。这一改进不仅解决了原始功能中的参数获取问题,还为模型训练、评估和部署提供了更完整的数据处理方案。在实际项目中,合理使用这些归一化参数能够显著提高模型的性能和可靠性。
登录后查看全文
热门项目推荐
相关项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5-w4a8GLM-5-w4a8基于混合专家架构,专为复杂系统工程与长周期智能体任务设计。支持单/多节点部署,适配Atlas 800T A3,采用w4a8量化技术,结合vLLM推理优化,高效平衡性能与精度,助力智能应用开发Jinja00
jiuwenclawJiuwenClaw 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。Python0193- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
AtomGit城市坐标计划AtomGit 城市坐标计划开启!让开源有坐标,让城市有星火。致力于与城市合伙人共同构建并长期运营一个健康、活跃的本地开发者生态。01
awesome-zig一个关于 Zig 优秀库及资源的协作列表。Makefile00
热门内容推荐
项目优选
收起
deepin linux kernel
C
27
12
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
601
4.04 K
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
69
21
Ascend Extension for PyTorch
Python
441
531
AscendNPU-IR是基于MLIR(Multi-Level Intermediate Representation)构建的,面向昇腾亲和算子编译时使用的中间表示,提供昇腾完备表达能力,通过编译优化提升昇腾AI处理器计算效率,支持通过生态框架使能昇腾AI处理器与深度调优
C++
112
170
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.46 K
823
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
922
770
暂无简介
Dart
846
204
React Native鸿蒙化仓库
JavaScript
321
375
openGauss kernel ~ openGauss is an open source relational database management system
C++
174
249