Crates.io | corgi |
lib.rs | corgi |
version | 0.9.9 |
source | src |
created_at | 2021-04-04 22:07:36.281635 |
updated_at | 2021-08-30 17:34:51.800064 |
description | Machine learning, and automatic differentation implementation for Rust |
homepage | https://github.com/patricksongzy/corgi |
repository | https://github.com/patricksongzy/corgi |
max_upload_size | |
id | 379012 |
size | 1,019,265 |
A neural network, and tensor dynamic automatic differentiation implementation for Rust.
for _ in 0..iterations {
// array operations are never in-place for corgi, so values never change
let input = Array::from((vec![batch_size, input_size], vec![...]));
let target = Array::from((vec![batch_size, output_size], vec![...]));
let _result = model.forward(input);
let loss = model.backward(target);
// update the parameters, and clear gradients (backward pass only sets gradients)
model.update();
println!("loss: {}", loss);
}
for _ in 0..10 {
c = &c + &(&a * &b);
if c[0] > 50.0 {
c = &c * &a;
}
}
c.backward(None);
Array
contains only its children.openblas
, or netlib
features can be enabled.tracked()
, or start_tracking()
must be used (see the documentation for details).tracked()
, and untracked()
in array.rs
.let initializer = initializer::he();
let relu = activation::relu();
let softmax = activation::softmax();
let ce = cost::cross_entropy();
let gd = GradientDescent::new(learning_rate);
let l1 = Dense::new(input_size, hidden_size, &initializer, Some(&relu));
let l2 = Dense::new(hidden_size, output_size, &initializer, Some(&softmax));
let mut model = Model::new(vec![&mut l1, &mut l2], &gd, &ce);
for _ in 0..iterations {
let mut input = vec![0.0; input_size * batch_size];
let mut target = vec![0.0; output_size * batch_size];
// set inputs, and targets
// arrays in corgi should not be mutated after creation, so we initialise the values first
let input = Array::from((vec![batch_size, input_size], input));
let target = Array::from((vec![batch_size, output_size], target));
let _result = model.forward(input);
let loss = model.backward(target);
// update the parameters, and clear gradients (backward pass only sets gradients)
model.update();
println!("loss: {}", loss);
}
let a = arr![5.0].tracked();
let b = arr![2.0].tracked();
let mut c = arr![0.0].tracked();
for _ in 0..10 {
c = &c + &(&a * &b);
if c[0] > 50.0 {
c = &c * &a;
}
}
assert_eq!(c, arr![195300.0]);
c.backward(None);
assert_eq!(c.gradient(), arr![1.0]);
assert_eq!(b.gradient(), arr![97650.0]);
assert_eq!(a.gradient(), arr![232420.0]);
A lot of the library was built around being as dynamic as possible, meaning if chosen well, some design choices might be similar to other dynamic computational graph libraries.
Third-party libraries were used, and can be found in Cargo.toml
.