CNL Stat : Data Mining : mlff_network_write
mlff_network_write
Writes a trained neural network to an ASCII file for later retrieval using imsls_f_mlff_network_read.
Synopsis
#include <imsls.h>
void imsls_f_mlff_network_write(Imsls_f_NN_Network *network, char *filename, ..., 0)
The type double function is imsls_d_mlff_network_write.
Required Arguments
Imsls_f_NN_Network *network (Input)
A trained neural network.
char *filename (Input)
The name of an ASCII file to be created. A complete or relative path can be used. If this file exists, it is replaced with a description of the neural network. If it does not exist, it is created. If the optional argument IMSLS_FILE is used, filename is ignored.
Synopsis with Optional Arguments
#include <imsls.h>
void imsls_f_mlff_network_write (Imsls_f_NN_Network *network, charImsls_f_NN_Network*filename,
IMSLS_PRINT,
IMSLS_FILE, FILE *file,
0)
Optional Arguments
IMSLS_PRINT, (Input)
Prints status of file open, writing and closing.
Default: no printing.
IMSLS_FILE, FILE *file (Input/Output)
A FILE pointer to a file opened for writing. This file is written but not closed. If this option is provided, filename is ignored. This option allows users to read additional user-defined data and multiple networks from the same file (see Example 2). To ensure this file is opened and closed with the same C run-time library used by the product, open and close this file using imsls_fopen and imsls_fclose instead of fopen and fclose.
Description
This function stores an Imsls_f_NN_Network data structure containing a trained neural network into an ASCII file. If the optional argument IMSLS_FILE is provided, imsls_f_mlff_network_write writes the data structure and returns without closing the file. If this argument is not provided, imsls_f_mlff_network_write creates a file using the path and name provided in filename, writes the data structure to that file, and then closes the file before returning.
Examples
Example 1
This example trains a network using the Draper-Smith data. These data consist of 13 patterns. The input attributes consist of four continuous attributes and one dependent variable. The network is stored into 73 lines of an ASCII file named NeuralNetworkEx1.txt.
 
#include <imsls.h>
#include <stdio.h>
int main(){
char *filename = "NeuralNetworkEx1.txt";
float *trainStats;
int i, j;
int n_patterns =13;
int n_inputs =4;
int n_nominal =0;
int n_continuous =4;
int n_outputs =1;
int *nominalAtt=NULL;
float ss;
float continuous[4*13], y[13];
float *draperSmithData;
float forecasts[13];
Imsls_f_NN_Network *networkStructure;
draperSmithData = imsls_f_data_sets(5,0);
for(i=0; i < n_patterns; i++){
y[i] = draperSmithData[5*i+4];
for(j=0; j<4; j++)
continuous[i*4+j] = draperSmithData[5*i+j];
}
networkStructure = imsls_f_mlff_network_init(n_inputs, n_outputs);
imsls_f_mlff_network(networkStructure,
IMSLS_CREATE_HIDDEN_LAYER, 4, IMSLS_LINK_ALL, 0);
imsls_random_seed_set(5555);
trainStats = imsls_f_mlff_network_trainer(networkStructure,
n_patterns, 0, n_continuous, nominalAtt,
continuous, y, IMSLS_STAGE_I, 100, 13,
IMSLS_FORECASTS_USER, forecasts, 0);
printf("OBS X1 X2 X3 X4 Y ");
printf("FORECAST\n");
ss = 0;
for(i=0; i<n_patterns; i++) {
ss += (y[i]-forecasts[i])*(y[i]-forecasts[i]);
printf("%2d %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",
i, continuous[i*4], continuous[i*4+1], continuous[i*4+2],
continuous[i*4+3], y[i], forecasts[i]);
}
printf("Sum of Squared Residuals: %7.2f\n\n", ss);
imsls_f_mlff_network_write(networkStructure, filename,
IMSLS_PRINT, 0);
}
Output
 
OBS X1 X2 X3 X4 Y FORECAST
0 7.00 26.00 6.00 60.00 78.50 78.50
1 1.00 29.00 15.00 52.00 74.30 74.30
2 11.00 56.00 8.00 20.00 104.30 104.22
3 11.00 31.00 8.00 47.00 87.60 87.60
4 7.00 52.00 6.00 33.00 95.90 95.78
5 11.00 55.00 9.00 22.00 109.20 109.34
6 3.00 71.00 17.00 6.00 102.70 102.55
7 1.00 31.00 22.00 44.00 72.50 72.50
8 2.00 54.00 18.00 22.00 93.10 93.24
9 21.00 47.00 4.00 26.00 115.90 116.05
10 1.00 40.00 23.00 34.00 83.80 83.80
11 11.00 66.00 9.00 12.00 113.30 112.30
12 10.00 68.00 8.00 12.00 109.40 110.33
Sum of Squared Residuals: 1.97
 
Opening NeuralNetworkEx1.txt for writing network data structure
Writing Neural Network... 73 Lines written to network file.
File NeuralNetworkEx1.txt closed.
Example 2
This example illustrates the use of the optional argument IMSLS_FILE to store multiple neural networks into one file. Two networks are trained using the Draper-Smith data. These data consist of 13 patterns each with four continuous attributes and one dependent variable. The first network is trained for forecasting the dependent variable using all four attributes, and the second is trained using only the first three. The networks are stored into 133 lines of an ASCII file named NeuralNetworkEx2.txt.
 
#include <imsls.h>
#include <stdio.h>
extern FILE* imsls_fopen(char* filename, char* mode);
extern void imsls_fclose(FILE* file);
int main(){
FILE *file;
char *filename = "NeuralNetworkEx2.txt";
float *trainStats;
int i, j;
int n_patterns =13;
int n_inputs4 =4;
int n_inputs3 =3;
int n_cont4 =4;
int n_cont3 =3;
int n_outputs =1;
int *categoricalAtt=NULL;
float ss3, ss4;
float cont4[4*13], cont3[3*13], y[13];
float *draperSmithData;
float forecasts3[13], forecasts4[13];
Imsls_f_NN_Network *networkStructure;
draperSmithData = imsls_f_data_sets(5,0);
 
for(i=0; i < n_patterns; i++){
y[i] = draperSmithData[5*i+4];
for(j=0; j<4; j++){
cont4[i*4+j] = draperSmithData[5*i+j];
if(j<3) cont3[i*3+j] = draperSmithData[5*i+j];
}
}
networkStructure = imsls_f_mlff_network_init(n_inputs4,
n_outputs);
imsls_f_mlff_network(networkStructure,
IMSLS_CREATE_HIDDEN_LAYER, 4,
IMSLS_LINK_ALL, 0);
imsls_random_seed_set(5555);
trainStats = imsls_f_mlff_network_trainer(networkStructure,
n_patterns, 0, n_cont4,
categoricalAtt, cont4, y,
IMSLS_MAX_STEP, 100.0,
IMSLS_STAGE_I, 50, n_patterns,
IMSLS_FORECASTS_USER, &forecasts4, 0);
/* open filestream */
file = imsls_fopen(filename, "w");
/* Write the number of network being placed into this file */
fprintf(file, "%d\n", 2);
printf("Writing network for model with 4 continuous attributes\n");
imsls_f_mlff_network_write(networkStructure, NULL,
IMSLS_PRINT, IMSLS_FILE, file, 0);
 
/* Create second neural network */
imsls_f_mlff_network_free(networkStructure);
imsls_free(trainStats);
networkStructure = imsls_f_mlff_network_init(n_inputs3,
n_outputs);
imsls_f_mlff_network(networkStructure,
IMSLS_CREATE_HIDDEN_LAYER, 4,
IMSLS_LINK_ALL, 0);
imsls_random_seed_set(5555);
trainStats = imsls_f_mlff_network_trainer(networkStructure,
n_patterns, 0, n_cont3,
categoricalAtt, cont3, y,
IMSLS_MAX_STEP, 100.0,
IMSLS_STAGE_I, 50, n_patterns,
IMSLS_FORECASTS_USER, &forecasts3, 0);
printf("Writing network for model with 3 continuous attributes\n");
imsls_f_mlff_network_write(networkStructure, NULL, IMSLS_PRINT,
IMSLS_FILE, file, 0);
imsls_fclose(file);
printf("File %s Closed.\n", filename);
printf("\nPrinting Forecasts for models with 3 and 4");
printf(" continuous attributes:\n");
printf("\n ");
printf("FORECAST FORECAST\n");
printf("OBS X1 X2 X3 X4 Y ");
printf("n_cont=3 n_cont=4\n");
ss4 = 0;
ss3 = 0;
for(i=0; i<n_patterns; i++) {
ss4 += (y[i]-forecasts4[i])*(y[i]-forecasts4[i]);
ss3 += (y[i]-forecasts3[i])*(y[i]-forecasts3[i]);
printf("%2d %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %9.2f\n",
i, cont4[i*4], cont4[i*4+1], cont4[i*4+2],
cont4[i*4+3], y[i], forecasts3[i], forecasts4[i]);
}
printf("Sum of Squared Residuals for X1-X3: %7.2f\n", ss3);
printf("Sum of Squared Residuals for X1-X4: %7.2f\n", ss4);
}
Output
 
Writing network for model with 4 continuous attributes
Writing Neural Network... 73 Lines written to network file.
File not closed.
Writing network for model with 3 continuous attributes
Writing Neural Network... 60 Lines written to network file.
File not closed.
File NeuralNetworkEx2.txt Closed.
 
Printing Forecasts for models with 3 and 4 continuous attributes:
 
FORECAST FORECAST
OBS X1 X2 X3 X4 Y n_cont=3 n_cont=4
0 7.00 26.00 6.00 60.00 78.50 78.97 78.50
1 1.00 29.00 15.00 52.00 74.30 73.62 74.30
2 11.00 56.00 8.00 20.00 104.30 104.61 104.30
3 11.00 31.00 8.00 47.00 87.60 87.32 87.60
4 7.00 52.00 6.00 33.00 95.90 94.88 95.90
5 11.00 55.00 9.00 22.00 109.20 108.82 109.20
6 3.00 71.00 17.00 6.00 102.70 102.69 102.70
7 1.00 31.00 22.00 44.00 72.50 72.97 72.50
8 2.00 54.00 18.00 22.00 93.10 94.28 93.10
9 21.00 47.00 4.00 26.00 115.90 115.04 115.90
10 1.00 40.00 23.00 34.00 83.80 83.64 83.80
11 11.00 66.00 9.00 12.00 113.30 114.45 113.30
12 10.00 68.00 8.00 12.00 109.40 109.20 109.40
Sum of Squared Residuals for X1-X3: 5.76
Sum of Squared Residuals for X1-X4: 0.00
Fatal Errors
IMSLS_FILE_OPEN_FAILURE
Unable to open file for writing network.