root/trunk/test_xor.d

Revision 33, 1.4 kB (checked in by qbert, 5 years ago)

Tango compatible

Line 
1 /** A simple XOR test.  */
2
3
4 import and.api;
5 import and.platform;
6
7
8 const real [] [] inputs  = [
9                 [ 0,0 ],
10                 [ 0,1 ],
11                 [ 1,0 ],
12                 [ 1,1 ]             
13
14 ];
15
16
17 const real [] [] outputs = [
18                 [ 0 ],
19                 [ 1 ],
20                 [ 1 ],
21                 [ 0 ]
22 ];
23
24
25 void main ()
26 {
27
28   try
29     {
30
31
32       IActivationFunction f = new TanhActivationFunction;
33
34
35       Layer input = new Layer(2,0);
36       Layer [] hidden = [ new Layer(4,2,f)  ];
37       Layer output = new Layer(1,4,f) ;
38
39       NeuralNetwork nn = new NeuralNetwork(input,hidden,output );
40       CostFunction cost = new SSE();
41       BackPropagation bp = new BackPropagation(nn,cost);
42
43       bp.epochs = uint.max;
44       bp.learningRate = 0.1;
45       bp.momentum = 0.5;
46       bp.errorThreshold = 0.0001;
47
48       void callback( uint currentEpoch, real currentError  )
49     {
50       Stdout.formatln("Epoch: [ {0} ] | Error [ {1} ] ",currentEpoch, currentError );
51     }
52
53       bp.setProgressCallback(&callback, 100 );
54      
55       bp.train(inputs,outputs);
56
57       Stdout.formatln("{0}",bp.actualEpochs);
58       Stdout.formatln("{0}" ,bp.computeOutput(inputs[0])[0]);
59       Stdout.formatln("{0}" ,bp.computeOutput(inputs[1])[0]);
60       Stdout.formatln("{0}" ,bp.computeOutput(inputs[2])[0]);
61       Stdout.formatln("{0}" ,bp.computeOutput(inputs[3])[0]);
62     }
63   catch ( Exception e )
64     {
65       Stdout.formatln("Exception: {0}",e.toUtf8() );
66     }
67
68 }
Note: See TracBrowser for help on using the browser.