1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
pub mod optimizers;

pub use self::optimizers::*;

use context::Context;
use ndarray_ext::NdArray;
use tensor::Tensor;


#[inline]
/// Updates shared variables with its gradients
///
/// This actually runs the computation graph.
/// For the usage, see `examples` dir in repo.
pub fn update<T: Optimizer>(
    variables: &[&Tensor],
    gradients: &[Tensor],
    optimizer: &mut T,
    ctx: &mut Context,
)
{
    assert_eq!(variables.len(), gradients.len());
    // run graph and get gradient arrays
    let gradient_refs = gradients.iter().map(|a| a).collect::<Vec<_>>();
    let mut grad_arrays = ::eval::eval_tensors(
        gradient_refs.as_slice(),
        &mut ctx.variables,
        &mut ctx.outputs,
    );
    ctx.outputs.clear();
    for v in variables {
        // safe unwrap
        assert_eq!(v.op.name(), "Variable", "Can't optimize non-variable");
        let mut v_arr = ctx.variables.get_mut(v).unwrap();
        let g = grad_arrays.remove(0);
        optimizer.update(v, v_arr, g);
    }
}

/// Trait for any gradient descent optimizer
pub trait Optimizer {
    #[inline]
    /// Updates the variable tensor
    ///
    /// Updates `param` with `grad`.
    /// `node` is a symbolic representation of `param`
    fn update(&mut self, node: &Tensor, param: &mut NdArray, grad: NdArray);
}