-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcnn.sac
More file actions
117 lines (103 loc) · 3.13 KB
/
cnn.sac
File metadata and controls
117 lines (103 loc) · 3.13 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
module cnn;
use CommandLine: all;
use Structures: all;
use StdIO: all;
export all;
string flops2String( double flops)
{
if (flops > 1e12) {
res = sprintf( "%.2f TFlops", flops/1e12);
} else if (flops > 1e9) {
res = sprintf( "%.2f GFlops", flops/1e9);
} else if (flops > 1e6) {
res = sprintf( "%.2f MFlops", flops/1e6);
} else if (flops > 1e3) {
res = sprintf( "%.2f KFlops", flops/1e3);
} else {
res = sprintf( "%.2f Flops", flops);
}
return res;
}
#if 0
inline
float[*] averageOuter( float[+] array)
{
return with {
([0] <= iv < take( [1], shape( array))) : array[iv];
} : fold( +, genarray( drop([1], shape( array)), 0f)) / tof( shape( array)[0]);
}
#else
inline
float[*] averageOuter( float[+] array)
{
return with {
(. <= iv <= .) : sum( with { (. <= [i] <= .) : array[[i]++iv];
} : genarray( take( [1], shape( array)), 0f));
} : genarray( drop([1], shape( array)), 0f) / tof( shape( array)[0]);
}
#endif
inline
int MaxPos( float[.,.,.,.,.] output)
{
n = shape( output)[0];
max = output[[0,0,0,0,0]];
res = 0;
for( i=0; i<n; i++)
if( output[[i,0,0,0,0]] > max) {
max = output[[i,0,0,0,0]];
res = i;
}
return res;
}
//------------------------------------------------------------------------------
// Error functions
//------------------------------------------------------------------------------
inline
float MeanSquaredError( float[*] result, float[*] labels)
{
return sum ( 0.5f * ( labels - result) * ( labels - result) );
}
int[.], double MeanSquaredErrorFlop( int[.] result_shp, int[.] labels_shp)
{
return (result_shp, tod (4*prod(result_shp) + (prod(result_shp)-1) ));
}
//------------------------------------------------------------------------------
// Commandline functions
//------------------------------------------------------------------------------
int, int, int, int, float
CnnReadParameters( int epochs, int batchsize, int trainings, int tests, float rate)
{
if( (argc() == 2) && (strcmp( argv(1), "-h") == 0 )) {
printf( "%s -mt <n> -e <epocs> -b <batchsize> -tr <training-items>"
" -te <test-items> -r <rate>\n", argv(0));
epochs = 0;
batchsize = 0;
trainings = 0;
tests = 0;
rate = 0f;
} else {
if( (argc() >1) && ( strcmp( argv(1), "-mt") == 0 )) {
off = 2;
} else {
off = 0;
}
while( argc() > off+1) {
if( strcmp( argv(off+1), "-e") == 0 ) {
epochs = toi( argv( off+2));
} else if( strcmp( argv(off+1), "-b") == 0 ) {
batchsize = toi( argv( off+2));
} else if( strcmp( argv(off+1), "-tr") == 0 ) {
trainings = toi( argv( off+2));
} else if( strcmp( argv(off+1), "-te") == 0 ) {
tests = toi( argv( off+2));
} else if( strcmp( argv(off+1), "-r") == 0 ) {
rate = tof( argv( off+2));
} else {
printf( "ignoring non-recognised parameter %s!\n", argv(off+1));
off --;
}
off += 2;
}
}
return (epochs, batchsize, trainings, tests, rate);
}