rustfmt
This commit is contained in:
parent
d80bd3c5e5
commit
2f3745a31c
7 changed files with 105 additions and 61 deletions
|
@ -3,20 +3,24 @@ extern crate rust_nn;
|
|||
use std::error::Error;
|
||||
use std::f64::consts::PI;
|
||||
|
||||
use ndarray_rand::RandomExt;
|
||||
use ndarray::Array1;
|
||||
use ndarray_rand::rand_distr::Uniform;
|
||||
use ndarray_rand::RandomExt;
|
||||
use plotters::prelude::*;
|
||||
use rust_nn::Network;
|
||||
use rust_nn::functions::{activation_functions, loss_functions};
|
||||
use rust_nn::layers::activation_layer::ActivationLayer;
|
||||
use rust_nn::layers::fc_layer::{FCLayer, Initializer};
|
||||
use ndarray::Array1;
|
||||
use rust_nn::Network;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
// training data
|
||||
let training_interval = (0.0f64, 2.0f64 * PI);
|
||||
let steps = 100000;
|
||||
let training_values = Array1::random(steps, Uniform::new(training_interval.0, training_interval.1)).to_vec();
|
||||
let training_values = Array1::random(
|
||||
steps,
|
||||
Uniform::new(training_interval.0, training_interval.1),
|
||||
)
|
||||
.to_vec();
|
||||
let mut x_train = Vec::new();
|
||||
let mut y_train = Vec::new();
|
||||
for x in training_values {
|
||||
|
@ -42,19 +46,23 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
network.add_layer(Box::new(FCLayer::new(
|
||||
8,
|
||||
Initializer::GaussianWFactor(0.0, 1.0, 0.1),
|
||||
Initializer::GaussianWFactor(0.0, 1.0, 0.1)
|
||||
Initializer::GaussianWFactor(0.0, 1.0, 0.1),
|
||||
)));
|
||||
network.add_layer(Box::new(ActivationLayer::new(
|
||||
activation_functions::Type::LeakyRelu,
|
||||
)));
|
||||
network.add_layer(Box::new(ActivationLayer::new(activation_functions::Type::LeakyRelu)));
|
||||
network.add_layer(Box::new(FCLayer::new(
|
||||
8,
|
||||
Initializer::GaussianWFactor(0.0, 1.0, 0.1),
|
||||
Initializer::GaussianWFactor(0.0, 1.0, 0.1)
|
||||
Initializer::GaussianWFactor(0.0, 1.0, 0.1),
|
||||
)));
|
||||
network.add_layer(Box::new(ActivationLayer::new(
|
||||
activation_functions::Type::LeakyRelu,
|
||||
)));
|
||||
network.add_layer(Box::new(ActivationLayer::new(activation_functions::Type::LeakyRelu)));
|
||||
network.add_layer(Box::new(FCLayer::new(
|
||||
1,
|
||||
Initializer::GaussianWFactor(0.0, 1.0, 0.1),
|
||||
Initializer::GaussianWFactor(0.0, 1.0, 0.1)
|
||||
Initializer::GaussianWFactor(0.0, 1.0, 0.1),
|
||||
)));
|
||||
|
||||
// train network on training data
|
||||
|
@ -79,20 +87,26 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
.draw()?;
|
||||
|
||||
// add the first plot
|
||||
let data1: Vec<(f64,f64)> = x_test.iter().zip(y_test_true.iter())
|
||||
let data1: Vec<(f64, f64)> = x_test
|
||||
.iter()
|
||||
.zip(y_test_true.iter())
|
||||
.map(|(x, y)| (x[0], y[0]))
|
||||
.collect();
|
||||
chart
|
||||
.draw_series(LineSeries::new(data1, &RED)).unwrap()
|
||||
.draw_series(LineSeries::new(data1, &RED))
|
||||
.unwrap()
|
||||
.label("true values")
|
||||
.legend(|(x, y)| PathElement::new(vec![(x, y), (x + 1, y)], &RED));
|
||||
|
||||
// add the second plot
|
||||
let data2: Vec<(f64,f64)> = x_test.iter().zip(y_test_pred.iter())
|
||||
let data2: Vec<(f64, f64)> = x_test
|
||||
.iter()
|
||||
.zip(y_test_pred.iter())
|
||||
.map(|(x, y)| (x[0], y[0]))
|
||||
.collect();
|
||||
chart
|
||||
.draw_series(LineSeries::new(data2, &BLUE)).unwrap()
|
||||
.draw_series(LineSeries::new(data2, &BLUE))
|
||||
.unwrap()
|
||||
.label("predicted values")
|
||||
.legend(|(x, y)| PathElement::new(vec![(x, y), (x + 1, y)], &BLUE));
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
extern crate rust_nn;
|
||||
|
||||
use rust_nn::Network;
|
||||
use ndarray::array;
|
||||
use rust_nn::functions::{activation_functions, loss_functions};
|
||||
use rust_nn::layers::activation_layer::ActivationLayer;
|
||||
use rust_nn::layers::fc_layer::{FCLayer, Initializer};
|
||||
use ndarray::array;
|
||||
use rust_nn::Network;
|
||||
|
||||
fn main() {
|
||||
// training data
|
||||
|
@ -12,20 +12,15 @@ fn main() {
|
|||
array![0.0, 0.0],
|
||||
array![0.0, 1.0],
|
||||
array![1.0, 0.0],
|
||||
array![1.0, 1.0]
|
||||
];
|
||||
let y_train = vec![
|
||||
array![0.0],
|
||||
array![1.0],
|
||||
array![1.0],
|
||||
array![0.0]
|
||||
array![1.0, 1.0],
|
||||
];
|
||||
let y_train = vec![array![0.0], array![1.0], array![1.0], array![0.0]];
|
||||
// test data
|
||||
let x_test= vec![
|
||||
let x_test = vec![
|
||||
array![0.0, 0.0],
|
||||
array![0.0, 1.0],
|
||||
array![1.0, 0.0],
|
||||
array![1.0, 1.0]
|
||||
array![1.0, 1.0],
|
||||
];
|
||||
|
||||
// initialize neural network
|
||||
|
@ -35,15 +30,19 @@ fn main() {
|
|||
network.add_layer(Box::new(FCLayer::new(
|
||||
3,
|
||||
Initializer::Gaussian(0.0, 1.0),
|
||||
Initializer::Gaussian(0.0, 1.0)
|
||||
Initializer::Gaussian(0.0, 1.0),
|
||||
)));
|
||||
network.add_layer(Box::new(ActivationLayer::new(
|
||||
activation_functions::Type::Tanh,
|
||||
)));
|
||||
network.add_layer(Box::new(ActivationLayer::new(activation_functions::Type::Tanh)));
|
||||
network.add_layer(Box::new(FCLayer::new(
|
||||
1,
|
||||
Initializer::Gaussian(0.0, 1.0),
|
||||
Initializer::Gaussian(0.0, 1.0)
|
||||
Initializer::Gaussian(0.0, 1.0),
|
||||
)));
|
||||
network.add_layer(Box::new(ActivationLayer::new(
|
||||
activation_functions::Type::Tanh,
|
||||
)));
|
||||
network.add_layer(Box::new(ActivationLayer::new(activation_functions::Type::Tanh)));
|
||||
|
||||
// train network on training data
|
||||
network.fit(x_train, y_train, 1000, 0.1, false);
|
||||
|
@ -58,4 +57,4 @@ fn main() {
|
|||
prediction.map_mut(|x| *x = x.round());
|
||||
print!("prediction: {}\n", prediction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue