11个版本

使用旧的Rust 2015

0.1.8 2024年2月2日
0.1.7 2018年10月19日
0.1.6 2015年11月7日
0.1.5 2015年10月26日
0.0.3 2015年6月28日

#120 in 机器学习

LGPL-3.0

84KB
1.5K SLoC

fann-rs

Build Status

Rust 封装了 Fast Artificial Neural Network (FANN) 库。这个crate提供了在低级绑定fann-sys-rs之上的FANN的安全接口。

文档

用法

fannlibc 添加到你的 Cargo.toml 依赖列表中

[dependencies]
fann = "*"
libc = "*"

并在crate根目录下添加以下内容

extern crate fann;
extern crate libc;

用法示例包含在 文档 中。


lib.rs:

Rust 对 Fast Artificial Neural Network 库的封装。

可以使用 Fann::new 方法创建一个具有随机权重的新的神经网络,或者,对于不同的网络拓扑,可以使用其变体 Fann::new_sparseFann::new_shortcut。现有的神经网络可以保存到文件中,也可以从文件中加载。

同样,训练数据集可以加载和保存到可读文件中,或者可以直接将浮点数切片作为训练数据提供给网络。

示例

extern crate fann;
use fann::{ActivationFunc, Fann, TrainAlgorithm, QuickpropParams};

fn main() {
   // Create a new network with two input neurons, a hidden layer with three neurons, and one
   // output neuron.
   let mut fann = Fann::new(&[2, 3, 1]).unwrap();
   // Configure the activation functions for the hidden and output neurons.
   fann.set_activation_func_hidden(ActivationFunc::SigmoidSymmetric);
   fann.set_activation_func_output(ActivationFunc::SigmoidSymmetric);
   // Use the Quickprop learning algorithm, with default parameters.
   // (Otherwise, Rprop would be used.)
   fann.set_train_algorithm(TrainAlgorithm::Quickprop(Default::default()));
   // Train for up to 500000 epochs, displaying progress information after intervals of 1000
   // epochs. Stop when the network's error on the training data drops to 0.001.
   let max_epochs = 500000;
   let epochs_between_reports = 1000;
   let desired_error = 0.001;
   // Train directly on data loaded from the file "xor.data".
   fann.on_file("test_files/xor.data")
       .with_reports(epochs_between_reports)
       .train(max_epochs, desired_error).unwrap();
   // The network now approximates the XOR problem:
   assert!(fann.run(&[-1.0,  1.0]).unwrap()[0] > 0.9);
   assert!(fann.run(&[ 1.0, -1.0]).unwrap()[0] > 0.9);
   assert!(fann.run(&[ 1.0,  1.0]).unwrap()[0] < 0.1);
   assert!(fann.run(&[-1.0, -1.0]).unwrap()[0] < 0.1);
}

FANN 还支持级联训练,在训练过程中通过添加额外的神经元来改变网络的拓扑结构。

extern crate fann;
use fann::{ActivationFunc, CascadeParams, Fann};

fn main() {
   // Create a new network with two input neurons and one output neuron.
   let mut fann = Fann::new_shortcut(&[2, 1]).unwrap();
   // Use the default cascade training parameters, but a higher weight multiplier:
   fann.set_cascade_params(&CascadeParams {
                                weight_multiplier: 0.6,
                                ..CascadeParams::default()
                            });
   // Add up to 50 neurons, displaying progress information after each.
   // Stop when the network's error on the training data drops to 0.001.
   let max_neurons = 50;
   let neurons_between_reports = 1;
   let desired_error = 0.001;
   // Train directly on data loaded from the file "xor.data".
   fann.on_file("test_files/xor.data")
       .with_reports(neurons_between_reports)
       .cascade()
       .train(max_neurons, desired_error).unwrap();
   // The network now approximates the XOR problem:
   assert!(fann.run(&[-1.0,  1.0]).unwrap()[0] > 0.9);
   assert!(fann.run(&[ 1.0, -1.0]).unwrap()[0] > 0.9);
   assert!(fann.run(&[ 1.0,  1.0]).unwrap()[0] < 0.1);
   assert!(fann.run(&[-1.0, -1.0]).unwrap()[0] < 0.1);
}

依赖

~140KB