comparison demo/mlp_conv.c @ 540:269c39f55134

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