0 | module NN.Architectures.MLP
  1 |
  2 | import Data.Tensor
  3 | import Data.Para
  4 |
  5 | import NN.Architectures.Affine
  6 |
  7 |
  8 | ||| N-layer multi-layer perceptron with a specified activation function,
  9 | ||| and flag for whether the last layer should have it
 10 | public export
 11 | multiLayerPerceptron : {a : Type} -> Num a =>
 12 |   {ieva : Axis} ->
 13 |   (allAlg : AllAlgebra [ieva] a) =>
 14 |   TensorMonoid ieva.cont =>
 15 |   (numLayers : Nat) ->
 16 |   (activation : Tensor [ieva] a -\-> Tensor [ieva] a) ->
 17 |   {default False lastLayerActivation : Bool} ->
 18 |   Tensor [ieva] a -\-> Tensor [ieva] a
 19 | multiLayerPerceptron 0 _ = id
 20 | multiLayerPerceptron 1 activation {lastLayerActivation = False}
 21 |   = affinePara
 22 | multiLayerPerceptron 1 activation {lastLayerActivation = True}
 23 |   = composePara affinePara activation
 24 | multiLayerPerceptron (S (S k)) activation
 25 |   = composePara (composePara affinePara activation) (multiLayerPerceptron (S k) activation {lastLayerActivation = lastLayerActivation})
 26 |
 27 |
 28 | -- public export
 29 | -- threeLayerPerceptron : {inputSize, outputSize : Nat} ->
 30 | --   AllConsistent [inName, outName] [Vect outputSize, Vect inputSize] =>
 31 | --   Tensor [inputSize] [inName] Double -\-> Tensor [outputSize] [outName] Double
 32 | -- threeLayerPerceptron =
 33 | --   let hiddenDimension1 = inputSize * 10
 34 | --       hiddenDimension2 = inputSize * 10
 35 | --   in     affinePara
 36 | --      \>> trivialParam Tensor.sigmoid
 37 | --      \>> affinePara {x=Vect hiddenDimension1}
 38 | --      \>> trivialParam Tensor.sigmoid
 39 | --      \>> affinePara {x=Vect hiddenDimension2}
 40 |
 41 |
 42 |
 43 |
 44 |
 45 | -- public export
 46 | -- inputTensor : Tensor [3] Double
 47 | -- inputTensor = ># [1, 2, 3]
 48 | -- 
 49 | -- 
 50 | -- public export
 51 | -- outputTensor : IO (Tensor [2] Double)
 52 | -- outputTensor = do
 53 | --   let pr = randomRIO {io=IO} {a=(Param (trivialParam Tensor.sigmoid) inputTensor)}
 54 | --   ?asdfasdf
 55 |
 56 |   {-
 57 | public export
 58 | mlpNonDependentPara : {a : Type} -> Num a =>
 59 |   {ieva : Cont} ->
 60 |   (allAlg : AllAlgebra [ieva] a) =>
 61 |   (allAppl : All TensorMonoid [ieva]) =>
 62 |   (numLayers : Nat) ->
 63 |   (activation : CTensor [ieva] a -> CTensor [ieva] a) ->
 64 |   {default False lastLayerActivation : Bool} ->
 65 |   IsNotDependent (multiLayerPerceptron numLayers (trivialParam activation) {lastLayerActivation = lastLayerActivation})
 66 | mlpNonDependentPara 0 activation = ?oqwi -- MkNonDep () (\t, _ => t)
 67 | mlpNonDependentPara 1 activation {lastLayerActivation = False}
 68 |   = MkNonDep (AffineLayerParams ieva ieva a)
 69 |     (\x, p => (Run (multiLayerPerceptron 1 (trivialParam activation) {lastLayerActivation = False})) x p)
 70 | mlpNonDependentPara 1 activation {lastLayerActivation = True} = ?wiii_1 -- MkNonDep ?ppp ?fff
 71 | mlpNonDependentPara (S k) activation = ?wiii_11 -- MkNonDep ?ppp ?fff
 72 |   -- = MkNonDep ?ppp ?fff
 73 |
 74 |
 75 |
 76 | public export
 77 | simpleNLayerNet : {features : Nat} -> (n : Nat) ->
 78 |   Tensor [features] Double -\-> Tensor [features] Double
 79 | simpleNLayerNet n = multiLayerPerceptron n (trivialParam sigmoid)
 80 |
 81 |
 82 | public export
 83 | exampleInput : Tensor [2] Double
 84 | exampleInput = ># [1, 5]
 85 |
 86 | public export
 87 | exampleParam : Tensor [2, 2] Double
 88 | exampleParam = ># [ [0.4, 0.2]
 89 |                   , [0.7, -3]]
 90 |
 91 | public export
 92 | exampleBias : Tensor [2] Double
 93 | exampleBias = ># [0, 0]
 94 |
 95 | public export
 96 | layerParam : AffineLayerParams (Vect 2) (Vect 2) Double
 97 | layerParam = MkParams exampleParam exampleBias
 98 |
 99 | public export
100 | exampleOutput : Tensor [2] Double
101 | exampleOutput = Run (simpleNLayerNet 2) exampleInput
102 |   ((layerParam ** ()) ** layerParam)
103 | -}
104 |