-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathzhang-batch.sac
More file actions
95 lines (67 loc) · 2.52 KB
/
zhang-batch.sac
File metadata and controls
95 lines (67 loc) · 2.52 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
import cnn: all;
import convolution: all;
import activation: all;
import pooling: all;
use Benchmarking: all;
use MathArray: all;
use Structures: all;
use StdIO: all;
#define CATEGORIES 10
#define BATCHSIZE 8
noinline
void donormal( int trainings, int batchsize, float[60000,28,28] training_images, float[6,5,5] k1, float[6] b1)
{
i1 = getInterval( "normal", 0);
res = genarray( [6,24,24], 0f);
c1 = genarray( [batchsize, 6,24,24], 0f);
start( i1);
for( i=0; i< trainings/batchsize; i++) {
c1+= with {
(.<= iv <= .) {
in = training_images[i*(batchsize)+iv];
} : MultiConv( in, k1, b1 );
} : genarray([batchsize], res);
}
end( i1);
printResult( i1);
print( shape(c1));
print( (c1[0,0,0,0]));
}
noinline
void dobatch( int trainings, int batchsize, float[7500,28,28,8] training_images, float[6,5,5] k1, float[6] b1)
{
i2 = getInterval( "batch", 0);
res = genarray( [6,24,24,BATCHSIZE], 0f);
c1 = genarray( [batchsize/BATCHSIZE, 6,24,24,BATCHSIZE], 0f);
start( i2);
for( i=0; i< trainings/batchsize; i++) {
c1+= with {
(.<= iv <= .) {
in = training_images[i*(batchsize/BATCHSIZE)+iv];
} : MultiConvBatch( in, k1, b1 );
} : genarray([batchsize/BATCHSIZE], res);
}
end( i2);
printResult( i2);
print( shape(c1));
print( (c1[0,0,0,0,0]));
}
int main()
{
epochs, batchsize, trainings, tests, rate = CnnReadParameters( 20, 100, 1000, 10000, 0.5f);
k1 = genarray( [6,5,5], 1f/25f);
b1 = genarray( [6], 1f/6f);
printf( "Reading training images ...\n");
training_images = ( float[60000,28,28]) mnist::ReadImages( "tutorial/train-images.idx3-ubyte");
printf( "Read %d training images ...\n", shape(training_images)[0]);
printf( "Running Zhang with %d epochs, batchsize %d, %d training images, %d tests, and a rate of %f\n",
epochs, batchsize, trainings, tests, tod( rate));
donormal( trainings, batchsize, training_images, k1, b1);
printf( "Reading batched training images ...\n");
training_images = ( float[7500,28,28,BATCHSIZE]) mnist::ReadImagesBatch( "tutorial/train-images.idx3-ubyte", BATCHSIZE);
printf( "Read %d training images ...\n", shape(training_images)[0]*BATCHSIZE);
printf( "Running Zhang with %d epochs, batchsize %d, %d training images, %d tests, and a rate of %f\n",
epochs, batchsize, trainings, tests, tod( rate));
dobatch( trainings, batchsize, training_images, k1, b1);
return 0;
}