annotate demo/mlp_conv.c @ 608:456a8fb9829e

spotlight
author Yoshua Bengio <bengioy@iro.umontreal.ca>
date Wed, 01 Dec 2010 18:03:11 -0500
parents 269c39f55134
children
rev   line source
540
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
1
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
2 #include "AS3.h"
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
3 #include <math.h>
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
4
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
5 typedef struct {
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
6 int n,x,y;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
7 float *w, *b;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
8 int nonlin; // 0aucune,1sigm,2tanh,3softmax
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
9 int maxpool; // 01
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
10 } LAYER;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
11
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
12 LAYER models[] = { {500,32,32,0,0,2,0},{62,1,1,0,0,3,0}, {1000,32,32,0,0,1,0},{1000,1,1,0,0,1,0},{1000,1,1,0,0,1,0},{62,1,1,0,0,1,0} };
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
13 int mod_i[] = {0,2,6};
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
14
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
15 LAYER *L;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
16 int nl;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
17
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
18 float *data, *o;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
19 #define BUF_SIZE 20000
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
20
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
21 static AS3_Val initparam(void* self, AS3_Val args) {
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
22 AS3_Val tmp = AS3_Undefined();
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
23 int il, len;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
24
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
25 for (il=0; il < sizeof(models)/sizeof(LAYER); ++il) {
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
26 tmp = AS3_Get(args, AS3_Int(2*il));
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
27 len = AS3_IntValue(AS3_GetS(tmp, "length"));
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
28 models[il].w = (float*) malloc(len);
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
29 AS3_ByteArray_readBytes(models[il].w, tmp, len);
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
30
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
31 tmp = AS3_Get(args, AS3_Int(2*il+1));
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
32 len = AS3_IntValue(AS3_GetS(tmp, "length"));
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
33 models[il].b = (float*) malloc(len);
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
34 AS3_ByteArray_readBytes(models[il].b, tmp, len);
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
35 }
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
36
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
37 data = (float*) malloc(BUF_SIZE);
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
38 o = (float*) malloc(BUF_SIZE);
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
39
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
40 return AS3_Int(0);
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
41 }
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
42
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
43 static AS3_Val choosemodel(void* self, AS3_Val args) {
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
44 int il;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
45
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
46 AS3_ArrayValue( args, "IntType", &il );
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
47
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
48 L = models + mod_i[il];
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
49 nl = mod_i[il+1] - mod_i[il];
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
50
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
51 return AS3_Int(0);
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
52 }
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
53
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
54 static AS3_Val prediction(void* self, AS3_Val args) {
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
55 AS3_Val in_arr = AS3_Undefined(), out_arr = AS3_Array(0);
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
56 float *tmp, d, e;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
57 int i,j,k,l, n,x,y, newx,newy, il, dx,dy;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
58 LAYER *pL;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
59
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
60 AS3_ArrayValue( args, "AS3ValType", &in_arr );
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
61
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
62 for(i=0; i < 1024; ++i)
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
63 data[i] = AS3_IntValue(AS3_Get(in_arr, AS3_Int(4*i+1))) /255.0;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
64
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
65 n = 1;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
66 x = 32;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
67 y = 32;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
68
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
69 #define DATA(l,j,i) data[((l)*y + (j))*x + (i)]
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
70 #define O(k,dy,dx) o[((k)*newy + (dy))*newx + (dx)]
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
71 #define W(k,l,j,i) pL->w[(((k)*n + (l))*pL->y + (j))*pL->x + (i)]
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
72
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
73 for (il=0; il < nl; ++il) {
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
74 flyield();
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
75 pL = L+il;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
76 newx = x+1-pL->x;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
77 newy = y+1-pL->y;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
78
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
79 for (dx=0; dx < newx; ++dx)
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
80 for (dy=0; dy < newy; ++dy)
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
81 for (k=0; k < pL->n; ++k) {
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
82 d = pL->b[k];
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
83 for (l=0; l < n; ++l)
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
84 for(j=0; j < pL->y; ++j)
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
85 for(i=0; i < pL->x; ++i)
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
86 d += DATA(l,j+dy,i+dx)*W(k,l,j,i);
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
87 O(k,dy,dx) = d;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
88 }
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
89
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
90 if(pL->maxpool) {
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
91 for (k=0; k < pL->n; ++k)
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
92 for (dx=0; dx < newx; dx+=2)
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
93 for (dy=0; dy < newy; dy+=2) {
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
94 d=O(k,dy,dx);
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
95 e=O(k,dy,dx+1); if(e>d) d=e;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
96 e=O(k,dy+1,dx); if(e>d) d=e;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
97 e=O(k,dy+1,dx+1); if(e>d) d=e;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
98 O(k,dy/2,dx/2)=d;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
99 }
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
100 newx /= 2;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
101 newy /= 2;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
102 }
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
103
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
104 for (dx=0; dx < newx; ++dx)
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
105 for (dy=0; dy < newy; ++dy) {
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
106 e = 0;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
107 for (k=0; k < pL->n; ++k) {
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
108 d = O(k,dy,dx);
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
109 if(pL->nonlin==1) d=1.0/(1.0 + exp(-d));
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
110 else if(pL->nonlin==2) d=tanh(d);
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
111 else if(pL->nonlin==3) { d=exp(d); e += d; }
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
112 O(k,dy,dx) = d;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
113 }
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
114 if(pL->nonlin==3 && e)
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
115 for (k=0; k < pL->n; ++k)
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
116 O(k,dy,dx) /= e;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
117 }
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
118
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
119 tmp = data;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
120 data = o;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
121 o = tmp;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
122
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
123 x = newx;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
124 y = newy;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
125 n = pL->n;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
126 }
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
127
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
128 for(i=0; i < n*x*y; ++i)
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
129 AS3_Set(out_arr, AS3_Int(i), AS3_Number(data[i]));
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
130
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
131 return out_arr;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
132 }
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
133
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
134 int main() {
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
135 AS3_Val initparamMethod = AS3_Function( NULL, initparam );
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
136 AS3_Val choosemodelMethod = AS3_Function( NULL, choosemodel );
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
137 AS3_Val predictionMethod = AS3_FunctionAsync( NULL, prediction );
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
138
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
139 AS3_Val result = AS3_Object( "initparam: AS3ValType, choosemodel: AS3ValType, prediction: AS3ValType", initparamMethod, choosemodelMethod, predictionMethod );
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
140
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
141 AS3_Release( initparamMethod );
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
142 AS3_Release( choosemodelMethod );
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
143 AS3_Release( predictionMethod );
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
144
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
145 AS3_LibInit( result );
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
146
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
147 return 0;
269c39f55134 Added demo source files
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff changeset
148 }