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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use super::*;
use crate::{shapes::*, tensor::*};

/// Reduction alogn multiple axes using variance
pub trait VarTo: HasErr + HasShape {
    /// Result [Tensor] has smaller number of dimensions.
    ///
    /// **Pytorch equivalent**: `t.var(Axes, unbiased=False)`
    ///
    /// Examples:
    /// ```rust
    /// # use dfdx::prelude::*;
    /// # let dev: Cpu = Default::default();
    /// let t = dev.tensor([[2.0f32, 3.0, 4.0], [3.0, 6.0, 9.0]]);
    /// let r = t.var::<Rank1<2>, _>(); // or `var::<_, Axis<1>>()`
    /// assert_eq!(r.array(), [0.6666667, 6.0]);
    /// ```
    fn var<Dst: Shape, Ax: Axes>(self) -> Self::WithShape<Dst>
    where
        Self::Shape: HasAxes<Ax> + ReduceShapeTo<Dst, Ax>,
    {
        self.try_var().unwrap()
    }
    /// Fallible version of [VarTo::var]
    fn try_var<Dst: Shape, Ax: Axes>(self) -> Result<Self::WithShape<Dst>, Self::Err>
    where
        Self::Shape: HasAxes<Ax> + ReduceShapeTo<Dst, Ax>;
}

impl<S: Shape, E: Dtype, D: Device<E>, T: Tape<E, D>> VarTo for Tensor<S, E, D, T> {
    fn try_var<Dst: Shape, Ax: Axes>(self) -> Result<Self::WithShape<Dst>, Self::Err>
    where
        Self::Shape: HasAxes<Ax> + ReduceShapeTo<Dst, Ax>,
    {
        let mean = self
            .retaped::<T>()
            .try_mean::<Dst, Ax>()?
            .try_broadcast_like(self.shape())?;
        mean.try_sub(self)?.try_square()?.try_mean()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tests::*;

    #[test]
    fn test_var_axis_0_2d() {
        let dev: TestDevice = Default::default();
        let t = dev
            .tensor([[1.0, 2.0, 3.0, 4.0], [0.0, 2.0, 5.0, 10.0]])
            .to_dtype::<TestDtype>();
        let r = t.leaky_trace().var::<Rank1<4>, _>();
        assert_close_to_literal!(r, [0.25, 0.0, 1.0, 9.0]);
        let g = r.mean().backward();
        assert_close_to_literal!(
            g.get(&t),
            [[0.125, 0.0, -0.25, -0.75], [-0.125, 0.0, 0.25, 0.75]]
        );
    }

    #[test]
    fn test_var_axis_1_2d() {
        let dev: TestDevice = Default::default();
        let t = dev
            .tensor([[1.0, 2.0, 3.0, 4.0], [0.0, 2.0, 5.0, 10.0]])
            .to_dtype::<TestDtype>();
        let r = t.leaky_trace().var::<Rank1<2>, _>();
        assert_close_to_literal!(r, [1.25, 14.1875]);
        let g = r.mean().backward();
        assert_close_to_literal!(
            g.get(&t),
            [
                [-0.375, -0.125, 0.125, 0.375],
                [-1.0625, -0.5625, 0.1875, 1.4375]
            ]
        );
    }
}