forked from rossng/depennd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralTensor.idr
55 lines (38 loc) · 1.49 KB
/
NeuralTensor.idr
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
module NeuralTensor
import Data.Vect
import Data.List
import Matrix
import DataTensor
%access public export
interface Layer (layer : Vect inr Nat -> Vect outr Nat -> Type) where
runLayer : Tensor inr i Double
-> layer i o
-> Tensor outr o Double
data FullyConnected : (inr : Nat) -> (outr : Nat) -> Vect inr Nat -> Vect outr Nat -> Type where
MkFullyConnected : (inr = 1)
-> (outr = 1)
-> (biases : Tensor 1 o Double)
-> (weights : Tensor 2 ((head o) :: i) Double)
-> FullyConnected 1 1 i o
Layer (FullyConnected 1 1) where
runLayer {i=[m]} {o=[n]} input (MkFullyConnected inr outr biases weights) = weights #* input #+ biases
Show (FullyConnected 1 1 i o) where
show (MkFullyConnected inr outr biases weights) = "FullyConnected\n " ++ (show biases) ++ "\n " ++ (show weights)
data ReLU : Vect inr Nat -> Vect outr Nat -> Type where
MkReLU : ReLU s s
Layer (ReLU) where
runLayer input MkReLU = map (\e => if e > 0 then e else 0) input
Show (ReLU s s) where
show MkReLU = "ReLU"
data Softmax : Vect inr Nat -> Vect outr Nat -> Type where
MkSoftmax : Softmax s s
Layer (Softmax) where
runLayer input MkSoftmax = map (/ s) input'
where input' = map exp input
s = sum input'
Show (Softmax s s) where
show MkSoftmax = "Softmax"
data Logit : Vect inr Nat -> Vect outr Nat -> Type where
MkLogit : Logit s s
Show (Logit s s) where
show MkLogit = "Logit"