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
49
//! Provides gradient descent optimizers.
extern crate ndarray;

pub mod adam;
#[allow(dead_code)]
pub mod sgd;

pub use self::adam::Adam;
pub use self::sgd::SGD;

use crate::tensor::Tensor;
use crate::Float;
use std::cmp::{Eq, Ordering, PartialEq};

#[doc(hidden)]
/// Key to access a state tensor.
/// Stateful optimizers use this.
pub struct StateKey<'a, T: Float + 'a>(pub &'a Tensor<T>);

impl<'a, T: Float> Eq for StateKey<'a, T> {}

impl<'a, T: Float> PartialEq for StateKey<'a, T> {
    #[inline]
    /// Compares addresses of the two tensors.
    /// This can be used for ordering-based data structures (e.g. BinaryTree).
    fn eq(&self, other: &StateKey<'a, T>) -> bool {
        (self.0 as *const _) == (other.0 as *const _)
    }
}

impl<'a, T: Float> Ord for StateKey<'a, T> {
    #[inline]
    /// Compares addresses of the two tensors.
    /// This can be used for ordering-based data structures (e.g. BinaryTree).
    fn cmp(&self, other: &Self) -> Ordering {
        let a = self.0 as *const Tensor<T>;
        let b = other.0 as *const Tensor<T>;
        a.cmp(&b)
    }
}

impl<'a, T: Float> PartialOrd for StateKey<'a, T> {
    #[inline]
    /// Compares addresses of the two tensors.
    /// This can be used for ordering-based data structures (e.g. BinaryTree).
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}