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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
    Appellation: params <mod>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::{build_bias, Features};
use core::ops;
use nd::linalg::Dot;
use nd::*;
use num::{One, Zero};

#[cfg(no_std)]
use alloc::vec;
#[cfg(feature = "std")]
use std::vec;

pub(crate) type Node<T> = (Array<T, Ix1>, Option<Array<T, Ix0>>);

macro_rules! constructor {
    ($call:ident where $($rest:tt)*) => {
        constructor!(@impl $call where $($rest)*);
    };
    (@impl $call:ident where $($rest:tt)*) => {
        pub fn $call<Sh>(biased: bool, shape: Sh) -> LinearParamsBase<S, D>
        where
            Sh: ndarray::ShapeBuilder<Dim = D>,
            $($rest)*
        {
            let shape = shape.into_shape();
            let dim = shape.raw_dim().clone();
            Self {
                bias: build_bias(biased, dim.clone(), |dim| ArrayBase::$call(dim)),
                weights: ArrayBase::$call(dim),
            }
        }
    };
}

pub struct LinearParamsBase<S, D = Ix2>
where
    D: RemoveAxis,
    S: RawData,
{
    pub(crate) bias: Option<ArrayBase<S, D::Smaller>>,
    pub(crate) weights: ArrayBase<S, D>,
}

impl<A, S, D> LinearParamsBase<S, D>
where
    D: RemoveAxis,
    S: RawData<Elem = A>,
{
    constructor!(default where A: Default, S: DataOwned);
    constructor!(ones where A: Clone + One, S: DataOwned);
    constructor!(zeros where A: Clone + Zero, S: DataOwned);

    pub fn new<Sh>(shape: Sh) -> Self
    where
        A: Clone + Default,
        S: DataOwned,
        Sh: ShapeBuilder<Dim = D>,
    {
        Self {
            bias: None,
            weights: ArrayBase::default(shape),
        }
    }

    pub fn biased<F>(self, builder: F) -> Self
    where
        F: Fn(D::Smaller) -> ArrayBase<S, D::Smaller>,
    {
        Self {
            bias: build_bias(true, self.raw_dim(), builder),
            ..self
        }
    }

    pub fn unbiased(self) -> Self {
        Self { bias: None, ..self }
    }

    pub fn bias(&self) -> Option<&ArrayBase<S, D::Smaller>> {
        self.bias.as_ref()
    }

    pub fn bias_mut(&mut self) -> Option<&mut ArrayBase<S, D::Smaller>> {
        self.bias.as_mut()
    }

    pub fn weights(&self) -> &ArrayBase<S, D> {
        &self.weights
    }

    pub fn weights_mut(&mut self) -> &mut ArrayBase<S, D> {
        &mut self.weights
    }

    pub fn features(&self) -> Features {
        Features::from_shape(self.shape())
    }

    pub fn inputs(&self) -> usize {
        self.features().dmodel()
    }

    pub fn is_biased(&self) -> bool {
        self.bias().is_some()
    }

    pub fn linear<T, B>(&self, data: &T) -> B
    where
        A: NdFloat,
        B: for<'a> ops::Add<&'a ArrayBase<S, D::Smaller>, Output = B>,
        S: Data<Elem = A>,
        T: Dot<Array<A, D>, Output = B>,
    {
        let dot = data.dot(&self.weights().t().to_owned());
        if let Some(bias) = self.bias() {
            return dot + bias;
        }
        dot
    }

    pub fn ndim(&self) -> usize {
        self.weights().ndim()
    }

    pub fn outputs(&self) -> usize {
        if self.ndim() == 1 {
            return 1;
        }
        self.shape()[1]
    }

    pub fn raw_dim(&self) -> D {
        self.weights().raw_dim()
    }

    pub fn shape(&self) -> &[usize] {
        self.weights().shape()
    }
}

impl<A, S> LinearParamsBase<S>
where
    S: RawData<Elem = A>,
{
    pub fn set_node(&mut self, idx: usize, node: Node<A>)
    where
        A: Clone + Default,
        S: DataMut + DataOwned,
    {
        let (weight, bias) = node;
        if let Some(bias) = bias {
            if !self.is_biased() {
                let mut tmp = ArrayBase::default(self.outputs());
                tmp.index_axis_mut(Axis(0), idx).assign(&bias);
                self.bias = Some(tmp);
            }
            self.bias
                .as_mut()
                .unwrap()
                .index_axis_mut(Axis(0), idx)
                .assign(&bias);
        }

        self.weights_mut()
            .index_axis_mut(Axis(0), idx)
            .assign(&weight);
    }
}

impl<A, S> IntoIterator for LinearParamsBase<S>
where
    A: Clone,
    S: Data<Elem = A>,
{
    type Item = Node<A>;
    type IntoIter = vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        if let Some(bias) = self.bias() {
            return self
                .weights()
                .axis_iter(Axis(0))
                .zip(bias.axis_iter(Axis(0)))
                .map(|(w, b)| (w.to_owned(), Some(b.to_owned())))
                .collect::<Vec<_>>()
                .into_iter();
        }
        self.weights()
            .axis_iter(Axis(0))
            .map(|w| (w.to_owned(), None).into())
            .collect::<Vec<_>>()
            .into_iter()
    }
}

impl<A, S> FromIterator<(Array1<A>, Option<Array0<A>>)> for LinearParamsBase<S, Ix2>
where
    A: Clone + Default,
    S: DataOwned<Elem = A> + DataMut,
{
    fn from_iter<I: IntoIterator<Item = (Array1<A>, Option<Array0<A>>)>>(nodes: I) -> Self {
        let nodes = nodes.into_iter().collect::<Vec<_>>();
        let mut iter = nodes.iter();
        let node = iter.next().unwrap();
        let shape = Features::new(node.0.shape()[0], nodes.len());
        let mut params = LinearParamsBase::default(true, shape);
        params.set_node(0, node.clone());
        for (i, node) in iter.into_iter().enumerate() {
            params.set_node(i + 1, node.clone());
        }
        params
    }
}

macro_rules! impl_from {


    (A) => {
        impl<A> From<(Array1<A>, A)> for LinearParamsBase<OwnedRepr<A>, Ix1>
        where
            A: Clone,
        {
            fn from((weight, bias): (Array1<A>, A)) -> Self {
                let bias = ArrayBase::from_elem((), bias);
                Self {
                    bias: Some(bias),
                    weights: weight,
                }
            }
        }
        impl<A> From<(Array1<A>, Option<A>)> for LinearParamsBase<OwnedRepr<A>, Ix1>
        where
            A: Clone,
        {
            fn from((weights, bias): (Array1<A>, Option<A>)) -> Self {
                Self {
                    bias: bias.map(|b| ArrayBase::from_elem((), b)),
                    weights,
                }
            }
        }
    };
    ($($bias:ty),*) => {
        $(impl_from!(@impl $bias);)*

    };
    (@impl $b:ty) => {
        impl<A, S, D> From<(ArrayBase<S, D>, Option<$b>)> for LinearParamsBase<S, D>
        where
            D: RemoveAxis,
            S: RawData<Elem = A>,
        {
            fn from((weights, bias): (ArrayBase<S, D>, Option<$b>)) -> Self {
                Self {
                    bias,
                    weights,
                }
            }
        }

        impl<A, S, D> From<(ArrayBase<S, D>, $b)> for LinearParamsBase<S, D>
        where
            D: RemoveAxis,
            S: RawData<Elem = A>,
        {
            fn from((weights, bias): (ArrayBase<S, D>, $b)) -> Self {
                Self {
                    bias: Some(bias),
                    weights,
                }
            }
        }
    };
}

impl_from!(A);
impl_from!(ArrayBase<S, D::Smaller>);