📄 io.c
字号:
popfile();
return(i);
};
/* failure for some reason */
sprintf(outstr,"error while reading pattern %d",i+1); pg(stdout,outstr);
if (readerror == 1) pg(stdout,"\n");
else if (readerror == 2) pg(stdout,": unexpected end of file\n");
else if (readerror == 3) pg(stdout,": unexpected }\n");
if (readerror == 2) popfile();
if (i > 0)
{
pl = (PATLIST *) start->patstart[list];
for (j=1;j<=prevnpats + i - 1;j++) pl = pl->next;
pl->next = NULL;
pl = (PATLIST *) last->patstart[list];
for (j=1;j<=prevnpats + i - 1;j++) pl = pl->next;
pl->next = NULL;
};
return(i);
}
int pg(f,str) /* paging and making a copy function */
FILE *f;
char *str;
{
char *ch3,action,cr;
int i;
long clockbefore, clockafter;
int nbackspaces = 5;
if (f != stdout) {fprintf(f,"%s",str); return(0);};
clockbefore = clock();
action = 0;
ch3 = str;
while (*ch3 != '\0')
{
if (*ch3 == '\n')
{
putchar('\n');
if (copyflag == '+') putc('\n',copy);
lineno = lineno + 1;
if (pagesize > 0 && lineno > 0 && lineno % pagesize == 0)
{
printf("More?");
#ifdef HOTKEYS
action = getch();
#else
action = getchar();
if (action != '\n') cr = getchar();
#endif
for(i=1;i<=nbackspaces;i++) putchar(8);
for(i=1;i<=nbackspaces;i++) putchar(' ');
for(i=1;i<=nbackspaces;i++) putchar(8);
if (action == 'q') goto stoppaging;
else if (action == 'c') lineno = -MAXINT;
#ifdef UNIX
else if (action == '\n') lineno = lineno - 1;
#else
else if (action == 13) lineno = lineno - 1;
#endif
else if (action == 4) lineno = pagesize / 2;
else lineno = 0;
}
}
else {putchar(*ch3); if (copyflag == '+') putc(*ch3,copy); };
ch3++;
};
stoppaging:
clockafter = clock();
iotime = iotime + clockafter - clockbefore;
if (action == 'q') return(1); else return(0);
}
void printnetsize(f) /* the size of the network */
FILE *f;
{LAYER *p;
pg(f,"m ");
p = start;
if (stmsize > 0)
{
sprintf(outstr," %1d+%1d",p->unitcount-stmsize,stmsize);
pg(f,outstr);
p = p->next;
};
while (p != NULL)
{
sprintf(outstr," %1d",p->unitcount);
pg(f,outstr);
p = p->next;
};
if (ioconnects) pg(f," x");
if (pccnet) pg(f," p");
}
void listseeds(f)
FILE *f;
{
SEEDNODE *s;
s = seedstart->next;
pg(f,"s");
while (s != NULL)
{
sprintf(outstr," %1d",s->val); pg(f,outstr);
s = s->next;
};
}
int printoutunits(patnum,layer,list,error,tok,mok)
int patnum;
LAYER *layer;
int list;
REAL error;
char tok,mok;
{
short unitno, fmtbreaknum;
UNIT *u;
WTTYPE upper, middle, diff;
int i;
WTTYPE *target;
PATLIST *pl;
#ifdef INTEGER
float tempreal;
#endif
upper = scale(1.0) - toler;
middle = scale(0.5);
unitno = 0;
fmtbreaknum = 1;
/*
if (layer == last && patnum > 0)
{
pl = last->currentpat[list];
target = pl->pats;
u = (UNIT *) layer->units;
for (i=1;i<=last->unitcount;i++)
{
u->tj = *target++;
u = u->next;
};
};
*/
if (patnum) {sprintf(outstr,"%5d ",patnum); pg(stdout,outstr);}
else pg(stdout," ");
u = (UNIT *) layer->units;
while (u != NULL)
{
diff = u->tj - u->oj;
unitno = unitno + 1;
if (outformat == 'r' || outformat == 's')
{
sprintf(outstr,"%6.3f ",unscale(u->oj)); pg(stdout,outstr);
if (format[fmtbreaknum] == unitno)
{
if (pg(stdout,"\n ")) return(1);
if (fmtbreaknum < MAXFORMAT - 1) fmtbreaknum = fmtbreaknum + 1;
}
}
else if (outformat == 'e')
{
sprintf(outstr,"%12.4e ",unscale(u->oj)); pg(stdout,outstr);
if (format[fmtbreaknum] == unitno)
{
if (pg(stdout,"\n ")) return(1);
if (fmtbreaknum < MAXFORMAT - 1) fmtbreaknum = fmtbreaknum + 1;
}
}
else if (outformat == 'a')
{
if (diff < toler) pg(stdout,"c");
else if (u->oj > upper) pg(stdout,"1");
else if (u->oj < toler) pg(stdout,"0");
else if (u->oj > u->tj) pg(stdout,"^");
else pg(stdout,"v");
if (format[fmtbreaknum] == unitno)
{
pg(stdout," ");
if (fmtbreaknum < MAXFORMAT - 1) fmtbreaknum = fmtbreaknum + 1;
}
}
else
{
if (u->oj > upper) pg(stdout,"1");
else if (u->oj > middle) pg(stdout,"^");
else if (u->oj < toler) pg(stdout,"0");
else pg(stdout,"v");
if (format[fmtbreaknum] == unitno)
{
pg(stdout," ");
if (fmtbreaknum < MAXFORMAT - 1) fmtbreaknum = fmtbreaknum + 1;
}
}
u = u->next;
};
if (patnum > 0)
{
sprintf(outstr," e %5.3f",error); pg(stdout,outstr);
if (tok) pg(stdout," ok"); else pg(stdout," ");
if (mok) pg(stdout," ok");
if (pg(stdout,"\n")) return(1); else return(0);
};
if (pg(stdout,"\n")) return(1);
if (pg(stdout,"\n")) return(1); else return(0);
}
void saveweights() /* saves weights on the file weights */
{
UNIT *u, *bu;
LAYER *layer;
WTNODE *w;
WTTYPE wvalue, evalue, dvalue, svalue;
int wtsize, unitno, layerno, i;
short wtinuse;
FILE *wtfileptr;
wtsize = WTSIZE;
sprintf(wtfilename,"%s",wtfile);
wtfileptr = fopen(wtfilename,"wb");
if (wtfileptr == NULL)
{
sprintf(outstr,"cannot open: %s\n",wtfilename); pg(stdout,outstr);
return;
};
fprintf(wtfileptr,"%d%c",totaliter,wtformat);
layer = start;
fprintf(wtfileptr," m %1d",layer->unitcount);
layer = layer->next;
while (layer != NULL)
{
fprintf(wtfileptr," %1d",layer->unitcount);
layer = layer->next;
};
if (ioconnects) fprintf(wtfileptr," x");
fprintf(wtfileptr," aah%c",start->next->activation);
fprintf(wtfileptr," ao%c",last->activation);
fprintf(wtfileptr," bh %f",unscale(start->next->biasact));
fprintf(wtfileptr," bo %f",unscale(last->biasact));
fprintf(wtfileptr," Dh %f",unscale(start->next->D));
fprintf(wtfileptr," Do %f",unscale(last->D));
fprintf(wtfileptr," file = %s",datafile);
#ifdef DOSMOD /* I don't remember this */
fputc('\r',wtfileptr);
#endif
fputc('\n',wtfileptr);
layer = start->next;
while (layer != NULL)
{
u = (UNIT *) layer->units;
while (u != NULL)
{
w = (WTNODE *) u->wtlist;
while (w != NULL)
{
#ifdef SYMMETRIC
wvalue = *(w->weight);
evalue = *(w->eta);
dvalue = *(w->olddw);
svalue = *(w->slope);
wtinuse = w->inuse;
#else
wvalue = w->weight;
evalue = w->eta;
dvalue = w->olddw;
svalue = w->slope;
wtinuse = w->inuse;
#endif
if (wtformat == 'r' || wtformat == 'R')
{
fprintf(wtfileptr,"%14.6e",unscale(wvalue));
fprintf(wtfileptr,"%3d",w->inuse);
if (wtformat == 'R')
{
fprintf(wtfileptr," %14.6e",unscale(evalue));
fprintf(wtfileptr," %14.6e",unscale(dvalue));
fprintf(wtfileptr," %14.6e",unscale(svalue));
};
bu = w->backunit;
fprintf(wtfileptr," %1d ",bu->layernumber);
if (bu->unitnumber == 32767) fprintf(wtfileptr,"b to ");
else fprintf(wtfileptr,"%1d to ",bu->unitnumber);
fprintf(wtfileptr,"%1d %1d",u->layernumber,u->unitnumber);
#ifdef DOSMOD
fputc('\r',wtfileptr);
#endif
fputc('\n',wtfileptr);
}
w = w->next;
};
u = u->next;
};
layer = layer->next;
};
fflush(wtfileptr);
fclose(wtfileptr);
lastsave = totaliter;
}
void restoreweights() /* restore weights */
{
FILE *wtfileptr;
UNIT *u;
LAYER *layer;
WTNODE *w;
int ch2, fileformat, wtsize, numread, i, layerno, unitno;
WTTYPE wvalue, evalue, dvalue, svalue;
int wtinuse;
double temp;
unsigned char *cptr;
/*
if (numwtfiles == '+') sprintf(wtfilename,"%s.%1d",wtfile,wtfilecount);
else sprintf(wtfilename,"%s",wtfile);
*/
wtfileptr = fopen(wtfilename,"rb");
if (wtfileptr == NULL)
{
sprintf(outstr,"cannot open: %s\n",wtfilename);
pg(stdout,outstr);
return;
};
numread = fscanf(wtfileptr,"%d",&totaliter);
if (numread == EOF) goto unexpectedeof;
if (numread == 0) goto badread;
fileformat = fgetc(wtfileptr);
if (fileformat == EOF) goto unexpectedeof;
if (fileformat != wtformat) pg(stdout,"note: weight format mismatch\n");
wtsize = WTSIZE;
do ch2 = fgetc(wtfileptr); while (ch2 != '\n');
layer = start->next;
while (layer != NULL)
{
u = (UNIT *) layer->units;
while (u != NULL)
{
u->inuse = 1;
w = (WTNODE *) u->wtlist;
while (w != NULL)
{
if (fileformat == 'r' || fileformat == 'R')
{
numread = fscanf(wtfileptr,"%lf",&temp);
if (numread == EOF) goto unexpectedeof;
if (numread == 0) goto badread;
wvalue = scale((REAL) temp);
#ifdef ALTCODE
{
do ch2 = fgetc(wtfileptr); while (ch2 == ' ');
wtinuse = 1;
if (ch2 == '-')
{
wtinuse = -1;
ch2 = fgetc(wtfileptr);
};
if (ch2 == 0) wtinuse = 0;
else if (ch2 == '1') wtinuse = wtinuse;
else if (ch2 == '2') wtinuse = wtinuse * 2;
else pg(stdout,"bad weight in use flag\n");
}
#else
numread = fscanf(wtfileptr,"%d",&wtinuse);
if (numread == EOF) goto unexpectedeof;
if (numread == 0) goto badread;
#endif
if (fileformat == 'R')
{
numread = fscanf(wtfileptr,"%lf",&temp);
if (numread == EOF) goto unexpectedeof;
if (numread == 0) goto badread;
evalue = scale((REAL) temp);
numread = fscanf(wtfileptr,"%lf",&temp);
if (numread == EOF) goto unexpectedeof;
if (numread == 0) goto badread;
dvalue = scale((REAL) temp);
numread = fscanf(wtfileptr,"%lf",&temp);
if (numread == EOF) goto unexpectedeof;
if (numread == 0) goto badread;
svalue = scale((REAL) temp);
};
do ch2 = fgetc(wtfileptr); while (ch2 != '\n');
if (ch2 == '\n')
{
ch2 = fgetc(wtfileptr);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -