/*----------------------------------------------------------------------*/ /* TDNN menu selection routines */ /*----------------------------------------------------------------------*/ #include #include #include #include "menu_exp.h" #include "file_exp.h" #include "function_exp.h" #include "graph_exp.h" #include "net_exp.h" void LoadWeight(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { char *request; function = LoadWFile; request = "Weight file to load:"; PopupPrompt(w, request, weightfile); } void SaveWeight(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { char *request; function = SaveWFile; request = "Weight file to save:"; PopupPrompt(w, request, weightfile); } void LoadTrain(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { char *request; function = LoadTFile; request = "Training file to load:"; PopupPrompt(w, request, trainfile); } void LoadSamp(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { char *request; function = LoadSFile; request = "Sample file to load:"; PopupPrompt(w, request, samplefile); } void Train2Samp(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { int p, k, offset; offset = Recurrent ? Network.nodes[Network.layers - 1] : 0; if (no_patterns == 0) { sprintf(msgstring, "Warning: No pattern set loaded."); Wprintf(); } else { for (p = 0; p < no_patterns; p++) { for (k = 0; k < Network.nodes[0]; k++) sampleinput[p][k + offset] = traininput[p][k + offset]; for (k = 0; k < offset; k++) sampleinput[p][k] = 0.0; } no_samples = no_patterns; sprintf(msgstring, "Set of %d samples created.", no_samples); Wprintf(); } } void FlushTaps(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { int l, k, j; for (l = 0; l < Network.layers; l++) for (k = 0; k < Network.nodes[l]; k++) for (j = 0; j < MAXACCUM; j++) Network.state[l][j][k] = 0.0; (drawtype)(w, client_data, call_data); } void RunTrain(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { char *request, current[30]; function = SelTProg; request = "Enter program name:"; sprintf(current, "%s", TrainProg); PopupPrompt(w, request, current); } void RunSamp(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { char *request, current[30]; function = SelSProg; request = "Enter program name:"; sprintf(current, "%s", SampProg); PopupPrompt(w, request, current); } void TConst(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { char *request, current[20]; function = SelTConst; request = "Select training constant:"; sprintf(current, "%5.3f", eta); PopupPrompt(w, request, current); } void SetEpsilon(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { char *request, current[20]; function = SelEps; request = "Select tolerance:"; sprintf(current, "%7.5f", epsilon); PopupPrompt(w, request, current); } void SetRanMag(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { char *request, current[20]; function = SelRandom; request = "Select magnitude of random weights:"; sprintf(current, "%7.5f", RanMag); PopupPrompt(w, request, current); } void SetBounds(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { char *request, current[20]; function = SelBounds; request = "Select sample for start, end:"; sprintf(current, "%d, %d", starteps, endeps); PopupPrompt(w, request, current); } void SetTitle(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { char *request, current[100]; function = SelTitle; request = "Print desired title:"; sprintf(current, "%s", pixtitle); PopupPrompt(w, request, current); } void SetUpdate(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { char *request, current[20]; function = SelUdate; request = "No. steps between refreshes:"; sprintf(current, "%6d", update); PopupPrompt(w, request, current); } void SetLayers(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { char *request, current[20]; function = SelectLay; request = "Number of network layers:"; sprintf(current, "%6d", Network.layers); PopupPrompt(w, request, current); } void ShowActive(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { ToggleExcl(w, Stylemenu, 0, 3); XtRemoveCallback(pix, XtNexpose, drawtype, NULL); XtRemoveCallback(pix, XtNresize, drawtype, NULL); drawtype = DrawActive; XtAddCallback(pix, XtNexpose, drawtype, NULL); XtAddCallback(pix, XtNresize, drawtype, NULL); XClearWindow (dpy, XtWindow(pix)); (drawtype)(w, client_data, call_data); } void ShowNet(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { ToggleExcl(w, Stylemenu, 0, 3); XtRemoveCallback(pix, XtNexpose, drawtype, NULL); XtRemoveCallback(pix, XtNresize, drawtype, NULL); drawtype = DrawNet; XtAddCallback(pix, XtNexpose, drawtype, NULL); XtAddCallback(pix, XtNresize, drawtype, NULL); XClearWindow (dpy, XtWindow(pix)); (drawtype)(w, client_data, call_data); } void DisableDraw(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { ToggleExcl(w, Stylemenu, 0, 3); drawtype = DrawNone; } void SetRecur(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { Toggle(w); if (Recurrent == TRUE) { Recurrent = FALSE; strcpy(msgstring, "Network is now standard feedforward."); XClearWindow (dpy, XtWindow(pix)); } else { Recurrent = TRUE; endeps = min(endeps, 1); strcpy(msgstring, "Implementing recurrent network."); } if (Network.nodes[0] < Network.nodes[Network.layers - 1]) { Network.nodes[0] = Network.nodes[Network.layers - 1] + 1; randomizeweights(); strcpy(msgstring, "Adding nodes for recurrent network."); } Wprintf(); (drawtype)(w, client_data, call_data); } void LastPatMode(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { Toggle(w); if (LLastPat == TRUE) { LLastPat = FALSE; strcpy(msgstring, "Feedback on all patterns."); } else { LLastPat = TRUE; strcpy(msgstring, "Feedback on last pattern only."); } Wprintf(); } void SelfOrg(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { Toggle(w); if (LSelf == TRUE) { LSelf = FALSE; strcpy(msgstring, "Network learns by supervised training."); } else { LSelf = TRUE; strcpy(msgstring, "Network learns by self-organization."); } Wprintf(); } void BinHL(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { ToggleExcl(w, AStylemenu, 0, 4); Sigmoid = Binary; DSig = DExpon; /* true derivative of hard-limiter blows up at zero */ Limiter = Binary; strcpy(msgstring, "Binary activations enabled."); Wprintf(); } void BipHL(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { ToggleExcl(w, AStylemenu, 0, 4); Sigmoid = Signum; DSig = DExpon; /* true derivative of signum blows up at zero */ Limiter = Signum; strcpy(msgstring, "Binary activations enabled."); Wprintf(); } void BinAct(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { ToggleExcl(w, AStylemenu, 0, 4); Sigmoid = Expon; DSig = DTanh; Limiter = Binary; strcpy(msgstring, "Binary activations enabled."); Wprintf(); } void BipAct(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { ToggleExcl(w, AStylemenu, 0, 4); Sigmoid = Tanh; DSig = DTanh; Limiter = Signum; strcpy(msgstring, "Bipolar (+/-) output enabled."); Wprintf(); } void SinAct(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { ToggleExcl(w, AStylemenu, 0, 4); Sigmoid = Sin; DSig = DSin; Limiter = Signum; strcpy(msgstring, "Sinusoidal output enabled."); Wprintf(); } void SetStep(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { Toggle(w); if (Step == TRUE) { Step = FALSE; strcpy(msgstring, "Step mode disabled."); } else { Step = TRUE; strcpy(msgstring, "Step mode enabled."); } Wprintf(); } void ResetWeights(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { randomizeweights(); (drawtype)(w, client_data, call_data); } void NormWeights(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { float maxweight; int i, j, k, l; maxweight = 0; for (l = 0; l < Network.layers; l++) { for (k = 0; k < Network.nodes[l]; k++) { if (l > 0) maxweight = max(Network.threshold[l-1][k], maxweight); for (j = 0; j < Network.taps[l]; j++) { if (l < Network.layers - 1) for (i = 0; i < Network.nodes[l+1]; i++) maxweight = max(Network.weight[l][j][k][i], maxweight); } } } weightnorm = 1 / maxweight; sprintf(msgstring, "Weight colors decreased by %6.3f", maxweight); Wprintf(); (drawtype)(); } void NetTrain(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { trainsamp = FALSE; if ((no_patterns == 0) && (!LRunTrain)) { sprintf(msgstring, "Error: No training set loaded."); Wprintf(); } else { XClearWindow(dpy, XtWindow(graph)); (learntype)(); } } void NetRun(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { int s, top, k; trainsamp = TRUE; if (LRunSample) { LoadSamps(); top = no_samples; } else top = Step ? 1 : no_samples; if ((no_samples == 0) && (!LRunSample)) { sprintf(msgstring, "Error: No sample set loaded."); Wprintf(); } else for (s = 0; s < top; s++) { pattern++; if (pattern >= no_samples) pattern = 0; if (Recurrent) for (k = 0; k < Network.nodes[Network.layers - 1]; k++) sampleinput[pattern][k] = Network.state[Network.layers - 1] [0][k]; FeedForward(sampleinput); if (!LRunSample) { (drawtype)(w, client_data, call_data); XClearWindow(dpy, XtWindow(graph)); DrawGraph (w, client_data, call_data); } } if (LRunSample) { (drawtype)(w, client_data, call_data); XClearWindow(dpy, XtWindow(graph)); DrawGraph (w, client_data, call_data); } } void BreakTrain(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { Interrupt = TRUE; } void SetNodes(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { char *request, current[7*MAXLAYERS], temp[5]; int i; function = SelectNod; request = "Enter # nodes for each layer:"; strcpy (current, ""); for (i = 0; i < Network.layers; i++) { sprintf(temp, "%d ", Network.nodes[i]); strcat(current, temp); } PopupPrompt(w, request, current); } void SetTaps(w, client_data, call_data) Widget w; XtPointer client_data, call_data; { char *request, current[7*MAXLAYERS], temp[5]; int i; function = SelectTap; request = "Enter # taps for each layer:"; strcpy (current, ""); for (i = 0; i < Network.layers; i++) { sprintf(temp, "%d ", Network.taps[i]); strcat(current, temp); } PopupPrompt(w, request, current); } void SelectNod(valuestr) String valuestr; { char *token; int i; token = strtok(valuestr, ", "); sscanf(token, "%d", &Network.nodes[0]); for (i = 1; i < Network.layers; i++) { token = strtok(NULL, ", "); sscanf(token, "%d", &Network.nodes[i]); if (Network.nodes[i] > MAXNODES) { Network.nodes[i] = MAXNODES; sprintf(msgstring,"Unable to create enough nodes."); Wprintf(); } if (Network.nodes[i] < 1) { Network.nodes[i] = 1; sprintf(msgstring, "Invalid number; set to one."); Wprintf(); } } randomizeweights(); XClearWindow (dpy, XtWindow(pix)); (drawtype)(pix, NULL, NULL); XClearWindow (dpy, XtWindow(graph)); DrawGraph (graph, NULL, NULL); } void SelectTap(valuestr) String valuestr; { char *token; int i, accum; accum = 1; token = strtok(valuestr, ", "); sscanf(token, "%d", &Network.taps[0]); for (i = 1; i < Network.layers; i++) { token = strtok(NULL, ", "); sscanf(token, "%d", &Network.taps[i]); } for (i = Network.layers - 1; i <= 0; i--) { if (Network.taps[i] > MAXTAPS) { Network.taps[i] = MAXTAPS; sprintf(msgstring, "Unable to create enough taps."); Wprintf(); } if (Network.taps[i] < 1) { Network.taps[i] = 1; sprintf(msgstring, "Invalid number; set to one."); Wprintf(); } accum += Network.taps[i] - 1; if (accum > MAXACCUM) { Network.taps[i] = 1; sprintf(msgstring, "Number of virtual taps exceeds maximum."); Wprintf(); } } randomizeweights(); XClearWindow (dpy, XtWindow(pix)); (drawtype)(pix, NULL, NULL); XClearWindow (dpy, XtWindow(graph)); DrawGraph(graph, NULL, NULL); } void SelTProg(string) String string; { int value; strcpy(TrainProg, string); if (LRunSample || LRunTrain) { fprintf(stdout, "-1\n"); LRunSample = FALSE; LRunTrain = FALSE; } if ((value = talkto(string)) != 0) sprintf(msgstring, "Unable to pipe to %s.", string); else { sprintf(msgstring, "Accepting input from %s.", string); LRunTrain = TRUE; setbuf(stdin, buf); } Wprintf(); } void SelSProg(string) String string; { int value; strcpy(SampProg, string); if (LRunTrain || LRunSample) { fprintf(stdout, "-1\n"); LRunTrain = FALSE; LRunSample = FALSE; } if ((value = talkto(string)) != 0) sprintf(msgstring, "Unable to pipe to %s.", string); else { sprintf(msgstring, "Accepting input from %s.", string); LRunSample = TRUE; setbuf(stdin, buf); } Wprintf(); } void SelTitle(string) String string; { strcpy(pixtitle, string); XClearWindow (dpy, XtWindow(pix)); (drawtype)(pix, NULL, NULL); } void SaveWFile(filename) String filename; { FILE *fp; int i, j, k, l; strcpy(weightfile, filename); if ((fp = fopen(weightfile, "w")) != NULL) { fprintf(fp, "%s\n", pixtitle); fprintf(fp, "%1d\n", (int) Recurrent); fprintf(fp, "%d\n", Network.layers); for (l = 0; l < Network.layers; l++) { fprintf(fp, "%d\t", Network.nodes[l]); fprintf(fp, "%d\n", Network.taps[l]); } for (l = 0; l < Network.layers - 1; l++) for (k = 0; k < Network.nodes[l]; k++) for (j = 0; j < Network.taps[l]; j++) for (i = 0; i < Network.nodes[l+1]; i++) fprintf(fp, "%7.4f\t", Network.weight[l][j][k][i]); fprintf(fp, "\n"); for (l = 0; l < Network.layers - 1; l++) for (k = 0; k < Network.nodes[l+1]; k++) fprintf(fp, "%7.4f\t", Network.threshold[l][k]); fprintf(fp, "\n"); sprintf(msgstring, "File successfully saved."); Wprintf(); fclose(fp); } else { sprintf(msgstring,"Error: unable to open file %s.", weightfile); Wprintf(); } } void LoadWFile(filename) String filename; { FILE *fp; int i, j, k, l, rectype; Boolean lastrec; lastrec = Recurrent; strcpy(weightfile, filename); if ((fp = fopen(weightfile, "r")) != NULL) { fgets(pixtitle, 250, fp); fscanf(fp, "%d", &rectype); fscanf(fp, "%d", &Network.layers); for (l = 0; l < Network.layers; l++) { fscanf(fp, "%d", &Network.nodes[l]); fscanf(fp, "%d", &Network.taps[l]); } for (l = 0; l < Network.layers - 1; l++) for (k = 0; k < Network.nodes[l]; k++) for (j = 0; j < Network.taps[l]; j++) for (i = 0; i < Network.nodes[l+1]; i++) fscanf(fp, "%f", &Network.weight[l][j][k][i]); for (l = 0; l < Network.layers - 1; l++) for (k = 0; k < Network.nodes[l+1]; k++) fscanf(fp, "%f", &Network.threshold[l][k]); Recurrent = (rectype == 0) ? FALSE : TRUE; if (Recurrent != lastrec) Toggle(recnet); sprintf(msgstring, "File successfully loaded."); Wprintf(); fclose(fp); XClearWindow (dpy, XtWindow(pix)); (drawtype)(pix, NULL, NULL); XClearWindow (dpy, XtWindow(graph)); DrawGraph (graph, NULL, NULL); } else { sprintf(msgstring,"Error: unable to open file %s.", weightfile); Wprintf(); } } void LoadTFile(filename) String filename; { FILE *fp; int no_outputs, no_inputs, i, j, offset, lastpat; if (LRunTrain) { fprintf(stdout, "-1\n"); LRunTrain = FALSE; } offset = Recurrent ? Network.nodes[Network.layers - 1] : 0; strcpy(trainfile,filename); if ((fp = fopen(trainfile, "r")) != NULL) { fscanf(fp, "%d", &no_patterns); fscanf(fp, "%d", &no_inputs); fscanf(fp, "%d", &no_outputs); lastpat = no_patterns - 1; for (i = 0; i < no_patterns; i++) { for (j = 0; j < no_inputs; j++) fscanf(fp, "%f", &traininput[i][j + offset]); for (j = 0; j < no_outputs; j++) fscanf(fp, "%f", &traindesired[i][j]); for (j = 0; j < offset; j++) traininput[i][j] = traindesired[lastpat][j]; lastpat++; if (lastpat == no_patterns) lastpat = 0; } /* select a useful value of update */ update = no_patterns + 1; /* set all training patterns to zero */ for (i = no_patterns; i < MAXPATTERNS; i++) { for (j = 0; j < offset; j++) traininput[i][j] = 0.0; for (j = no_inputs + offset; j < MAXNODES; j++) traininput[i][j] = 0.0; for (j = no_outputs; j < MAXNODES; j++) traindesired[i][j] = 0.0; } trainsamp = FALSE; if ((no_outputs != Network.nodes[Network.layers - 1]) || ((no_inputs + offset) != Network.nodes[0])) sprintf(msgstring, "Mismatch warning: pattern file expects %d inputs, %d outputs.", (no_inputs + offset), no_outputs); else sprintf(msgstring, "File successfully loaded."); Wprintf(); fclose(fp); XClearWindow (dpy, XtWindow(graph)); DrawGraph (graph, NULL, NULL); } else { sprintf(msgstring,"Error: unable to open file %s.", trainfile); Wprintf(); } } void LoadSFile(filename) String filename; { FILE *fp; int no_inputs, i, j, offset; if (LRunSample) { fprintf(stdout, "-1\n"); LRunSample = FALSE; } offset = Recurrent ? Network.nodes[Network.layers - 1] : 0; strcpy(samplefile, filename); if ((fp = fopen(samplefile, "r")) != NULL) { fscanf(fp, "%d", &no_samples); fscanf(fp, "%d", &no_inputs); for (i = 0; i < no_samples; i++) { for (j = 0; j < no_inputs; j++) fscanf(fp, "%f", &sampleinput[i][j + offset]); for (j = 0; j < offset; j++) sampleinput[i][j] = 0.0; } update = no_patterns + 1; /* set all other sample patterns to zero */ for (i = no_samples; i < MAXPATTERNS; i++) { for (j = 0; j < offset; j++) sampleinput[i][j] = 0.0; for (j = no_inputs + offset; j < MAXNODES; j++) sampleinput[i][j] = 0.0; } trainsamp = TRUE; if ((no_inputs + offset) != Network.nodes[0]) sprintf(msgstring, "Mismatch warning: pattern file expects %d inputs.", (no_inputs + offset)); else sprintf(msgstring, "File successfully loaded."); Wprintf(); fclose(fp); XClearWindow (dpy, XtWindow(graph)); DrawGraph (graph, NULL, NULL); } else { sprintf(msgstring,"Error: unable to open file %s.", samplefile); Wprintf(); } } void SelTConst(valuestr) String valuestr; { sscanf(valuestr, "%f", &eta); } void SelEps(valuestr) String valuestr; { sscanf(valuestr, "%f", &epsilon); } void SelRandom(valuestr) String valuestr; { sscanf(valuestr, "%f", &RanMag); } void SelBounds(valuestr) String valuestr; { char *token; int i; token = strtok(valuestr, ", "); sscanf(token, "%d", &starteps); token = strtok(NULL, ", "); sscanf(token, "%d", &endeps); if (endeps >= starteps) sprintf(msgstring, "Tolerance = 1 to %d; Tolerance = %5.3f from %d.", starteps, epsilon, endeps); else { sprintf(msgstring, "Warning: backward bounds: reversing."); i = endeps; endeps = starteps; starteps = i; } Wprintf(); } void SelUdate(valuestr) String valuestr; { sscanf(valuestr, "%d", &update); } void SelectLay(valuestr) String valuestr; { int i, lastlayers; lastlayers = Network.layers; sscanf(valuestr, "%d", &Network.layers); if (Network.layers > MAXLAYERS) { Network.layers = MAXLAYERS; sprintf(msgstring,"Too many layers--truncating to maximum."); Wprintf(); } for (i = lastlayers; i < Network.layers; i++) { Network.nodes[i] = 1; Network.taps[i] = 1; } randomizeweights(); XClearWindow (dpy, XtWindow(pix)); (drawtype)(pix, NULL, NULL); XClearWindow (dpy, XtWindow(graph)); DrawGraph (graph, NULL, NULL); }