//Test_dlnn.mq4
#include <dlnn.mqh>
#define LayerCount 3 //êîëè÷åñòâî ñëî¸â (âêëþ÷àÿ âõîäíîé è âûõîäíîé ñëîè)
#define InputCount 2 //êîëè÷åñòâî íåéðîíîâ âî âõîäíîì ñëîå
#define OutputCount 1 //êîëè÷åñòâî íåéðîíîâ â âûõîäíîì ñëîå
#define PatternCount 4 //êîëè÷åñòâî ïðèìåðîâ
// Ñåòü äîëæíà íàó÷èòüñÿ âû÷èñëÿòü çíà÷åíèå
// ôóíêöèè XOR
// -----------------------------------
// âõîä 1 | âõîä 2 | âûõîä
// -----------------------------------
// 1 1 0
// 1 0 1
// 0 1 1
// 0 0 0
// -----------------------------------
//Ìàññèâ âõîäíûõ äàííûõ
double InMatrix[PatternCount][InputCount]={1, 1, 1, 0, 0, 1, 0, 0};
//Ìàññèâ âûõîäíûõ äàííûõ
double OutMatrix[PatternCount] = {0, 1, 1, 0};
//Êîëè÷åñòâî íåéðîíîâ â êàæäîì èç ñëî¸â
int LayersStruct[LayerCount] = {InputCount, 2, OutputCount};
int start()
{
//----
int r, nn, e, l, ns;
string sn1="\n", sn="\n";
string sf="net_for_example";
double q;
double Alpha = 1;
double Moment = 0.9;
double StartSpeed = 0.5;
double FinishSpeed = 0.1;
int EpohCount=100000;
double Kerror = 0.005;
//----
nn=-1;
nn=CreateNN(LayerCount, LayersStruct, Alpha, Moment, StartSpeed, FinishSpeed);
if(nn<0){Comment("Îøèáêà "+nn+" ïðè ñîçäàíèè ñåòè"); return(0);}
sn=sn+"Ñîçäàíà ñåòü ñ íîìåðîì "+nn+"\n";
r=TrainNN(nn, PatternCount, InMatrix, OutMatrix, EpohCount, Kerror);
if(r<0){Comment(sn,"Îøèáêà "+r+" ïðè îáó÷åíèè ñåòè"); return(0);}
sn=sn+"Ñåòü îáó÷åíà (ýïîõ="+GetEpohNN(nn)+"; îøèáêà="+DoubleToStr(GetErrorNN(nn),3)+")\nÐåçóëüòàò: ";
double OutputM[1];
double InputM[InputCount];
InputM[0] = 1;
InputM[1] = 1;// âõîäíûå äàííûå
RunNN(nn, InputM, OutputM);//Ðàññ÷¸ò
//Âûâîä ðåçóëüòàòà
sn=sn+DoubleToStr(InputM[0],0)+" "+DoubleToStr(InputM[1],0)+" = "+DoubleToStr(OutputM[0],2);
InputM[0] = 0;
InputM[1] = 1;// âõîäíûå äàííûå
RunNN(nn, InputM, OutputM);//Ðàññ÷¸ò
//Âûâîä ðåçóëüòàòà
sn=sn+" // "+DoubleToStr(InputM[0],0)+" "+DoubleToStr(InputM[1],0)+" = "+DoubleToStr(OutputM[0],2);
InputM[0] = 1;
InputM[1] = 0;// âõîäíûå äàííûå
RunNN(nn, InputM, OutputM);//Ðàññ÷¸ò
//Âûâîä ðåçóëüòàòà
sn=sn+" // "+DoubleToStr(InputM[0],0)+" "+DoubleToStr(InputM[1],0)+" = "+DoubleToStr(OutputM[0],2);
InputM[0] = 0;
InputM[1] = 0;// âõîäíûå äàííûå
RunNN(nn, InputM, OutputM);//Ðàññ÷¸ò
//Âûâîä ðåçóëüòàòà
sn=sn+" // "+DoubleToStr(InputM[0],0)+" "+DoubleToStr(InputM[1],0)+" = "+DoubleToStr(OutputM[0],2)+"\n";
//----
r=SaveNN(nn,sf);
if(r<0) {Comment(sn,"Îøèáêà "+r+" ïðè ñîõðàíåíèè ñåòè"); return(0);}
sn=sn+"Ñåòü ñîõðàíåíà â ôàéë "+sf+"\n";
r=DestroyNN(nn);
if(r<0) {Comment(sn,"Îøèáêà "+r+" ïðè óíè÷òîæåíèè ñåòè"); return(0);}
sn=sn+"Ñåòü óäàëåíà\n";
//----
nn=LoadNN(sf);
if(nn<0) {Comment(sn,"Îøèáêà "+nn+" ïðè çàãðóçêå ñåòè èç ôàéëà"); return(0);}
sn1=sn1+"Ñåòü çàãðóæåíà èç ôàéëà "+sf+". Íîìåð ñåòè "+nn+"\n";
l=GetLayersNN(nn);
sn1=sn1+"Ïàðàìåòðû ñåòè: Ñëîåâ= "+l+"; Íåéðîíîâ ïî ñëîÿì=";
for(int i=0; i<l; i++) sn1=sn1+" "+GetNeuronsNN(nn,i);
sn1=sn1+"\nÐåçóëüòàò: ";
InputM[0] = 1;
InputM[1] = 1;// âõîäíûå äàííûå
RunNN(nn, InputM, OutputM);//Ðàññ÷¸ò
//Âûâîä ðåçóëüòàòà
sn1=sn1+DoubleToStr(InputM[0],0)+" "+DoubleToStr(InputM[1],0)+" = "+DoubleToStr(OutputM[0],2);
InputM[0] = 0;
InputM[1] = 1;// âõîäíûå äàííûå
RunNN(nn, InputM, OutputM); //Ðàññ÷¸ò
//Âûâîä ðåçóëüòàòà
sn1=sn1+" // "+DoubleToStr(InputM[0],0)+" "+DoubleToStr(InputM[1],0)+" = "+DoubleToStr(OutputM[0],2);
InputM[0] = 1;
InputM[1] = 0;// âõîäíûå äàííûå
RunNN(nn, InputM, OutputM); //Ðàññ÷¸ò
//Âûâîä ðåçóëüòàòà
sn1=sn1+" // "+DoubleToStr(InputM[0],0)+" "+DoubleToStr(InputM[1],0)+" = "+DoubleToStr(OutputM[0],2);
InputM[0] = 0;
InputM[1] = 0;// âõîäíûå äàííûå
RunNN(nn, InputM, OutputM);//Ðàññ÷¸ò
//Âûâîä ðåçóëüòàòà
sn1=sn1+" // "+DoubleToStr(InputM[0],0)+" "+DoubleToStr(InputM[1],0)+" = "+DoubleToStr(OutputM[0],2)+"\n";
//----
r=DestroyNN(nn);
Comment(sn, sn1, "\nFinish...");
return(0);
}
Comments