66 static int i, il,
in,
j,
m, mp1;
74 for(j=0;j<
m;j++)
NET.Outn[0][j] = rrin[j];
77 for(i=mp1; i<=
NET.Nneur[0]; i+=4)
79 NET.Outn[0][i-1] = rrin[i-1];
80 NET.Outn[0][
i] = rrin[
i];
81 NET.Outn[0][i+1] = rrin[i+1];
82 NET.Outn[0][i+2] = rrin[i+2];
90 for(il=2; il<
NET.Nlayer; il++)
93 deriv1[il-1],
NET.Nneur[il-1]);
95 NET.Outn[il],
NET.Nneur[il],
98 for(in=0; in<
NET.Nneur[
NET.Nlayer-1]; in++)
100 deriv1[
NET.Nlayer-1][
in] = 1;
117 static int i, il,
in,
j, ilm1,
m, mp1;
124 for(j=0;j<
m;j++)
NET.Outn[0][j] = rrin[j];
127 for(i=mp1; i<=
NET.Nneur[0]; i+=4)
129 NET.Outn[0][i-1] = rrin[i-1];
130 NET.Outn[0][
i] = rrin[
i];
131 NET.Outn[0][i+1] = rrin[i+1];
132 NET.Outn[0][i+2] = rrin[i+2];
140 for(il=1; il<
NET.Nlayer; il++)
143 m =
NET.Nneur[ilm1]%4;
144 for(in=0; in<
NET.Nneur[il]; in++)
146 a =
NET.Weights[il][
in][0];
148 for(j=1;j<=
m;j++) a +=
149 NET.Weights[il][in][j]*
NET.Outn[ilm1][j-1];
152 for(j=mp1; j<=
NET.Nneur[ilm1]; j+=4)
155 NET.Weights[il][
in][j+3]*
NET.Outn[ilm1][j+2]+
156 NET.Weights[il][
in][j+2]*
NET.Outn[ilm1][j+1]+
157 NET.Weights[il][
in][j+1]*
NET.Outn[ilm1][
j]+
158 NET.Weights[il][
in][
j]*
NET.Outn[ilm1][j-1];
160 switch(
NET.T_func[il][in])
164 case 1:
NET.Outn[il][
in] =
a;
166 case 0:
NET.Outn[il][
in] = 0;
189 static int il,
in,
m, mp1;
191 dbl **rrout, **deriv1;
192 register dbl *prrout;
194 int nhid =
NET.Nneur[1];
204 rrout[0][0] = rrin[1];
209 rrout[0][0] = rrin[1];
210 rrout[0][1] = rrin[2];
215 rrout[0][0] = rrin[1];
216 rrout[0][1] = rrin[2];
217 rrout[0][2] = rrin[3];
222 prrout = &(rrout[0][mp1]);
223 prrin = &(rrin[mp1+1]);
224 for(i=mp1; i<=
NET.Nneur[0]; i+=4, prrout+=4, prrin+=4)
226 *(prrout-1) = *(prrin-1);
228 *(prrout+1)= *(prrin+1);
229 *(prrout+2) = *(prrin+2);
235 NET.Outn[1],nhid,nin);
240 for(il=2; il<
NET.Nlayer; il++)
244 NET.Outn[il],
NET.Nneur[il],
NET.Nneur[il-1]);
246 for(in=0; in<
NET.Nneur[
NET.Nlayer-1]; in++)
247 deriv1[
NET.Nlayer-1][in] = 1;
270 int nhid =
NET.Nneur[1];
272 int jpat,
j, il, ilm1,
m,
in, mp1;
274 dbl *pweights, *ptmp;
277 for(ipat=0; ipat<npat-1; ipat+=2)
280 NET.vWeights[1], 2, nhid, nin+1,
283 switch(
NET.T_func[1][0])
294 for(jpat=0; jpat<2; jpat++)
296 for(j=0; j<nhid; j++)
298 tmp[j+jpat*nhid] = 0;
304 for(jpat=0; jpat<2; jpat++)
306 for(in=0; in<nhid; in++)
308 NET.Outn[1][
in] = tmp[jpat*nhid+
in];
310 for(il=2; il<
NET.Nlayer; il++)
313 m =
NET.Nneur[ilm1]%4;
314 for(in=0; in<
NET.Nneur[il]; in++)
316 pweights = &(
NET.Weights[il][
in][0]);
320 for(j=1;j<=
m;j++,pweights++) a +=
321 (*pweights)*
NET.Outn[ilm1][j-1];
324 for(j=mp1; j<=
NET.Nneur[ilm1];
328 *(pweights+3)*
NET.Outn[ilm1][j+2]+
329 *(pweights+2)*
NET.Outn[ilm1][j+1]+
330 *(pweights+1)*
NET.Outn[ilm1][j]+
331 *(pweights )*
NET.Outn[ilm1][j-1];
333 switch(
NET.T_func[il][in])
337 case 1:
NET.Outn[il][
in] =
a;
339 case 0:
NET.Outn[il][
in] = 0;
343 if(il ==
NET.Nlayer-1)
345 for(in=0; in<
NET.Nneur[
NET.Nlayer-1]; in++)
347 rrans = (
dbl)
PAT.Rans[ifile][ipat+jpat][in];
348 err += (rrans-
NET.Outn[
NET.Nlayer-1][in])*
350 PAT.Pond[ifile][ipat+jpat];
358 for(ipat=ipat; ipat<npat; ipat++)
361 &(
PAT.vRin[ifile][ipat*(nin+1)]),tmp,
364 switch(
NET.T_func[1][0])
375 for(j=0; j<nhid; j++)
382 for(in=0; in<nhid; in++)
386 for(il=2; il<
NET.Nlayer; il++)
389 m =
NET.Nneur[ilm1]%4;
390 for(in=0; in<
NET.Nneur[il]; in++)
392 pweights = &(
NET.Weights[il][
in][0]);
396 for(j=1;j<=
m;j++,pweights++) a +=
397 (*pweights)*
NET.Outn[ilm1][j-1];
400 for(j=mp1; j<=
NET.Nneur[ilm1];
404 *(pweights+3)*
NET.Outn[ilm1][j+2]+
405 *(pweights+2)*
NET.Outn[ilm1][j+1]+
406 *(pweights+1)*
NET.Outn[ilm1][j]+
407 *(pweights )*
NET.Outn[ilm1][j-1];
409 switch(
NET.T_func[il][in])
413 case 1:
NET.Outn[il][
in] =
a;
415 case 0:
NET.Outn[il][
in] = 0;
419 if(il ==
NET.Nlayer-1)
421 for(in=0; in<
NET.Nneur[
NET.Nlayer-1]; in++)
423 rrans = (
dbl)
PAT.Rans[ifile][ipat][in];
424 err += (rrans-
NET.Outn[
NET.Nlayer-1][in])*
426 PAT.Pond[ifile][ipat];
452 int in,jn,ipat,ipati;
456 tmp = (
dbl *) malloc(2 *
NET.Nneur[1] *
sizeof(
dbl));
459 printf(
"not enough memory in MLP_Test\n");
461 for(ipat=0; ipat<
PAT.Npat[
ifile]; ipat++)
465 ipati = ExamplesIndex[ipat];
472 for(in=0; in<
NET.Nneur[
NET.Nlayer-1]; in++)
474 rrans = (
dbl)
PAT.Rans[ifile][ipati][in];
475 err += (rrans-
NET.Outn[
NET.Nlayer-1][in])*
477 PAT.Pond[ifile][ipati];
483 for(in=0; in<
NET.Nneur[
NET.Nlayer-1]; in++)
484 for(jn=0; jn<=
NET.Nneur[
NET.Nlayer-2]; jn++)
498 for(in=0; in<
NET.Nneur[
NET.Nlayer-1]; in++)
499 for(jn=0; jn<=
NET.Nneur[
NET.Nlayer-2]; jn++)
525 int il, in1,
in, itest2;
526 dbl deriv, deriv1, deriv2, deriv3, deriv4, pond;
528 register dbl a,
b,
dd, a1, a2, a3, a4;
529 dbl *pout, *pdelta, *pw1, *pw2, *pw3, *pw4;
532 if(
NET.Debug>=5) printf(
" Entry MLP_Stochastic\n");
533 weights =
NET.Weights;
544 for(ipat=0;ipat<
PAT.Npat[0];ipat++)
546 ii = ExamplesIndex[ipat];
547 pond =
PAT.Pond[0][
ii];
552 for(in=0; in<
NET.Nneur[
NET.Nlayer-1]; in++)
554 deriv =
NET.Deriv1[
NET.Nlayer-1][
in];
555 a = (
dbl)
PAT.Rans[0][ii][in];
556 b =
NET.Outn[
NET.Nlayer-1][in]-a;
558 NET.Delta[
NET.Nlayer-1][in] = b*deriv*pond*eta;
561 for(il=
NET.Nlayer-2; il>0; il--)
563 dd =
NET.Delta[il+1][0];
564 for(in=0; in<
NET.Nneur[il]-3; in+=4)
566 deriv1 =
NET.Deriv1[il][
in];
567 deriv2 =
NET.Deriv1[il][in+1];
568 deriv3 =
NET.Deriv1[il][in+2];
569 deriv4 =
NET.Deriv1[il][in+3];
570 itest2 = (
NET.Nneur[il+1]==1);
571 a1 = dd*weights[il+1][0][in+1];
572 a2 = dd*weights[il+1][0][in+2];
573 a3 = dd*weights[il+1][0][in+3];
574 a4 = dd*weights[il+1][0][in+4];
576 pdelta = &(
NET.Delta[il+1][1]);
577 for(in1=1; in1<
NET.Nneur[il+1];
580 a1 += *pdelta * weights[il+1][in1][in+1];
581 a2 += *pdelta * weights[il+1][in1][in+2];
582 a3 += *pdelta * weights[il+1][in1][in+3];
583 a4 += *pdelta * weights[il+1][in1][in+4];
585 L1:
NET.Delta[il][
in] = a1*deriv1;
586 NET.Delta[il][in+1] = a2*deriv2;
587 NET.Delta[il][in+2] = a3*deriv3;
588 NET.Delta[il][in+3] = a4*deriv4;
590 for(in=in; in<
NET.Nneur[il]; in++)
592 deriv =
NET.Deriv1[il][
in];
593 itest2 = (
NET.Nneur[il+1]==1);
594 a = dd*weights[il+1][0][in+1];
596 pdelta = &(
NET.Delta[il+1][1]);
597 for(in1=1; in1<
NET.Nneur[il+1];
601 weights[il+1][in1][in+1];
603 L2:
NET.Delta[il][
in] = a*deriv;
612 for(il=1; il<
NET.Nlayer; il++)
614 inm1 =
NET.Nneur[il-1];
615 for(in=0; in<
NET.Nneur[il]-3; in+=4)
617 a1 =
NET.Delta[il][
in];
618 a2 =
NET.Delta[il][in+1];
619 a3 =
NET.Delta[il][in+2];
620 a4 =
NET.Delta[il][in+3];
621 pout = &(
NET.Outn[il-1][0]);
622 weights[il][
in][0] += a1;
623 weights[il][in+1][0] += a2;
624 weights[il][in+2][0] += a3;
625 weights[il][in+3][0] += a4;
626 weights[il][
in][1] += a1* (*pout);
627 weights[il][in+1][1] += a2* (*pout);
628 weights[il][in+2][1] += a3* (*pout);
629 weights[il][in+3][1] += a4* (*pout);
631 pw1 = &(weights[il][
in][2]);
632 pw2 = &(weights[il][in+1][2]);
633 pw3 = &(weights[il][in+2][2]);
634 pw4 = &(weights[il][in+3][2]);
635 for(in1=2; in1<=inm1;
636 ++in1, ++pout, ++pw1, ++pw2,
645 for(in=in; in<
NET.Nneur[il]; in++)
647 a1 =
NET.Delta[il][
in];
648 pout = &(
NET.Outn[il-1][0]);
649 weights[il][
in][0] += a1;
650 weights[il][
in][1] += a1* (*pout);
652 pw1 = &(weights[il][
in][2]);
653 for(in1=2; in1<=inm1;
654 ++in1, ++pout, ++pw1)
663 for(il=1; il<
NET.Nlayer; il++)
665 for(in=0; in<
NET.Nneur[il]; in++)
668 a =
NET.Delta[il][
in];
672 b = a*
NET.Outn[il-1][0];
676 for(in1=2; in1<=
NET.Nneur[il-1]; in1++)
678 b = a*
NET.Outn[il-1][in1-1];
713 int Nweights, Nlinear, ipat, ierr;
719 Nweights =
NET.Nweights;
720 Nlinear =
NET.Nneur[
NET.Nlayer-2] + 1;
722 if(
NET.Debug>=5) printf(
" Entry MLP_Epoch\n");
732 if(iepoch==1 &&
LEARN.Meth==7)
746 for(ipat=0;ipat<nn;ipat++)
748 ierr =
MLP_Train(&ExamplesIndex[ipat],&err);
749 if(ierr!=0) printf(
"Epoch: ierr= %d\n",ierr);
754 for(ipat=0;ipat<
PAT.Npat[0];ipat++)
757 if(ierr!=0) printf(
"Epoch: ierr= %d\n",ierr);
771 if((iepoch-1)%
LEARN.Nreset==0)
790 if((iepoch-1)%
LEARN.Nreset==0)
799 beta =
LEARN.Norm/ONorm;
806 if((iepoch-1)%
LEARN.Nreset==0)
838 printf(
"Line search fail \n");
844 if((iepoch-1)%
LEARN.Nreset==0)
851 if(
NET.Debug>=5) printf(
"Before GetGammaDelta \n");
853 if(
NET.Debug>=5) printf(
"After GetGammaDelta \n");
855 if(
NET.Debug>=5) printf(
"After GetBFGSH \n");
865 if(
NET.Debug>=5) printf(
"After BFGSdir \n");
874 printf(
"Line search fail \n");
880 if(
NET.Debug>=5) printf(
" End MLP_Epoch\n");
903 if(*ipat<0)
return(2);
907 for(in=0; in<
NET.Nneur[
NET.Nlayer-1]; in++)
909 *err += ((
dbl)
PAT.Rans[0][*ipat][in]-
NET.Outn[
NET.Nlayer-1][in])
910 *((
dbl)
PAT.Rans[0][*ipat][in]-
NET.Outn[
NET.Nlayer-1][in])*
936 for(il=
NET.Nlayer-2; il>0; il--) {
938 for(in=0; in<
NET.Nneur[il]; in++) {
941 for(in1=0; in1<=
NET.Nneur[il-1]; in1++) {
943 + eps *
LEARN.Odw[il][
in][in1];
947 for(in1=0; in1<=
NET.Nneur[il-1]; in1++) {
976 for(il=
NET.Nlayer-1; il>0; il--) {
977 for(in1=0; in1<=
NET.Nneur[il-1]; in1++) {
980 for(in=0; in<
NET.Nneur[il]; in++) {
982 + epseta *
LEARN.Odw[il][
in][in1]);
1005 for(il=1; il<
NET.Nlayer; il++)
1006 for(in=0; in<
NET.Nneur[il]; in++)
1007 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1008 dd +=
LEARN.DeDw[il][in][jn]*
1009 LEARN.DeDw[il][in][jn];
1027 for(il=1; il<
NET.Nlayer; il++)
1028 for(in=0; in<
NET.Nneur[il]; in++)
1029 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1030 dd +=
LEARN.DeDw[il][in][jn]*
1031 LEARN.ODeDw[il][in][jn];
1047 for(il=1; il<
NET.Nlayer; il++)
1048 for(in=0; in<
NET.Nneur[il]; in++)
1049 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1050 LEARN.DeDw[il][in][jn] = 0;
1066 for(il=1; il<
NET.Nlayer; il++)
1067 for(in=0; in<
NET.Nneur[il]; in++)
1068 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1069 LEARN.DeDw[il][in][jn] /= (
dbl) Nexamples;
1084 for(il=1; il<
NET.Nlayer; il++)
1085 for(in=0; in<
NET.Nneur[il]; in++)
1086 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1087 LEARN.ODeDw[il][in][jn] =
LEARN.DeDw[il][in][jn];
1103 for(il=1; il<
NET.Nlayer; il++)
1104 for(in=0; in<
NET.Nneur[il]; in++)
1105 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1128 int il, in1,
in,
ii;
1131 dbl *pout, *pdedw, *pdelta;
1136 b = (
dbl)
PAT.Pond[0][ipat];
1137 for(in=0; in<
NET.Nneur[
NET.Nlayer-1]; in++)
1139 deriv =
NET.Deriv1[
NET.Nlayer-1][
in];
1141 (out[
in] - (
dbl) ans[in])*deriv*
b;
1144 for(il=
NET.Nlayer-2; il>0; il--)
1147 for(in=0; in<
NET.Nneur[il]; in++)
1149 deriv =
NET.Deriv1[il][
in];
1150 a =
NET.Delta[il+1][0] *
NET.Weights[il+1][0][in+1];
1151 pdelta = &(
NET.Delta[il+1][1]);
1152 for(in1=1; in1<
NET.Nneur[il+1]; in1++, pdelta++)
1154 a += *pdelta *
NET.Weights[il+1][in1][in+1];
1156 NET.Delta[il][
in] = a * deriv;
1160 for(il=1; il<
NET.Nlayer; il++)
1162 ii =
NET.Nneur[il-1];
1163 for(in=0; in<
NET.Nneur[il]; in++)
1165 a =
NET.Delta[il][
in];
1167 LEARN.DeDw[il][
in][1] += a *
NET.Outn[il-1][0];
1168 pout = &(
NET.Outn[il-1][1]);
1169 pdedw = &(
LEARN.DeDw[il][
in][2]);
1170 for(in1=1; in1<
ii; ++in1, ++pout, ++pdedw)
1172 (*pdedw) += a * (*pout);
1206 if(layer>
NLMAX)
return(1);
1209 NET.T_func[layer-1][neuron-1] = func;
1229 for(il=0; il<
NET.Nlayer; il++) {
1230 for(in=0; in<
NET.Nneur[il]; in++) {
1231 NET.T_func[il][
in] = 2;
1232 if(il==
NET.Nlayer-1)
NET.T_func[il][
in] = 1;
1250 for(il=1; il<
NET.Nlayer; il++)
1251 for(in=0; in<
NET.Nneur[il]; in++)
1252 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1253 dir[il][in][jn] = -
LEARN.DeDw[il][in][jn];
1272 for(il=1; il<
NET.Nlayer; il++)
1273 for(in=0; in<
NET.Nneur[il]; in++)
1274 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1276 dir[il][
in][jn] = -
LEARN.DeDw[il][
in][jn]+
1277 beta*dir[il][
in][jn];
1296 for(il=1; il<
NET.Nlayer; il++)
1297 for(in=0; in<
NET.Nneur[il]; in++)
1298 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1300 ddir +=
LEARN.DeDw[il][
in][jn]*dir[il][
in][jn];
1320 for(il=1; il<
NET.Nlayer; il++)
1321 for(in=0; in<
NET.Nneur[il]; in++)
1322 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1349 g = (
dbl*) malloc(
NET.Nweights*
sizeof(
dbl));
1350 s = (
dbl*) malloc(Nweights*
sizeof(
dbl));
1352 for(il=1; kk<Nweights; il++)
1353 for(in=0; in<
NET.Nneur[il]; in++)
1354 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1356 g[kk] =
LEARN.DeDw[il][
in][jn];
1359 for(i=0; i<Nweights; i++)
1362 for(j=0; j<Nweights; j++)
1364 s[
i] += BFGSH[
i][
j] * g[
j];
1369 for(il=1; kk<Nweights; il++)
1370 for(in=0; in<
NET.Nneur[il]; in++)
1371 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1373 dir[il][
in][jn] = -s[kk];
1394 for(i=0; i<Nweights; i++)
1395 for(j=0; j<Nweights; j++)
1398 if(i==j) BFGSH[
i][
j] = 1;
1423 typedef double dble;
1431 Hgamma = (dble *) malloc(Nweights*
sizeof(dble));
1432 tmp = (dble *) malloc(Nweights*
sizeof(dble));
1434 for(i=0; i<Nweights; i++)
1436 deltaTgamma += (dble) delta[i] * (dble) Gamma[
i];
1439 for(j=0; j<Nweights; j++)
1441 a += (dble) BFGSH[i][j] * (dble) Gamma[
j];
1442 b += (dble) Gamma[j] * (dble) BFGSH[
j][
i];
1446 factor += (dble) Gamma[i]*Hgamma[i];
1448 if(deltaTgamma == 0)
return 1;
1449 a = 1 / deltaTgamma;
1450 factor = 1 + factor*
a;
1452 for(i=0; i<Nweights; i++)
1454 b = (dble) delta[i];
1455 for(j=0; j<Nweights; j++)
1456 BFGSH[i][j] += (
dbl) (factor*b* (dble)
1457 delta[j]-(tmp[j]*b+Hgamma[i]*(dble)delta[
j]))*
a;
1479 dbl alpha1, alpha2, alpha3;
1480 dbl err1, err2, err3;
1482 int icount, il,
in, jn;
1489 w0 = (
dbl ***) malloc(
NET.Nlayer*
sizeof(
dbl**));
1490 for(il=1; il<
NET.Nlayer; il++)
1492 w0[il] = (
dbl **) malloc(
NET.Nneur[il]*
sizeof(
dbl*));
1493 for(in=0; in<
NET.Nneur[il]; in++)
1495 w0[il][
in] = (
dbl *) malloc((
NET.Nneur[il-1]+1)*
1497 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1499 w0[il][
in][jn] =
NET.Weights[il][
in][jn];
1510 if(
NET.Debug>=4) printf(
"err depart= %f\n",err1);
1517 if(alpha2 < 0.01) alpha2 = 0.01;
1518 if(alpha2 > 2.0) alpha2 = 2.0;
1522 if(
NET.Debug>=4) printf(
"alpha, err= %e %e\n",alpha2,err2);
1532 for(icount=1;icount<=100;icount++)
1534 alpha3 = alpha3*
tau;
1537 if(
NET.Debug>=4) printf(
"alpha, err= %e %e\n",alpha3,err3);
1539 if(err3>err2)
break;
1554 for(icount=1;icount<=100;icount++)
1556 alpha2 = alpha2/
tau;
1559 if(
NET.Debug>=4) printf(
"alpha, err= %e %e\n",alpha2,err2);
1561 if(err1>err2)
break;
1576 *alpmin = 0.5*(alpha1+alpha3-(err3-err1)/((err3-err2)/(alpha3-alpha2)
1577 -(err2-err1)/(alpha2-alpha1)));
1578 if(*alpmin>10000) *alpmin=10000;
1582 LastAlpha = *alpmin;
1585 for(il=1; il<
NET.Nlayer; il++)
1586 for(in=0; in<
NET.Nneur[il]; in++)
1587 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1588 LEARN.Odw[il][in][jn] =
NET.Weights[il][in][jn]
1591 for(il=1; il<
NET.Nlayer; il++)
1592 for(in=0; in<
NET.Nneur[il]; in++)
1594 for(il=1; il<
NET.Nlayer; il++)
1620 int icount, il,
in, jn;
1627 w0 = (
dbl ***) malloc(
NET.Nlayer*
sizeof(
dbl**));
1628 for(il=1; il<
NET.Nlayer; il++)
1630 w0[il] = (
dbl **) malloc(
NET.Nneur[il]*
sizeof(
dbl*));
1631 for(in=0; in<
NET.Nneur[il]; in++)
1633 w0[il][
in] = (
dbl *) malloc((
NET.Nneur[il-1]+1)*
1635 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1637 w0[il][
in][jn] =
NET.Weights[il][
in][jn];
1648 if(
NET.Debug>=4) printf(
"err depart= %f\n",err1);
1664 for(icount=1;icount<=100;icount++)
1666 alpha2 = alpha2/
tau;
1670 if(err1>err2)
break;
1685 for(il=1; il<
NET.Nlayer; il++)
1686 for(in=0; in<
NET.Nneur[il]; in++)
1687 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1688 LEARN.Odw[il][in][jn] =
NET.Weights[il][in][jn]
1691 for(il=1; il<
NET.Nlayer; il++)
1692 for(in=0; in<
NET.Nneur[il]; in++)
1694 for(il=1; il<
NET.Nlayer; il++)
1707 w0 = (
dbl ***) malloc(
NET.Nlayer*
sizeof(
dbl**));
1708 for(il=1; il<
NET.Nlayer; il++)
1710 w0[il] = (
dbl **) malloc(
NET.Nneur[il]*
sizeof(
dbl*));
1711 for(in=0; in<
NET.Nneur[il]; in++)
1713 w0[il][
in] = (
dbl *) malloc((
NET.Nneur[il-1]+1)*
1715 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1717 w0[il][
in][jn] =
NET.Weights[il][
in][jn];
1727 for(il=1; il<
NET.Nlayer; il++)
1728 for(in=0; in<
NET.Nneur[il]; in++)
1729 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1730 LEARN.Odw[il][in][jn] =
NET.Weights[il][in][jn]
1733 for(il=1; il<
NET.Nlayer; il++)
1734 for(in=0; in<
NET.Nneur[il]; in++)
1736 for(il=1; il<
NET.Nlayer; il++)
1757 register int il,
in,jn;
1759 for(il=1; il<
NET.Nlayer; il++)
1760 for(in=0; in<
NET.Nneur[il]; in++)
1761 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1762 NET.Weights[il][in][jn] = w0[il][in][jn]+
1763 alpha*dir[il][in][jn];
1782 dbl alpha1, alpha2, alpha3;
1783 dbl err1, err2, err3;
1785 int icount, il,
in, jn;
1792 printf(
" entry LineSearchHyb \n");
1799 w0 = (
dbl ***) malloc((
NET.Nlayer-1)*
sizeof(
dbl**));
1800 for(il=1; il<
NET.Nlayer-1; il++)
1802 w0[il] = (
dbl **) malloc(
NET.Nneur[il]*
sizeof(
dbl*));
1803 for(in=0; in<
NET.Nneur[il]; in++)
1805 w0[il][
in] = (
dbl *) malloc((
NET.Nneur[il-1]+1)*
1807 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1809 w0[il][
in][jn] =
NET.Weights[il][
in][jn];
1817 if(
NET.Debug>=4) printf(
"LinesearchHyb err depart= %f\n",err1);
1824 if(alpha2 < 0.01) alpha2 = 0.01;
1825 if(alpha2 > 2.0) alpha2 = 2.0;
1838 for(icount=1;icount<=100;icount++)
1840 alpha3 = alpha3*
tau;
1844 if(err3>err2)
break;
1859 for(icount=1;icount<=100;icount++)
1861 alpha2 = alpha2/
tau;
1865 if(err1>err2)
break;
1879 *alpmin = 0.5*(alpha1+alpha3-(err3-err1)/((err3-err2)/(alpha3-alpha2)
1880 -(err2-err1)/(alpha2-alpha1)));
1881 if(*alpmin>10000) *alpmin=10000;
1885 LastAlpha = *alpmin;
1888 for(il=1; il<
NET.Nlayer-1; il++)
1889 for(in=0; in<
NET.Nneur[il]; in++)
1890 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1891 LEARN.Odw[il][in][jn] =
NET.Weights[il][in][jn]
1894 for(il=1; il<
NET.Nlayer-1; il++)
1895 for(in=0; in<
NET.Nneur[il]; in++)
1897 for(il=1; il<
NET.Nlayer-1; il++)
1901 printf(
" exit LineSearchHyb \n");
1924 for(il=1; il<
NET.Nlayer-1; il++)
1925 for(in=0; in<
NET.Nneur[il]; in++)
1926 for(jn=0; jn<=
NET.Nneur[il-1]; jn++)
1928 NET.Weights[il][
in][jn] = w0[il][
in][jn]+
1929 alpha*dir[il][
in][jn];
1974 double err,lambda,lambda2;
1975 integer Nl,M,Nhr,khr,nrhs,iret,ierr;
1976 int il,
in, inl, ipat;
1984 lambda2 =
LEARN.Alambda;
1988 Nl =
NET.Nneur[
NET.Nlayer-2] + 1;
2002 for(ipat=0;ipat<
PAT.Npat[0];ipat++)
2017 dpat[ipat] = (
dbl)
PAT.Rans[0][ipat][0]*
sqrt(
PAT.Pond[0][ipat]);
2020 for(in=0;in<
NET.Nneur[il];in++)
2022 khr = M *(in+1) + ipat;
2023 HR[khr] =
NET.Outn[il][
in]*
2028 lambda =
sqrt(lambda2);
2029 for(ipat=0;ipat<=
NET.Nneur[il];ipat++)
2031 dpat[ipat+
PAT.Npat[0]] = 0;
2032 for(in=0;in<=
NET.Nneur[il];in++)
2034 khr = M *in + ipat +
PAT.Npat[0];
2036 if(in==ipat) HR[khr]=lambda;
2042 printf(
"entry ResLin, err=MLP_Test(0,0), err= %f\n",err);
2048 ierr =
dgels_(&Trans,&M,&Nl,&nrhs,HR,&M,dpat,&M,Work,
2050 if(iret != 0) printf(
"Warning from dgels: iret = %d\n",(
int)iret);
2051 if(ierr != 0) printf(
"Warning from dgels: ierr = %d\n",(
int)ierr);
2059 for (inl=0; inl<=
NET.Nneur[il-1];inl++)
2061 NET.Weights[il][0][inl] = dpat[inl];
2066 printf(
"ResLin, apres tlsfor, err= %f\n",err);
2109 index[
ii] = index[
i];
2131 return mini+(maxi-mini)*
random()/RAND_MAX;
2149 for(ilayer=1;ilayer<
NET.Nlayer;ilayer++)
2150 for(ineur=0;ineur<
NET.Nneur[ilayer];ineur++)
2151 for(i=0;i<=
NET.Nneur[ilayer-1];i++)
2152 NET.Weights[ilayer][ineur][i]=
2170 for(ilayer=1; ilayer<
NET.Nlayer; ilayer++)
2174 printf(
"Couche %d\n",ilayer);
2178 printf(
"Layer %d\n",ilayer);
2180 for(ineur=0; ineur<
NET.Nneur[ilayer]; ineur++)
2184 printf(
"Neurone %d",ineur);
2188 printf(
"Neuron %d",ineur);
2190 for(i=0; i<=
NET.Nneur[ilayer-1]; i++)
2193 (
double)
NET.Weights[ilayer][ineur][i]);
2228 int *inet,
int *ilearn,
int *iexamples)
2231 char otherfile[
CLEN];
2245 int nlayer, nneur[
NLMAX];
2247 printf(
"\nLoading file %s\n",filename);
2248 LVQpat=fopen(filename,
"r");
2249 if(LVQpat == 0)
return -1;
2253 while(fgets(s,
CLEN,LVQpat))
2259 printf(
"Number of neurons %s",s);
2261 sscanf(s,
"%s %s",cc,s2);
2263 if(ierr != 0)
return ierr;
2265 if(ierr != 0)
return ierr;
2269 sscanf(s,
"%s %d",cc,&l);
2273 printf(
"Number of patterns %d\n",np);
2275 else if(*(cc+1)==
'I')
2279 printf(
"Number of inputs %d\n",nin);
2281 else if(*(cc+1)==
'O' && *(cc+2)==
'U')
2285 printf(
"Number of outputs %d\n",nout);
2287 else if(*(cc+1)==
'O' && *(cc+2)==
'R')
2290 if(l==1) printf(
"Normalize inputs\n");
2293 else if(*(cc+1)==
'L')
2295 printf(
"NLAY datacard is no longer needed\n");
2297 else if(*(cc+1)==
'E')
2300 printf(
"Number of epochs %d\n",l);
2302 else if(*(cc+1)==
'R')
2306 "Reset to steepest descent every %d epochs\n",
2315 sscanf(s,
"%s %le",cc,&p);
2316 printf(
"Learning parameter %f\n",p);
2319 else if(*(s+1)==
'M')
2322 sscanf(s,
"%s %d",cc,&(
LEARN.Meth));
2323 printf(
"Learning method = ");
2326 case 1: printf(
"Stochastic Minimization\n");
2328 case 2: printf(
"Steepest descent with fixed step\n");
2330 case 3: printf(
"Steepest descent with line search\n");
break;
2331 case 4: printf(
"Polak-Ribiere Conjugate Gradients\n");
break;
2332 case 5: printf(
"Fletcher-Reeves Conjugate Gradients\n");
2334 case 6: printf(
"BFGS\n");
2336 case 7: printf(
"Hybrid BFGS-linear\n");
2338 default: printf(
"Error: unknown method\n");
break;
2342 else if(*(s+1)==
'T')
2344 sscanf(s,
"%s %lf",cc,&p);
2345 printf(
"Tau %f\n",p);
2348 else if(*(s+1)==
'A')
2350 sscanf(s,
"%s %lf",cc,&p);
2351 printf(
"Lambda %f\n",p);
2359 sscanf(s,
"%s %le",cc,&p);
2360 printf(
"Flat spot elimination parameter %f\n",p);
2363 else if(*(s+1)==
'I')
2365 sscanf(s,
"%s %s",cc,otherfile);
2366 ierr =
ReadPatterns(otherfile,ifile, inet, ilearn, iexamples);
2367 if(ierr != 0)
return ierr;
2372 sscanf(s,
"%s %le",cc,&p);
2373 printf(
"Momentum term %f\n",p);
2380 sscanf(s,
"%s %d",cc,&OutputWeights);
2381 if(OutputWeights == 0)
2383 printf(
"Never write file weights.out\n");
2385 else if(OutputWeights == -1)
2387 printf(
"Write weights to output file at the end\n");
2391 printf(
"Write weights to file every %d epochs\n",
2395 else if(*(s+3)==
'F')
2397 sscanf(s,
"%s %s",cc,cc2);
2398 if(*cc2==
'F' || *cc2==
'C')
2404 printf(
" *** Error while loading file %s at line %s :",
2406 printf(
" unknown language\n");
2411 printf(
" *** Error while loading file %s at line %s\n",
2417 sscanf(s,
"%s %d",cc,&(
NET.Rdwt));
2420 printf(
"Random weights \n");
2424 printf(
"Read weights from file weights.in\n");
2429 sscanf(s,
"%s %d",cc,&(
DIVERS.Stat));
2438 sscanf(s,
"%s %d",cc,&(
DIVERS.Ihess));
2444 sscanf(s,
"%s %le",cc,&p);
2446 printf(
"Learning parameter decay %f\n",
2447 (
double)
LEARN.Decay);
2451 sscanf(s,
"%s %d",cc,&(
DIVERS.Dbin));
2452 printf(
"Fill histogram every %d epochs\n",
DIVERS.Dbin);
2456 sscanf(s,
"%s %d",cc,&(
NET.Debug));
2457 printf(
"Debug mode %d\n",
NET.Debug);
2465 sscanf(s,
"%s %d",cc,&(
PAT.Iponde));
2469 ss = (
char**) malloc((npon+1)*
sizeof(
char*));
2470 for(i=0;i<=npon;i++)
2471 ss[i]=(
char*) malloc(40*
sizeof(
char));
2473 sscanf(ss[1],
"%d",&(
PAT.Iponde));
2476 sscanf(ss[i],
"%le",&(
PAT.Ponds[i-2]));
2494 if(nin==0)
return 2;
2495 if(nout==0)
return 3;
2503 if(ierr != 0)
return ierr;
2528 if(nout>nin) nmax=
nout;
2529 ss = (
char**) malloc((nmax+1)*
sizeof(
char*));
2530 if(ss == 0)
return -111;
2531 for(i=0;i<=nmax;i++)
2533 ss[
i]=(
char*) malloc(40*
sizeof(
char));
2534 if(ss[i] == 0)
return -111;
2543 sscanf(ss[i],
"%le",&p);
2553 sscanf(ss[i],
"%le",&p);
2575 printf(
"%d examples loaded \n\n",
PAT.Npat[ifile]);
2589 if (strtok(tmp,
" "))
2592 while (strtok(
NULL,
" ")) i++;
2606 strcpy(ss[0],strtok(tmp,
" "));
2608 strcpy(ss[i],strtok(
NULL,
" "));
2622 strcpy(ss[0],strtok(tmp,
" "));
2624 strcpy(ss[i],strtok(
NULL,
" "));
2641 if(LearnMemory==0)
return;
2643 for(il=0; il<
NET.Nlayer; il++)
2645 for(in=0; in<
NET.Nneur[il]; in++)
2652 if(BFGSMemory==0)
return;
2654 for(il=0; il<
NET.Nweights; il++)
2690 dir = (
dbl ***) malloc(
NET.Nlayer*
sizeof(
dbl**));
2691 if(dir == 0)
return -111;
2693 for(il=0; il<
NET.Nlayer; il++)
2695 dir[il] = (
dbl **) malloc(
NET.Nneur[il]*
sizeof(
dbl*));
2696 if(dir[il] == 0)
return -111;
2697 for(in=0; in<
NET.Nneur[il]; in++)
2702 dir[0][
in] = (
dbl *)
2703 malloc(101*
sizeof(
dbl));
2704 if(dir[0][in] == 0)
return -111;
2708 dir[il][
in] = (
dbl *)
2709 malloc((
NET.Nneur[il-1]+1)*
sizeof(
dbl));
2710 if(dir[il][in] == 0)
return -111;
2711 Nweights +=
NET.Nneur[il-1]+1;
2715 NET.Nweights = Nweights;
2717 if(BFGSMemory==0 &&
LEARN.Meth>= 6)
2720 Gamma = (
dbl*) malloc(Nweights*
sizeof(
dbl));
2721 delta = (
dbl*) malloc(Nweights*
sizeof(
dbl));
2722 BFGSH = (
dbl**) malloc(Nweights*
sizeof(
dbl*));
2723 if(Gamma == 0 || delta == 0 || BFGSH == 0)
2726 for(i=0; i<Nweights; i++)
2728 BFGSH[
i] = (
dbl*) malloc(Nweights*
sizeof(
dbl));
2729 if(BFGSH[i] == 0)
return -111;
2772 W=fopen(filename,
"w");
2774 fprintf(W,
" SUBROUTINE RNNFUN(rin,rout)\n");
2775 fprintf(W,
" DIMENSION RIN(%d)\n",
NET.Nneur[0]);
2776 fprintf(W,
" DIMENSION ROUT(%d)\n",
NET.Nneur[
NET.Nlayer-1]);
2779 for(in=0; in<
NET.Nneur[0]; in++)
2783 fprintf(W,
" OUT%d = RIN(%d)\n",in+1,in+1);
2787 fprintf(W,
" OUT%d = (RIN(%d)-%e)/%e\n",in+1,in+1,
2791 for(il=1; il<
NET.Nlayer-1; il++)
2794 fprintf(W,
"C layer %d\n",il+1);
2795 for(in=0; in<
NET.Nneur[il]; in++)
2797 fprintf(W,
" RIN%d = %e\n",in+1,
2798 (
double)
NET.Weights[il][in][0]);
2799 for(jn=1;jn<=
NET.Nneur[il-1]; jn++)
2800 fprintf(W,
" > +(%e) * OUT%d\n",
2801 (
double)
NET.Weights[il][in][jn],jn);
2804 for(in=0; in<
NET.Nneur[il]; in++)
2806 if(
NET.T_func[il][in]==0)
2808 fprintf(W,
" OUT%d = 0\n",in+1);
2810 else if(
NET.T_func[il][in]==1)
2812 fprintf(W,
" OUT%d = RIN%d\n",in+1,in+1);
2814 else if(
NET.T_func[il][in]==2)
2816 fprintf(W,
" OUT%d = SIGMOID(RIN%d)\n",
2823 fprintf(W,
"C layer %d\n",il+1);
2824 for(in=0; in<
NET.Nneur[il]; in++)
2826 fprintf(W,
" RIN%d = %e\n",in+1,
2827 (
double)
NET.Weights[il][in][0]);
2828 for(jn=1;jn<=
NET.Nneur[il-1]; jn++)
2829 fprintf(W,
" > +(%e) * OUT%d\n",
2830 (
double)
NET.Weights[il][in][jn],jn);
2833 for(in=0; in<
NET.Nneur[il]; in++)
2835 if(
NET.T_func[il][in]==0)
2837 fprintf(W,
" ROUT(%d) = 0\n",in+1);
2839 else if(
NET.T_func[il][in]==1)
2841 fprintf(W,
" ROUT(%d) = RIN%d\n",in+1,in+1);
2843 else if(
NET.T_func[il][in]==2)
2845 fprintf(W,
" ROUT(%d) = SIGMOID(RIN%d)\n",
2851 fprintf(W,
" END\n");
2852 fprintf(W,
" REAL FUNCTION SIGMOID(X)\n");
2853 fprintf(W,
" SIGMOID = 1./(1.+EXP(-X))\n");
2854 fprintf(W,
" END\n");
2881 W=fopen(filename,
"w");
2884 fprintf(W,
"double sigmoid(double x)\n");
2886 fprintf(W,
"return 1/(1+exp(-x));\n");
2888 fprintf(W,
"void rnnfun(double *rin,double *rout)\n");
2890 fprintf(W,
" double out1[%d];\n",
NET.Nneur[0]);
2891 fprintf(W,
" double out2[%d];\n",
NET.Nneur[1]);
2892 if(
NET.Nlayer>=3) fprintf(W,
" double out3[%d];\n",
NET.Nneur[2]);
2893 if(
NET.Nlayer>=4) fprintf(W,
" double out4[%d];\n",
NET.Nneur[3]);
2896 for(in=0; in<
NET.Nneur[0]; in++)
2900 fprintf(W,
" out1[%d] = rin[%d];\n",in,in);
2904 fprintf(W,
" out1[%d] = (rin[%d]-%e)/%e;\n",
2910 for(il=1; il<=
NET.Nlayer-1; il++)
2913 fprintf(W,
"/* layer %d */\n",il+1);
2914 for(in=0; in<
NET.Nneur[il]; in++)
2916 fprintf(W,
" out%d[%d] = %e\n",il+1,in,
2917 (
double)
NET.Weights[il][in][0]);
2918 for(jn=1;jn<=
NET.Nneur[il-1]; jn++)
2919 fprintf(W,
" +(%e) * out%d[%d]\n",
2920 (
double)
NET.Weights[il][in][jn],il,jn-1);
2924 for(in=0; in<
NET.Nneur[il]; in++)
2926 if(
NET.T_func[il][in]==0)
2928 fprintf(W,
" out%d[%d] = 0;\n",il+1,in);
2930 else if(
NET.T_func[il][in]==1)
2933 else if(
NET.T_func[il][in]==2)
2935 fprintf(W,
" out%d[%d] = sigmoid(out%d[%d]);\n",
2941 for(in=0; in<
NET.Nneur[il]; in++)
2943 fprintf(W,
" rout[%d] = out%d[%d];\n",in,il+1,in);
2975 W=fopen(filename,
"w");
2978 fprintf(W,
"# network structure ");
2979 for(ilayer=0; ilayer<
NET.Nlayer; ilayer++)
2981 fprintf(W,
"%d ",
NET.Nneur[ilayer]);
2984 fprintf(W,
"\n %d\n",iepoch);
2985 for(ilayer=1; ilayer<
NET.Nlayer; ilayer++)
2987 for(ineur=0; ineur<
NET.Nneur[ilayer]; ineur++)
2989 for(i=0; i<=
NET.Nneur[ilayer-1]; i++)
2991 fprintf(W,
" %1.15e\n",
2992 (
double)
NET.Weights[ilayer][ineur][i]);
3027 W=fopen(filename,
"r");
3034 sscanf(s,
" %d",iepoch);
3035 for(ilayer=1; ilayer<
NET.Nlayer; ilayer++)
3037 for(ineur=0; ineur<
NET.Nneur[ilayer]; ineur++)
3039 for(i=0; i<=
NET.Nneur[ilayer-1]; i++)
3041 fscanf(W,
" %le",&p);
3042 NET.Weights[ilayer][ineur][
i] = (
dbl) p;
3082 if(ifile>1 || ifile<0)
return(1);
3084 if(ExamplesMemory==0)
3091 if(
PAT.Pond == 0 ||
PAT.Rin == 0
3092 ||
PAT.Rans == 0 ||
PAT.vRin == 0)
return -111;
3097 if(iadd==0 && PatMemory[ifile]!=0)
3103 if(iadd==0 || PatMemory[ifile]==0)
3105 PatMemory[
ifile] = 1;
3107 if(
PAT.Pond[ifile] == 0)
return -111;
3108 for(j=0; j<npat; j++)
3109 PAT.Pond[ifile][j] = 1;
3112 if(
PAT.Rin[ifile] == 0)
return -111;
3114 if(
PAT.Rans[ifile] == 0)
return -111;
3118 if(
PAT.vRin[ifile] == 0)
return -111;
3120 for(j=0; j<npat; j++)
3125 for(j=0; j<npat; j++)
3128 if(
PAT.Rans[ifile][j] == 0)
return -111;
3134 ExamplesIndex = (
int *) malloc(npat*
sizeof(
int));
3135 if(ExamplesIndex == 0)
return -111;
3136 for(j=0; j<npat; j++) ExamplesIndex[j] = j;
3145 if(tmp == 0)
return -111;
3151 for(j=
PAT.Npat[ifile];j<ntot;j++)
3155 if(PatMemory[ifile]==1) free(
PAT.Pond[ifile]);
3172 if(tmp3 == 0)
return -111;
3174 for(j=0; j<
PAT.Npat[
ifile]*(nin+1); j++)
3178 if(PatMemory[ifile]==1) free(
PAT.vRin[ifile]);
3180 for(j=0; j<ntot; j++)
3187 if(tmp2 == 0)
return -111;
3192 for(j=
PAT.Npat[ifile];j<ntot;j++)
3195 if(tmp2[j] == 0)
return -111;
3197 if(PatMemory[ifile]==1) free(
PAT.Rans[ifile]);
3200 PatMemory[
ifile] = 1;
3205 free(ExamplesIndex);
3206 ExamplesIndex = (
int *) malloc(ntot*
sizeof(
int));
3207 if(ExamplesIndex == 0)
return -111;
3208 for(j=0; j<ntot; j++) ExamplesIndex[j] = j;
3234 if(ifile>1 || ifile<0)
return 1;
3236 if(PatMemory[ifile]==0)
return 2;
3238 free(
PAT.Pond[ifile]);
3242 free(
PAT.Rans[ifile][i]);
3244 free(
PAT.Rin[ifile]);
3245 free(
PAT.Rans[ifile]);
3246 free(
PAT.vRin[ifile]);
3247 PatMemory[
ifile] = 0;
3280 fmean = (
dbl*) malloc(Ninputs*
sizeof(
dbl));
3282 if(Nexamples<100) nmax=Nexamples;
3284 for(j=0;j<Ninputs;j++)
3287 for(ipat=0;ipat<nmax;ipat++)
3289 fmean[
j] += (
dbl) inputs[ipat][j];
3291 fmean[
j] = fmean[
j]/(
dbl) nmax;
3297 maximum[
j] = -99999;
3298 for(ipat=0;ipat<Nexamples;ipat++)
3300 mean[
j] += (
dbl) inputs[ipat][j];
3301 sigma[
j] += ((
dbl) inputs[ipat][j]-fmean[j])*
3302 ((
dbl) inputs[ipat][j]-fmean[j]);
3303 if((
dbl) inputs[ipat][
j] > maximum[
j])
3304 maximum[j]=(
dbl) inputs[ipat][
j];
3305 if((
dbl) inputs[ipat][
j] < minimum[
j])
3306 minimum[j]=(
dbl) inputs[ipat][
j];
3308 mean[
j] = mean[
j]/(
dbl) Nexamples;
3309 sigma[
j] =
sqrt(sigma[j]/ (
dbl) Nexamples -
3311 (mean[j]-fmean[j]));
3336 mean = (
dbl *) malloc(
NET.Nneur[0]*
sizeof(
dbl));
3337 sigma = (
dbl *) malloc(
NET.Nneur[0]*
sizeof(
dbl));
3338 minimum = (
dbl *) malloc(
NET.Nneur[0]*
sizeof(
dbl));
3339 maximum = (
dbl *) malloc(
NET.Nneur[0]*
sizeof(
dbl));
3341 if(mean == 0 || sigma == 0 || minimum == 0
3342 || maximum == 0)
return -111;
3345 mean,sigma,minimum,maximum);
3347 printf(
"\t mean \t\t RMS \t\t min \t\t max\n");
3348 for(j=0;j<
NET.Nneur[0];j++)
3350 printf(
"var%d \t %e \t %e \t %e \t %e\n",j+1,
3351 mean[j],sigma[j],minimum[j],maximum[j]);
3382 mean = (
dbl *) malloc(
NET.Nneur[0]*
sizeof(
dbl));
3383 sigma = (
dbl *) malloc(
NET.Nneur[0]*
sizeof(
dbl));
3386 minimum = (
dbl *) malloc(
NET.Nneur[0]*
sizeof(
dbl));
3387 maximum = (
dbl *) malloc(
NET.Nneur[0]*
sizeof(
dbl));
3389 if(mean == 0 || sigma == 0 || minimum == 0
3390 || maximum == 0 ||
STAT.mean == 0 ||
3391 STAT.sigma == 0)
return -111;
3394 mean,sigma,minimum,maximum);
3396 if(
NET.Debug>=1) printf(
"\t mean \t\t RMS \t\t min \t\t max\n");
3397 for(j=0;j<
NET.Nneur[0];j++)
3400 printf(
"var%d \t %e \t %e \t %e \t %e\n",j+1,
3401 mean[j],sigma[j],minimum[j],maximum[j]);
3405 STAT.sigma[
j] = sigma[
j];
3408 for(ipat=0;ipat<
PAT.Npat[0];ipat++)
3410 PAT.Rin[0][ipat][
j] =
3411 (
PAT.Rin[0][ipat][
j]-(float) mean[j])/
3414 for(ipat=0;ipat<
PAT.Npat[1];ipat++)
3416 PAT.Rin[1][ipat][
j] =
3417 (
PAT.Rin[1][ipat][
j]-(float) mean[j])/
3426 if(
NET.Debug>=1) printf(
"\n");
3452 NET.Nneur = (
int *) malloc(Nlayer*
sizeof(
int));
3453 if(
NET.Nneur == 0)
return -111;
3455 NET.T_func = (
int **) malloc(Nlayer*
sizeof(
int *));
3456 NET.Deriv1 = (
dbl **) malloc(Nlayer*
sizeof(
dbl *));
3457 NET.Inn = (
dbl **) malloc(Nlayer*
sizeof(
dbl *));
3458 NET.Outn = (
dbl **) malloc(Nlayer*
sizeof(
dbl *));
3459 NET.Delta = (
dbl **) malloc(Nlayer*
sizeof(
dbl *));
3460 if(
NET.T_func == 0 ||
NET.Deriv1 == 0
3461 ||
NET.Inn == 0 ||
NET.Outn == 0
3462 ||
NET.Delta == 0)
return -111;
3464 for(i=0; i<Nlayer; i++)
3466 NET.T_func[
i] = (
int *) malloc(Neurons[i]*
sizeof(
int));
3467 NET.Deriv1[
i] = (
dbl *) malloc(Neurons[i]*
sizeof(
dbl));
3468 NET.Inn[
i] = (
dbl *) malloc(Neurons[i]*
sizeof(
dbl));
3469 NET.Outn[
i] = (
dbl *) malloc(Neurons[i]*
sizeof(
dbl));
3470 NET.Delta[
i] = (
dbl *) malloc(Neurons[i]*
sizeof(
dbl));
3471 if(
NET.T_func[i] == 0 ||
NET.Deriv1[i] == 0
3472 ||
NET.Inn[i] == 0 ||
NET.Outn[i] == 0
3473 ||
NET.Delta[i] ==0 )
return -111;
3476 NET.Weights = (
dbl ***) malloc(Nlayer*
sizeof(
dbl **));
3477 NET.vWeights = (
dbl **) malloc(Nlayer*
sizeof(
dbl *));
3478 LEARN.Odw = (
dbl ***) malloc(Nlayer*
sizeof(
dbl **));
3479 LEARN.ODeDw = (
dbl ***) malloc(Nlayer*
sizeof(
dbl **));
3480 LEARN.DeDw = (
dbl ***) malloc(Nlayer*
sizeof(
dbl **));
3481 if(
NET.Weights == 0 ||
NET.vWeights == 0
3483 ||
LEARN.DeDw == 0)
return -111;
3485 for(i=1; i<Nlayer; i++)
3488 NET.vWeights[
i] = (
dbl *) malloc(k * Neurons[i] *
3490 NET.Weights[
i] = (
dbl **) malloc(Neurons[i]*
sizeof(
dbl *));
3491 LEARN.Odw[
i] = (
dbl **) malloc(Neurons[i]*
sizeof(
dbl *));
3492 LEARN.ODeDw[
i] = (
dbl **) malloc(Neurons[i]*
sizeof(
dbl *));
3493 LEARN.DeDw[
i] = (
dbl **) malloc(Neurons[i]*
sizeof(
dbl *));
3494 if(
NET.Weights[i] == 0 ||
NET.vWeights[i] == 0
3496 ||
LEARN.DeDw[i] == 0)
return -111;
3498 for(j=0; j<Neurons[
i]; j++)
3504 if(
LEARN.Odw[i][j] == 0
3505 ||
LEARN.ODeDw[i][j] == 0
3506 ||
LEARN.DeDw[i][j] == 0)
return -111;
3530 for(i=1; i<
NET.Nlayer; i++)
3532 for(j=0; j<
NET.Nneur[
i]; j++)
3535 free(
LEARN.Odw[i][j]);
3536 free(
LEARN.ODeDw[i][j]);
3537 free(
LEARN.DeDw[i][j]);
3539 free(
NET.vWeights[i]);
3540 free(
NET.Weights[i]);
3542 free(
LEARN.ODeDw[i]);
3543 free(
LEARN.DeDw[i]);
3552 for(i=0; i<
NET.Nlayer; i++)
3554 free(
NET.T_func[i]);
3555 free(
NET.Deriv1[i]);
3593 if(strlen(s)==0)
return -1;
3594 if(strlen(s)>1024)
return -2;
3597 if (strtok(tmp,
","))
3600 while (strtok(
NULL,
",")) i++;
3603 if(i >
NLMAX)
return -3;
3608 sscanf(strtok(tmp,
","),
"%d",&(Nneur[0]));
3609 for (i=1;i<*Nlayer;i++)
3610 sscanf(strtok(
NULL,
","),
"%d",&(Nneur[i]));
3639 if((*nl)>
NLMAX)
return(1);
3640 if((*nl)<2)
return(2);
3645 if(ierr != 0)
return ierr;
3648 NET.Nlayer = (int) *nl;
3651 for(il=0; il<
NET.Nlayer; il++) {
3652 NET.Nneur[il] = nn[il];
3681 register dbl a1, a2, a3, a4,
c, d;
3683 dbl *pM2 = &(M[m+1]);
3684 dbl *pM3 = &(M[2*(m+1)]);
3685 dbl *pM4 = &(M[3*(m+1)]);
3690 i+=4, pM1 += 3*mp1, pM2 += 3*mp1, pM3 += 3*mp1, pM4 += 3*mp1,
3697 pM1++; pM2++; pM3++; pM4++;
3698 for(j=0; j<m-1; j+=2, pM1+=2, pM2+=2, pM3+=2, pM4+=2)
3702 a1 = a1 + *pM1 * c + *(pM1+1) * d;
3703 a2 = a2 + *pM2 * c + *(pM2+1) * d;
3704 a3 = a3 + *pM3 * c + *(pM3+1) * d;
3705 a4 = a4 + *pM4 * c + *(pM4+1) * d;
3707 for(j=j; j<
m; j++, pM1++, pM2++, pM3++, pM4++)
3715 *pr = a1; *(pr+1) = a2; *(pr+2) = a3; *(pr+3) = a4;
3719 pM1 = &(M[i*(m+1)]);
3722 for(j=0; j<
m; j++, pM1++)
3724 a1 = a1 + *pM1 * v[
j];
3747 register dbl a1, a2, a3, a4,
c, d;
3750 dbl *pM3 = &(M[2*
m]);
3751 dbl *pM4 = &(M[3*
m]);
3756 i+=4, pM1 += 3*mp1, pM2 += 3*mp1, pM3 += 3*mp1, pM4 += 3*mp1,
3763 for(j=0; j<m-1; j+=2, pM1+=2, pM2+=2, pM3+=2, pM4+=2)
3767 a1 = a1 + *pM1 * c + *(pM1+1) * d;
3768 a2 = a2 + *pM2 * c + *(pM2+1) * d;
3769 a3 = a3 + *pM3 * c + *(pM3+1) * d;
3770 a4 = a4 + *pM4 * c + *(pM4+1) * d;
3772 for(j=j; j<
m; j++, pM1++, pM2++, pM3++, pM4++)
3780 *pr = a1; *(pr+1) = a2; *(pr+2) = a3; *(pr+3) = a4;
3786 for(j=0; j<
m; j++, pM1++)
3788 a1 = a1 + *pM1 * v[
j];
3814 int Ni,
int Nj,
int Nk,
int NaOffs,
int NbOffs)
3818 dbl s00,s01,s10,s11;
3820 dbl *pb0,*pb1,*pc0,*pc1;
3822 for (j=0; j<=Nj-2; j+=2)
3826 s00 = 0.0; s01 = 0.0; s10 = 0.0; s11 = 0.0;
3828 for (k=0,pb0=b+k+NbOffs*j,
3829 pb1=b+k+NbOffs*(j+1),
3838 s00 += (*pa0)*(*pb0);
3839 s01 += (*pa0)*(*pb1);
3840 s10 += (*pa1)*(*pb0);
3841 s11 += (*pa1)*(*pb1);
3843 *pc0 = s00; *(pc0+1) = s01; *pc1 = s10; *(pc1+1) = s11;
3845 for (j=j; j<Nj; j++)
3849 s00 = 0.0; s10 = 0.0;
3850 for (k=0,pb0=b+k+NbOffs*j,
3858 s00 += (*pa0)*(*pb0);
3859 s10 += (*pa1)*(*pb0);
3861 *pc0 = s00; *pc1 = s10;
dbl MLP_Epoch(int iepoch, dbl *alpmin, int *Ntest)
void getnLexemes(int n, char *s, char **ss)
dbl MLP_Test_MM(int ifile, dbl *tmp)
int ReadPatterns(char *filename, int ifile, int *inet, int *ilearn, int *iexamples)
int DecreaseSearch(dbl *alpmin, int *Ntest, dbl Err0)
int SaveWeights(char *filename, int iepoch)
void MLP_Out(type_pat *rrin, dbl *rrout)
int MLP_SetNet(int *nl, int *nn)
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
int MLP_PrCFun(char *filename)
int MLP_StatInputs(int Nexamples, int Ninputs, type_pat **inputs, dbl *mean, dbl *sigma, dbl *minimum, dbl *maximum)
void SetLambda(double Wmax)
int GetBFGSH(int Nweights)
int GetNetStructure(char *s, int *Nlayer, int *Nneur)
void MLP_Out_T(type_pat *rrin)
void MLP_vSigmoideDeriv(dbl *x, dbl *dy, int n)
int MLP_Train(int *ipat, dbl *err)
int ShuffleExamples(int n, int *index)
double MLP_Rand(dbl mini, dbl maxi)
int DeDwSum(type_pat *ans, dbl *out, int ipat)
int CountLexemes(char *s)
struct net_ net_ MLP_HIDDEN
int LoadWeights(char *filename, int *iepoch)
void MLP_LineHyb(dbl ***w0, dbl alpha)
int AllocPatterns(int ifile, int npat, int nin, int nout, int iadd)
int SetTransFunc(int layer, int neuron, int func)
void InitBFGSH(int Nweights)
void MLP_MM2rows(dbl *c, type_pat *a, dbl *b, int Ni, int Nj, int Nk, int NaOffs, int NbOffs)
void MLP_MatrixVector(dbl *M, type_pat *v, dbl *r, int n, int m)
void DeDwScale(int Nexamples)
void MLP_Line(dbl ***w0, dbl alpha)
void MLP_MatrixVectorBias(dbl *M, dbl *v, dbl *r, int n, int m)
void getLexemes(char *s, char **ss)
int dgels_(char *trans, integer *m, integer *n, integer *nrhs, doublereal *a, integer *lda, doublereal *b, integer *ldb, doublereal *work, integer *lwork, integer *info)
void MLP_vSigmoide(dbl *x, int n)
int LineSearch(dbl *alpmin, int *Ntest, dbl Err0)
std::vector< std::vector< double > > tmp
void BFGSdir(int Nweights)
void MLP_Out2(type_pat *rrin)
int FreePatterns(int ifile)
dbl MLP_Test(int ifile, int regul)
int MLP_PrFFun(char *filename)
int AllocNetwork(int Nlayer, int *Neurons)
int LineSearchHyb(dbl *alpmin, int *Ntest)