4 个版本 (破坏性更新)
新版本 0.4.0 | 2024年8月19日 |
---|---|
0.3.0 | 2024年7月26日 |
0.2.0 | 2024年6月28日 |
0.1.0 | 2024年6月24日 |
#103 in 机器学习
每月275次下载
270KB
4.5K SLoC
fekan
一个用于构建和训练 Kolmogorov-Arnold 神经网络的库。
fekan
包包含构建和训练 Kolmogorov-Arnold 网络 (KANs) 的工具,包括表示完整模型的结构体;以及表示单个 KAN 层的结构体,可用于单独使用或在其他模型中使用。
欢迎提交问题和拉取请求!
什么是 Kolmogorov-Arnold 网络?
与对前一层的激活进行加权和通过固定非线性函数传递不同,KAN 中的每个节点都将前一层的每个激活通过不同的、可训练的非线性函数传递,然后求和。这使得网络比传统神经网络更具可解释性,在某些情况下,使用更小的内存占用即可获得更高的准确性。
由于 KAN 层的激活无法通过矩阵乘法计算,因此训练 KAN 比训练类似大小的传统神经网络要慢得多。然而,作者希望 KANs 的更高精度将允许在许多情况下使用更小的网络,从而抵消大部分增加的训练时间;并且 KANs 的可解释性将充分证明剩余的训练时间。
有关此库的理论背景和适合 KANs 的问题集示例,请参阅 arXiv 论文 KAN: Kolmogorov-Arnold Neural Networks
二进制使用
fekan
包包含用于构建和训练 KANs 的命令行工具。使用以下命令安装:
cargo install fekan --features serialization
构建新模型并在数据集上训练它
fekan build classifier ...
或
fekan build regressor ...
加载现有模型进行进一步训练
fekan load [model_file] train ...
加载现有模型以在数据集上进行预测
fekan load [model_file] infer ...
以下示例命令用于构建用于确定一组特征是否映射到狗或猫的分类模型:
fekan build classifier --data dog_or_cat_data.json --classes "cat,dog" --degree 3 --coefs 4 --hidden-layer-sizes "5,3" --num-epochs 250 --batch-size 100 --knot-adaptivity 0.1 --learning-rate 0.05 --validation-split 0.2 --validate-each-epoch --model-out my_new_model.cbor
其中数据文件看起来像这样...
[
{
features: [1.2, 3.14159, -22.0]
label: "cat"
}
{
features: [2.89, -0.002, 16.288844]
label: "dog"
}
]
要获取完整的用法详情,请使用帮助命令,例如:fekan help [命令]
CLI支持从.pkl
、.json
和(avro目前存在bug)文件中读取数据。功能必须在名为"features"的单个"list"-类型列/字段中,标签(用于训练)必须在名为"labels"的单个列中;标签应为字符串(用于分类模型)和浮点数(用于回归模型)。模型可以保存为pickle、json或cbor文件,格式由提供的文件扩展名推断。.avro
代码示例
构建、训练并保存一个完整的KAN回归模型,该模型具有二维输入、一个包含3个节点的隐藏层和一个输出节点,每个层使用10个系数的3次方B样条(也称为控制点)。
Build, train and save a full KAN regression model with a 2-dimensional input, 1 hidden layer with 3 nodes, and 1 output node,
where each layer uses degree-4 [B-splines](https://en.wikipedia.org/wiki/B-spline) with 5 coefficients (AKA control points):
```rust
use fekan::kan::{Kan, KanOptions, ModelType};
use fekan::{Sample, training_options::{TrainingOptions, EachEpoch}};
use tempfile::tempfile;
// initialize the model
let model_options = KanOptions{
input_size: 2,
layer_sizes: vec![3, 1],
degree: 3,
coef_size: 7,
model_type: ModelType::Regression,
class_map: None};
let mut untrained_model = Kan::new(&model_options);
// train the model
let training_data: Vec<Sample> = Vec::new();
/* Load training data */
# let sample_1 = Sample::new(vec![1.0, 2.0], 3.0);
# let sample_2 = Sample::new(vec![-1.0, 1.0], 0.0);
# let training_data = vec![sample_1, sample_2];
let trained_model = fekan::train_model(untrained_model, &training_data, TrainingOptions::default())?;
// save the model
// both Kan and KanLayer implement the serde Serialize trait, so they can be saved to a file using any serde-compatible format
// here we use the ciborium crate to save the model in the CBOR format
let mut file = tempfile().unwrap();
ciborium::into_writer(&trained_model, &mut file)?;
# Ok::<(), Box<dyn std::error::Error>>(())
加载和使用训练好的分类模型
use fekan::kan::Kan;
let trained_model = serde_json::from_reader(&model_file);
let data: Vec<Vec<f64>> = /* load data */
let predictions: Vec<(Vec<f64>, &str)> = Vec::with_capacity(data.len());
for features in data{
let logits: Vec<f64> = trained_model.forward(features);
let (index, probability) = /* interpret your logit data */
let label: &str = trained_model.node_to_label(index); // get the human-readable label for a given output node
}
待办事项清单
fekan
功能齐全,但还有许多改进之处。
- 与刘等人的平权
- 网格扩展
- 在网格更新时调整系数以匹配先前函数
- 修剪不必要的节点
- 符号化
- 可视化
- 除SGD(Adam、LBFGS)之外的方法进行训练
- 速度
- 支持多线程
- 支持SIMD/并行计算
许可证
根据您的要求,许可协议可以是以下之一:
- Apache许可证,版本2.0(LICENSE-APACHE或https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT许可证(LICENSE-MIT或http://opensource.org/licenses/MIT)
任选其一。
贡献
除非您明确声明,否则您有意提交以包含在作品中的任何贡献,如Apache-2.0许可证中定义,应如上所述双许可,不得附加任何额外条款或条件。
依赖项
~10–19MB
~256K SLoC