#![cfg(feature = "neural_network")]
use ndarray::{Array, ArrayD};
use rustyml::neural_network::layer::activation_layer::relu::ReLU;
use rustyml::neural_network::layer::dense::Dense;
use rustyml::neural_network::loss_function::binary_cross_entropy::BinaryCrossEntropy;
use rustyml::neural_network::loss_function::categorical_cross_entropy::CategoricalCrossEntropy;
use rustyml::neural_network::loss_function::mean_absolute_error::MeanAbsoluteError;
use rustyml::neural_network::loss_function::mean_squared_error::MeanSquaredError;
use rustyml::neural_network::loss_function::sparse_categorical_cross_entropy::SparseCategoricalCrossEntropy;
use rustyml::neural_network::optimizer::sgd::SGD;
use rustyml::neural_network::sequential::Sequential;
#[test]
fn mse_test() {
let x = Array::ones((2, 4)).into_dyn();
let y = Array::ones((2, 1)).into_dyn();
let mut model = Sequential::new();
model
.add(Dense::new(4, 3, ReLU::new()).unwrap())
.add(Dense::new(3, 1, ReLU::new()).unwrap());
model.compile(SGD::new(0.01).unwrap(), MeanSquaredError::new());
model.summary();
model.fit(&x, &y, 3).unwrap();
let prediction = model.predict(&x);
println!("Prediction results: {:?}", prediction);
}
#[test]
fn mae_test() {
let x = Array::ones((2, 4)).into_dyn();
let y = Array::ones((2, 1)).into_dyn();
let mut model = Sequential::new();
model
.add(Dense::new(4, 3, ReLU::new()).unwrap())
.add(Dense::new(3, 1, ReLU::new()).unwrap());
model.compile(SGD::new(0.01).unwrap(), MeanAbsoluteError::new());
model.summary();
model.fit(&x, &y, 3).unwrap();
let prediction = model.predict(&x);
println!("Prediction results: {:?}", prediction);
}
#[test]
fn binary_cross_entropy_test() {
let x = Array::ones((2, 4)).into_dyn();
let y = Array::ones((2, 1)).into_dyn();
let mut model = Sequential::new();
model
.add(Dense::new(4, 3, ReLU::new()).unwrap())
.add(Dense::new(3, 1, ReLU::new()).unwrap());
model.compile(SGD::new(0.01).unwrap(), BinaryCrossEntropy::new());
model.summary();
model.fit(&x, &y, 3).unwrap();
let prediction = model.predict(&x);
println!("Prediction results: {:?}", prediction);
}
#[test]
fn categorical_cross_entropy_test() {
let x = Array::ones((2, 4)).into_dyn();
let y = Array::ones((2, 1)).into_dyn();
let mut model = Sequential::new();
model
.add(Dense::new(4, 3, ReLU::new()).unwrap())
.add(Dense::new(3, 1, ReLU::new()).unwrap());
model.compile(SGD::new(0.01).unwrap(), CategoricalCrossEntropy::new());
model.summary();
model.fit(&x, &y, 3).unwrap();
let prediction = model.predict(&x);
println!("Prediction results: {:?}", prediction);
}
#[test]
fn sparse_categorical_cross_entropy_test() {
let x = Array::ones((2, 4)).into_dyn();
let y: ArrayD<f32> = Array::from_shape_vec((2, 1), vec![0.0, 1.0])
.unwrap()
.into_dyn();
let mut model = Sequential::new();
model
.add(Dense::new(4, 3, ReLU::new()).unwrap())
.add(Dense::new(3, 3, ReLU::new()).unwrap());
model.compile(
SGD::new(0.01).unwrap(),
SparseCategoricalCrossEntropy::new(),
);
model.summary();
model.fit(&x, &y, 3).unwrap();
let prediction = model.predict(&x);
println!("Prediction results: {:?}", prediction);
}