File size: 14,457 Bytes
d5bfab8 |
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mkl.h>
#include <math.h>
#include <string.h>
#include <omp.h>
#define DIM 64
#define BUFFER 10000000
// M is the number of lines (output), N is the number of column (input)
// DIM has to be bigger than maximum output dimension of heads
//shuffle
void swap(long *a, long *b) {
long temp = *a;
*a = *b;
*b = temp;
}
void shuffle(long n ,long A[]) {
srand(time(NULL));
long i;
for(i = n-1; i > 0; i--) {
long j = rand() % (i+1);
swap(&A[i], &A[j]);
}
}
//reading input data
void read(const char* File, long A[]) {
FILE *myFile;
myFile = fopen(File, "r");
long i = 0;
char *line;
line = (char*)malloc (BUFFER);
while (myFile && fgets(line,BUFFER,myFile)) {
char* token = strtok(line, " ");
while (token) {
long num;
if (sscanf(token, "%ld", &num) == 1) {A[i] = num; i++;}
token = strtok(NULL, " ");
}
}
free (line);
}
// Warning: does scaling too
void read_double(const char* File, double A[]) {
FILE *myFile;
myFile = fopen(File, "r");
long i = 0;
char *line;
line = (char*)malloc (BUFFER);
while (myFile && fgets(line,BUFFER,myFile)) {
char* token = strtok(line, " ");
while (token) {
double d;
if (sscanf(token, "%lf", &d) == 1) {A[i] = 2.0 * d - 1.0; i++;}
token = strtok(NULL, " ");
}
}
free (line);
}
// Printing functions
void print_vecti (char *s, long size, long X[]) {
printf("%s\n",s);
for (long i = 0; i < size; ++i){printf("%li ", X[i]);}
printf ("\n");
}
void print_vect (char *s, long size, double X[]) {
printf("%s\n",s);
for (long i = 0; i < size; ++i){printf("%f ", X[i]);}
printf ("\n");
}
void print_mat (char *s, long n, long m, double A[]) {
printf("%s\n",s);
for (long i = 0; i < m*n; ++i)
{printf("%.16f ",A[i]); if (i % n == n - 1) {printf("\n");}}
}
// initialization
double randfrom(double min, double max)
{
double range = (max - min);
double div = RAND_MAX / range;
return min + (rand() / div);
}
void rand_mat (long arity, double A[]) {
long size = (arity * DIM + 1) * DIM;
double coeff = sqrt (6.0 / ((arity * DIM + 1) + DIM));
for (long i = 0; i < size; ++i) {A[i] = coeff * randfrom(-1.0, 1.0);}
}
void fixed_mat (long arity, double A[]) {
long size = (arity * DIM + 1) * DIM;
for (long i = 0; i < size; ++i) {A[i] = 0.1;}
}
void rand_head (long out, double A[]) {
long size = (DIM + 1) * out;
double coeff = sqrt (6.0 / ((DIM + 1) + out));
for (long i = 0; i < size; ++i) {A[i] = coeff * randfrom(-1.0, 1.0);}
}
void fixed_head (long out, double A[]) {
long size = (DIM + 1) * out;
for (long i = 0; i < size; ++i) {A[i] = 0.1;}
}
void zero_vect (long size, double A[]) {
for (long long i = 0; i < size; ++i) {A[i] = 0.0;}
}
void zero_ivect (long size, long A[]) {
for (long i = 0; i < size; ++i) {A[i] = 0;}
}
void zero_mat (long arity, double A[]) {
zero_vect ((arity * DIM + 1) * DIM, A);
}
void zero_head (long out, double A[]) {
zero_vect ((DIM + 1) * out, A);
}
void copy (long n, double X[], double Y[]) {
cblas_dcopy (n,X,1,Y,1);}
void one_vect (long size, double X[])
{for (long i = 0; i < size; ++i) {X[i] = 1.0;}}
// computation
void tensor (double lr, long arity, double A[], double B[], double C[]) {
long indim = arity * DIM + 1;
cblas_dgemm
(CblasRowMajor, CblasNoTrans, CblasNoTrans,
DIM, indim, 1, lr, A, 1, B, indim, 0.0, C, indim);
//print_mat ("tensor",indim,DIM,C);
}
void mv (long arity, double A[], double X[], double Y[]) {
long indim = arity * DIM + 1;
//print_vect ("in",indim,X);
cblas_dgemv (CblasRowMajor,CblasNoTrans,DIM,indim,1.0,A,indim,X,1,0.0,Y,1);
//print_vect ("out",DIM,Y);
}
void tmv (long arity, double A[], double X[], double Y[]) {
long indim = arity * DIM + 1;
//print_vect ("dout",DIM,X);
cblas_dgemv (CblasRowMajor,CblasTrans,DIM,indim,1.0,A,indim,X,1,0.0,Y,1);
//print_vect ("din",indim,Y);
}
//same thing with special dimensions for the heads
void tensor_head (double lr, long out, double A[], double B[], double C[]) {
long indim = DIM + 1;
cblas_dgemm
(CblasRowMajor, CblasNoTrans, CblasNoTrans,
out, indim, 1, lr, A, 1, B, indim, 0.0, C, indim);
//print_mat ("tensor",indim,out,C);
}
void mv_head (long out, double A[], double X[], double Y[]) {
long indim = DIM + 1;
//print_vect ("in",indim,X);
cblas_dgemv (CblasRowMajor,CblasNoTrans,out,indim,1.0,A,indim,X,1,0.0,Y,1);
//print_vect ("out",out,Y);
}
void tmv_head (long out, double A[], double X[], double Y[]) {
long indim = DIM + 1;
//print_vect ("dout",out,X);
cblas_dgemv (CblasRowMajor,CblasTrans,out,indim,1.0,A,indim,X,1,0.0,Y,1);
//print_vect ("din",indim,Y);
}
// Update
void clip (long arity, double A[]) {
for (long i = 0; i < (arity * DIM + 1)*DIM; ++i) {
if (A[i] > 4.0) {A[i] = 4.0;}
if (A[i] < -4.0) {A[i] = -4.0;}
}
}
void clip_head (long out, double A[]) {
for (long i = 0; i < (DIM + 1)*out; ++i) {
if (A[i] > 4.0) {A[i] = 4.0;}
if (A[i] < -4.0) {A[i] = -4.0;}
}
}
void dtanh (long size, double C[], double X[], double Y[]) {
//print_vect ("doutn",size,X);
for (long i = 0; i < size ; ++i) {
double dtanh_temp = C[i];
Y[i] = (1.0 - dtanh_temp * dtanh_temp) * X[i];
}
}
int main()
{
// some indices
long i;
long tmpex,ex,nex,ep,op,nop;
long offset,sub,di,nhead;
long sum;
double err;
long a1,a2,a3;
long max_threads;
// main arguments
long ARG[3];
// operators
long *ARITY, *HEAD;
// example structures
long *SIZE, *CUMUL, *SHF, *D;
double *OBJ, *OBJcur;
// reading arguments
read("data/arg.txt",ARG);
nop = ARG[0];
nex = ARG[1];
printf ("nop: %li\n", nop);
printf ("nex: %li\n", nex);
// allocating operators/examples
ARITY = (long*)mkl_malloc(nop*sizeof(long),64);
HEAD = (long*)mkl_malloc(nop*sizeof(long),64);
SIZE = (long*)mkl_malloc(nex*sizeof(long),64);
SHF = (long*)mkl_malloc(nex*sizeof(long),64);
CUMUL = (long*)mkl_malloc(nex*sizeof(long),64);
// reading operators/examples
read("data/arity.txt",ARITY);
read("data/head.txt",HEAD);
read("data/size.txt",SIZE);
// reading dag of examples
sum = 0;
for (ex = 0; ex < nex; ++ex) {sum += SIZE[ex];};
printf("sum: %li\n", sum);
long bD = 4;
D = (long*)mkl_malloc(bD*sum*sizeof(long),64);
read("data/dag.txt",D);
nhead = 0;
for (di = 0; di < sum; ++di) {
if (HEAD [D[bD*di]] > 0) {++nhead;}
}
printf("nhead: %li\n", nhead);
CUMUL[0]=0;
for (ex = 1; ex < nex; ++ex) {CUMUL[ex]=CUMUL[ex-1]+SIZE[ex-1];}
for (ex = 0; ex < nex; ++ex) {SHF[ex]=ex;}
// potential objectives for each head in the dag
printf("allocating doubles\n");
OBJ = (double*)mkl_malloc(DIM*sum*sizeof(double),64);
printf("reading doubles\n");
read_double("data/obj.txt",OBJ);
printf("success doubles\n");
//fixed biais for nullary operators
double biais [1] = {1.0};
//weights for each operator
long bA = (3*DIM+1)*DIM;
double *A, *Acur;
A = (double*)mkl_malloc(bA*nop*sizeof( double ),64);
for (op=0; op < nop; ++op) {
if (HEAD[op] > 0) {
//fixed_head (HEAD[op],A+bA*op);
rand_head (HEAD[op],A+bA*op);
}
else {
//fixed_mat (ARITY[op],A+op*bA);
rand_mat (ARITY[op],A+op*bA);
}
}
//update matrix for each operator
double *U, *Ucur;
U = (double*)mkl_malloc(bA*nop*sizeof( double ),64);
void zeroU () {
for (op=0; op < nop; ++op) {
if (HEAD[op] == 0) {zero_mat (ARITY[op],U+op*bA);}
else {zero_head (HEAD[op],U+bA*op);}
}
}
zeroU ();
//check if an operator was updated
//long *UPD;
//UPD = (long*)mkl_malloc(nop*sizeof( long ),64);
//computation trace for each example
long bX = 3*DIM+1;
long bY = DIM;
double *X, *Y, *TY;
double *Xcur, *Ycur, *TYcur;
X = (double*)mkl_malloc(bX*sum*sizeof( double ),64);
Y = (double*)mkl_malloc(bY*sum*sizeof( double ),64);
TY = (double*)mkl_malloc(bY*sum*sizeof( double ),64);
one_vect (bX*sum,X);
double *GX, *GY, *GTY;
double *GXcur, *GYcur, *GTYcur;
GX = (double*)mkl_malloc(bX*sum*sizeof( double ),64);
GY = (double*)mkl_malloc(bY*sum*sizeof( double ),64);
GTY = (double*)mkl_malloc(bY*sum*sizeof( double ),64);
// setting the number of threads
max_threads = mkl_get_max_threads();
printf ("max_threads: %li\n", max_threads);
mkl_set_num_threads(1);
printf ("threads: %i\n", 1);
//training
long EP = 500;
double lr = 0.001;
for (ep = 0; ep < EP; ++ep) {
if (ep == 250) {lr = 0.0001;}
shuffle (nex,SHF);
zero_vect (bY*sum,GTY); //zeroing gradients of every examples
err = 0;
//if (ep == 250) {lr = 0.0001;}
#pragma omp parallel for num_threads(8) private(tmpex,ex,offset,di,op,sub,Xcur,Ycur,TYcur,Acur,GXcur,GYcur,GTYcur,OBJcur,Ucur,a1,a2,a3)
for (tmpex = 0; tmpex < nex; ++tmpex) {
ex = SHF[tmpex];
offset = CUMUL[ex];
//double *Acopy;
//Acopy = (double*)mkl_malloc(bA*nop*sizeof( double ),64);
//copy (bA*nop,A,Acopy);
//forward pass
for (sub = 0; sub < SIZE[ex]; ++sub) {
di = offset + sub;
op = D[bD*di];
//printf ("oper: %li %li %li\n", op, D[bD*di+1], D[bD*di+2]);
Xcur = X + bX * di;
Ycur = Y + bY * di;
TYcur = TY + bY * di;
Acur = A + bA * op;
if (HEAD[op] > 0) {
a1 = D[bD*di+1];
copy (DIM, TY + bY * (offset+a1), Xcur);
mv_head (HEAD[op], Acur, Xcur, Ycur);
vdTanh (HEAD[op],Ycur,TYcur);
}
else {switch(ARITY[op]){
case 0:
mv (ARITY[op],Acur,biais,Ycur);
copy (bY, Ycur, TYcur);
break;
case 1:
a1 = bY * (offset + D[bD*di+1]);
copy (DIM, TY + a1, Xcur);
mv (ARITY[op], Acur, Xcur, Ycur);
vdTanh (bY,Ycur,TYcur);
break;
case 2:
a1 = bY * (offset + D[bD*di+1]);
a2 = bY * (offset + D[bD*di+2]);
copy (DIM, TY + a1, Xcur);
copy (DIM, TY + a2, Xcur + bY);
mv (ARITY[op], Acur, Xcur, Ycur);
vdTanh (bY,Ycur,TYcur);
break;
case 3:
a1 = bY * (offset + D[bD*di+1]);
a2 = bY * (offset + D[bD*di+2]);
a3 = bY * (offset + D[bD*di+3]);
copy (DIM, TY + a1, Xcur);
copy (DIM, TY + a2, Xcur + bY);
copy (DIM, TY + a3, Xcur + bY*2);
mv (ARITY[op], Acur, Xcur, Ycur);
vdTanh (bY,Ycur,TYcur);
break;
default: break;
}}
//print_vect ("outn",bY,TYcur);
}//end forward pass
//backward pass
for (sub = SIZE[ex] - 1; sub >= 0; --sub) {
di = offset + sub;
op = D[bD*di];
Xcur = X + bX * di;
Ycur = Y + bY * di;
TYcur = TY + bY * di;
GXcur = GX + bX * di;
GYcur = GY + bY * di;
GTYcur = GTY + bY * di;
Acur = A + bA * op;
if (HEAD[op] > 0) {
//print_vect ("expectv",HEAD[op],OBJ+ex);
OBJcur = OBJ + DIM * di;
vdSub (HEAD[op], OBJcur, TYcur, GTYcur);
//print_vect ("diff",HEAD[op],GTYcur);
err += sqrt (cblas_ddot (HEAD[op],GTYcur,1,GTYcur,1));
dtanh (HEAD[op], TYcur, GTYcur, GYcur);
tmv_head (HEAD[op], Acur, GYcur, GXcur);
a1 = bY * (offset + D[bD*di+1]);
vdAdd (bY, GTY + a1, GXcur, GTY + a1);
}
else {switch(ARITY[op]){
case 0:
copy (bY, GTYcur, GYcur);
tmv (ARITY[op], Acur, GYcur, GXcur);
break;
case 1:
dtanh (bY, TYcur, GTYcur, GYcur);
tmv (ARITY[op], Acur, GYcur, GXcur);
a1 = bY * (offset + D[bD*di+1]);
vdAdd (bY, GTY + a1, GXcur, GTY + a1);
break;
case 2:
dtanh (bY, TYcur, GTYcur, GYcur);
tmv (ARITY[op], Acur, GYcur, GXcur);
a1 = bY * (offset + D[bD*di+1]);
a2 = bY * (offset + D[bD*di+2]);
vdAdd (bY, GTY + a1, GXcur, GTY + a1);
vdAdd (bY, GTY + a2, GXcur + bY, GTY + a2);
break;
case 3:
dtanh (bY, TYcur, GTYcur, GYcur);
tmv (ARITY[op], Acur, GYcur, GXcur);
a1 = bY * (offset + D[bD*di+1]);
a2 = bY * (offset + D[bD*di+2]);
a3 = bY * (offset + D[bD*di+3]);
vdAdd (bY, GTY + a1, GXcur, GTY + a1);
vdAdd (bY, GTY + a2, GXcur + bY, GTY + a2);
vdAdd (bY, GTY + a3, GXcur + bY*2, GTY + a3);
break;
default: break;
}}
}//end backward pass
//mkl_free (Acopy);
//updates
//zero_ivect (nop,UPD);
for (sub = SIZE[ex] - 1; sub >= 0; --sub) {
di = offset + sub;
op = D[bD*di];
//UPD[op] = 1;
Acur = A + bA * op;
Ucur = U + bA * op;
Xcur = X + bX * di;
GYcur = GY + bY * di;
if (HEAD [op] > 0) {
tensor_head (lr,HEAD[op],GYcur,Xcur,Ucur);
vdAdd ((DIM+1)*HEAD[op],Acur,Ucur,Acur);
}
else {
if (ARITY[op] == 0) {
tensor (lr,ARITY[op],GYcur,biais,Ucur);
vdAdd ((ARITY[op]*DIM+1)*DIM,Acur,Ucur,Acur);
}
else {
tensor (lr,ARITY[op],GYcur,Xcur,Ucur);
vdAdd ((ARITY[op]*DIM+1)*DIM,Acur,Ucur,Acur);
}
}
}
//clipping
if (ep % 10 == 9) {
for (op = 0; op < nop; ++op) {
//if (UPD[op] > 0) {
Acur = A + bA * op;
if (HEAD [op] > 0) {clip_head (HEAD[op],Acur);
//print_mat ("A",DIM+1,HEAD[op],Acur);
}
else {clip (ARITY[op],Acur);
//print_mat ("A",ARITY[op]*DIM+1,DIM,Acur);
}
//}
}//end clipping
}
}//end example
printf("%li: %f\n", ep, err / nhead);
fflush(stdout);
}//end training
// export to standard ml
printf("START MATRICES\n");
for (op = 0; op < nop; ++op) {
Acur = A + bA * op;
if (HEAD [op] > 0) {print_mat ("A",DIM+1,HEAD[op],Acur);}
else {print_mat ("A",ARITY[op]*DIM+1,DIM,Acur);}
}
// export to openblas
FILE *fp;
fp = fopen("/home/thibault/big/repos/oeis/tnn_in_c/ob_mat", "w");
fprintf(fp, "double A[%ld] = {", bA*nop);
for (i = 0; i < bA*nop; i++)
{
fprintf(fp, "%.16f", A[i]);
if (i < bA*nop - 1) {fprintf(fp, ", ");}
else {fprintf(fp,"};\n");}
}
fclose(fp);
fp = fopen("/home/thibault/big/repos/oeis/tnn_in_c/ob_head", "w");
fprintf(fp, "long HEAD[%ld] = {", nop);
for (i = 0; i < nop; i++)
{
fprintf(fp, "%ld", HEAD[i]);
if (i < nop - 1) {fprintf(fp, ", ");}
else {fprintf(fp,"};\n");}
}
fclose(fp);
fp = fopen("/home/thibault/big/repos/oeis/tnn_in_c/ob_arity", "w");
fprintf(fp, "long ARITY[%ld] = {", nop);
for (i = 0; i < nop; i++)
{
fprintf(fp, "%ld", ARITY[i]);
if (i < nop - 1) {fprintf(fp, ", ");}
else {fprintf(fp,"};\n");}
}
fclose(fp);
return 0;
}
|