0 | module NN.Architectures.Affine
6 | import Data.ComMonoid
7 | import Data.Container.Additive
8 | import Data.Autodiff.Model
14 | AffineParams : (x, y : Axis) -> y `ConsistentWith` [x] =>
16 | AffineParams x y a = (Tensor [y, x] a, Tensor [y] a)
19 | affineImpl : {x, y : Axis} ->
20 | y `ConsistentWith` [x] =>
23 | TensorMonoid x.cont => TensorMonoid y.cont =>
24 | DPair (Tensor [x] a) (const (AffineParams x y a)) -> Tensor [y] a
25 | affineImpl (
input ** (weights, bias))
26 | = matrixVectorProduct weights input + bias
29 | affinePara : {x, y : Axis} -> {a : Type} -> Num a =>
30 | y `ConsistentWith` [x] =>
32 | TensorMonoid x.cont => TensorMonoid y.cont =>
33 | Tensor [x] a -\-> Tensor [y] a
35 | (const (AffineParams x y a))
39 | affineModel : {x, y : Axis} -> {a : Type} ->
41 | y `ConsistentWith` [x] =>
43 | TensorMonoid x.cont => TensorMonoid y.cont =>
44 | Algebra (Ext y.cont) (Tensor [x] a) =>
45 | Random (Tensor [y, x] a) => Random (Tensor [y] a) =>
46 | Const (Tensor [x] a) -\-> Const (Tensor [y] a)
47 | affineModel = layer (AffineParams x y a)
48 | [| MkPair (randomRIO (-
1, 1)) (randomRIO (-
1, 1)) |]
49 | (\input, (weights, bias) =>
50 | (
matrixVectorProduct weights input + bias ** \dy =>
51 | (vectorMatrixProduct dy weights, (outer dy input, dy)))
)