mlff_network_read

Retrieves a neural network from a file previously saved using imsls_f_mlff_network_write.

Synopsis

#include <imsls.h>

Imsls_f_NN_Network *imsls_f_mlff_network_read (char *filename, …, 0)

The type double function is imsls_d_mlff_network_read.

Required Arguments

char *filename (Input)
The name of an ASCII file containing a description of a trained neural network previously saved using imsls_f_mlff_network_write. A complete or relative path can be used. If the optional argument IMSLS_FILE is used, filename is ignored and the file is not closed before returning.

Return Value

A pointer to an Imsls_f_NN_Network data structure containing the neural network stored using imsls_f_mlff_network_write. This space can be released by using the imsls_free function.

Synopsis with Optional Arguments

#include <imsls.h>

Imsls_f_NN_Network *imsls_f_mlff_network_read (char *filename,

IMSLS_PRINT,

IMSLS_FILE, FILE *file,

0)

Optional Arguments

IMSLS_PRINT , (Input)
Prints status of file open, reading and closing.

Default: No printing.

IMSLS_FILE, FILE *file (Input)
A FILE pointer to a file opened for reading. This file is read 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 reads an Imsls_f_NN_Network data structure, a neural network previously stored as an ASCII file using imsls_f_mlff_network_write. If the optional argument IMSLS_FILE is provided, the data structure is read from that file stream and the file stream is not closed. If this argument is not provided, imsls_f_mlff_network_read opens a file using the path and name provided in filename, reads the data structure from that file, and then closes the file before returning.

Examples

Example 1

This example reads a network previously trained using the Draper-Smith data. These data consist of 13 patterns, each with four continuous attributes and one dependent variable. The network was stored into 73 lines of an ASCII file named NeuralNetworkEx1.txt using imsls_f_mlff_network_write (see Example 1 of imsls_f_mlff_network_write).

 

#include <imsls.h>

#include <stdio.h>

int main(){

char *filename = "NeuralNetworkEx1.txt";

int i, j;

int n_patterns =13;

int n_inputs =4;

int n_categorical =0;

int n_continuous =4;

int n_outputs =1;

int *categoricalAtt=NULL;

float ss;

float continuous[4*13], y[13], contAtt[4];

float *draperSmithData;

float forecast[1], forecasts[13];

Imsls_f_NN_Network *network;

draperSmithData = imsls_f_data_sets(5,0);

 

for(i=0; i < n_patterns; i++){

y[i] = draperSmithData[5*i+4];

for(j=0; j<n_continuous; j++)

continuous[i*n_continuous+j] = draperSmithData[5*i+j];

}

network = imsls_f_mlff_network_read(filename,

IMSLS_PRINT, 0);

 

for(i=0; i<n_patterns; i++){

for(j=0; j<n_inputs; j++)

contAtt[j] = continuous[i*n_continuous+j];

imsls_f_mlff_network_forecast(network,

n_categorical, n_continuous, categoricalAtt, contAtt,

IMSLS_RETURN_USER, forecast, 0);

forecasts[i] = forecast[0];

}

printf("\nOBS 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*n_continuous], continuous[i*n_continuous+1],

continuous[i*n_continuous+2], continuous[i*n_continuous+3],

y[i], forecasts[i]);

}

printf("Sum of Squared Residuals: %7.2f\n", ss);

 

}

Output

Notice that the forecasts produced using imsls_f_mlff_network_forecast are identical to the original forecasts in Example 1 of imsls_f_mlff_network_write.

 

 

Attempting to open NeuralNetworkEx1.txt for

reading network data structure

File NeuralNetworkEx1.txt Successfully Opened

File NeuralNetworkEx1.txt closed

 

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

Example 2

This example illustrates the use of the optional argument IMSLS_FILE to read multiple neural networks previously stored into a single file using imsls_f_mlff_network_write. Two networks were 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 to forecast the dependent variable using all 4 inputs and the second using only the first 3. The networks are read from an ASCII file previously created using imsls_f_mlff_network_write named NeuralNetworkEx2.txt (see Example 2 of imsls_f_mlff_network_write).

 

#include <imsls.h>

#include <stdio.h>

#include <stdlib.h>

extern FILE* imsls_fopen(char* filename, char* mode);

extern int imsls_fclose(FILE* file);

 

int main(){

FILE *file;

char *filename = "NeuralNetworkEx2.txt";

int i, j, n;

int n_patterns =13;

int n_inputs =4;

int n_categorical =0;

int n_continuous =4;

int n_outputs =1;

int n_networks =0;

int *categoricalAtt=NULL;

float ss3, ss4;

float cont4[4*13], y[13], contAtt4[4];

float cont3[3*13];

float *draperSmithData;

float forecast[1], forecasts[2*13];

Imsls_f_NN_Network **neural_network;

draperSmithData = imsls_f_data_sets(5,0);

 

for(i=0; i < n_patterns; i++){

y[i] = draperSmithData[5*i+4];

for(j=0; j<n_continuous; j++){

cont4[i*n_continuous+j] = draperSmithData[5*i+j];

if(j<3) cont3[i*3+j] = draperSmithData[5*i+j];

}

}

 

/* open filestream */

file = imsls_fopen(filename, "r");

printf("File %s Opened\n", filename);

/* Read the number of network being placed into this file */

fscanf(file, "%d", &n_networks);

printf("File contains %d neural networks\n", n_networks);

neural_network = (Imsls_f_NN_Network **) malloc(n_networks*

sizeof(Imsls_f_NN_Network *));

printf("Reading Networks and Preparing Forecasts...\n");

for(n=0; n<n_networks; n++){

neural_network[n] = imsls_f_mlff_network_read(NULL,

IMSLS_PRINT, IMSLS_FILE, file, 0);

n_continuous = neural_network[n]->layers[0].n_nodes;

printf("Preparing forecasts for network with");

printf(" %d continuous attributes\n", n_continuous);

for(i=0; i<n_patterns; i++){

for(j=0; j<4; j++) contAtt4[j] = cont4[i*4+j];

imsls_f_mlff_network_forecast(neural_network[n],

n_categorical, n_continuous,

categoricalAtt, contAtt4,

IMSLS_RETURN_USER, forecast, 0);

forecasts[n*n_patterns + i] = forecast[0];

}

}

 

imsls_fclose(file);

printf("File %s Closed.\n\n", filename);

printf(" ");

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]-forecasts[i])*(y[i]-forecasts[i]);

ss3 += (y[i]-forecasts[n_patterns+i])*

(y[i]-forecasts[n_patterns+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], forecasts[n_patterns+i], forecasts[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

Notice that the forecasts produced using imsls_f_mlff_network_forecast are identical to the original forecasts in Example 2 of imsls_f_mlff_network_write.

 

File NeuralNetworkEx2.txt Opened

File contains 2 neural networks

Reading Networks and Preparing Forecasts...

Network restored from file. File not closed.

Preparing forecasts for network with 4 continuous attributes

Network restored from file. File not closed.

Preparing forecasts for network with 3 continuous attributes

File NeuralNetworkEx2.txt Closed.

 

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 reading neural network.