pub mod io {
pub mod device;
pub(crate) mod safetensor;
}
pub mod util {
pub mod scheduler;
pub mod precision;
pub mod core;
pub mod functions;
pub mod log;
}
pub mod layers;
pub mod ffn;
#[cfg(test)]
mod tests {
use crate::ffn::FeedForwardNetwork;
use crate::io::device::GpuContext;
use crate::layers::DenseBlock;
use crate::util::core::Tensor;
use crate::util::functions::{Activation, InitFunc, InitHeUniformFunc, LossFunc, Normalisation, Regularisation};
use crate::util::functions::Optimiser::SGD;
use crate::util::log::Error;
const BATCH_SIZE: usize = 64;
fn gen_input(context: &GpuContext) -> Tensor<f32> {
Tensor::zeros(&context, &[64, 32])
}
fn gen_target(context: &GpuContext) -> Tensor<f32> {
Tensor::zeros(&context, &[64, 4])
}
#[test]
fn it_works() {
example().unwrap();
}
fn example() -> Result<(), Error> {
let context = GpuContext::new(16);
let mut init = InitHeUniformFunc::new::<f32>(10, 0.1);
let layers: Vec<DenseBlock<f32>> = vec![
DenseBlock::new(&context, true, 32, 16, BATCH_SIZE, &mut init,
Normalisation::Disabled, Activation::LeakyReLU(0.01), Regularisation::None, 0.1),
DenseBlock::new(&context, true, 16, 8, BATCH_SIZE, &mut init,
Normalisation::Disabled, Activation::LeakyReLU(0.01), Regularisation::None, 0.1),
DenseBlock::new(&context, true, 8, 4, BATCH_SIZE, &mut init,
Normalisation::Disabled, Activation::Identity, Regularisation::None, 0.1),
];
let network = FeedForwardNetwork::<f32>::new(layers, 32);
let input = &gen_input(&context); let sgd = SGD(0.9, true); let outputs = network.forward(&context, &input, BATCH_SIZE, true, 1)?;
network.backward(&context, &outputs, &gen_target(&context), &input,
LossFunc::CrossEntropyLoss, Activation::Softmax, BATCH_SIZE,
&[sgd, sgd, sgd], &[sgd, sgd, sgd], 0.01, f32::MAX, 1)?;
Ok(())
}
}