-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathProgram.fs
More file actions
95 lines (75 loc) · 3.16 KB
/
Copy pathProgram.fs
File metadata and controls
95 lines (75 loc) · 3.16 KB
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
open Tensor
open SymTensor
open SymTensor.Compiler.Cuda
open Models
open Datasets
open Optimizers
[<EntryPoint>]
let main argv =
//let device = DevHost
let device = DevCuda
let mnist = Mnist.load ("../../../../Data/MNIST") 0.1
let mnist = if device = DevCuda then TrnValTst.toCuda mnist else mnist
// define symbolic sizes
let mb = ModelBuilder<single> "NeuralNetModel"
let nBatch = mb.Size "nBatch"
let nInput = mb.Size "nInput"
let nClass = mb.Size "nClass"
// define model parameters
let mlp =
MLP.pars (mb.Module "MLP")
{ Layers = [{NeuralLayer.defaultHyperPars with NInput=nInput; NOutput=nClass; TransferFunc=ActivationFunc.Tanh}]
LossMeasure = LossLayer.MSE }
// define variables
let input1 : ExprT = mb.Var<single> "Input1" [nBatch; nInput]
let input2 : ExprT = mb.Var<single> "Input2" [nBatch; nInput]
let target : ExprT = mb.Var<single> "Target" [nBatch; nClass]
let inputB = Expr.sumKeepingAxis 1 input1
let a0, a1 = ElemExpr.arg2<single>
let i0, i1 = ElemExpr.idx2
let input3 =
Expr.elements [nBatch; nInput] (a0[i0; i1] * a1[i0; SizeSpec.fix 0L]) [input1; inputB]
// instantiate model
mb.SetSize nInput mnist.Trn.[0L].Input.Shape.[0]
mb.SetSize nClass mnist.Trn.[0L].Target.Shape.[0]
let mi = mb.Instantiate device
let pred = MLP.pred mlp input3
let loss = MLP.loss mlp input3 target
let smplVarEnv (smpl: InputTargetSampleT) =
VarEnv.empty
|> VarEnv.add input1 smpl.Input
|> VarEnv.add input2 smpl.Input
|> VarEnv.add target smpl.Target
let trainable =
Train.trainableFromLossExpr mi loss smplVarEnv GradientDescent.New GradientDescent.DefaultCfg
let trainCfg : Train.Cfg = {
Train.defaultCfg with
Seed = 100
//BatchSize = 10
BatchSize = 10000L
LossRecordInterval = 10
Termination = Train.ItersWithoutImprovement 100
MinImprovement = 1e-7
TargetLoss = None
MinIters = Some 100
MaxIters = None
LearningRates = [1e-3; 1e-4; 1e-5]
}
//Debug.Timing <- true
//Debug.TraceCompile <- true
//Debug.VisualizeUExpr <- true
//Debug.DisableCombineIntoElementsOptimization <- true
Debug.VisualizeExecItems <- true
//Debug.TerminateAfterCompilation <- true
let lossFn = mi.Func loss |> arg3 input1 input2 target
let initialLoss = lossFn mnist.Trn.All.Input mnist.Trn.All.Input mnist.Trn.All.Target |> Tensor.value
printfn "Initial training loss: %f" initialLoss
printfn "Computing dLoss / dInput1."
Debug.VisualizeUExpr <- true
let dLoss = Deriv.compute loss |> Deriv.ofVar input1
let dLossFn = mi.Func dLoss |> arg3 input1 input2 target
let dLossVal =
dLossFn (mnist.Trn.Part(0L,10L).All.Input) (mnist.Trn.Part(0L,10L).All.Input) (mnist.Trn.Part(0L,10L).All.Target)
printfn "Done."
let result = Train.train trainable mnist trainCfg
0