mlffNetworkRead

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

Synopsis

mlffNetworkRead (filename)

Required Arguments

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

Return Value

An Imsls_d_NN_Network data structure containing the neural network stored using mlffNetworkWrite. This space can be released by using the free function.

Optional Arguments

t_print , (Input)

Prints status of file open, reading and closing.

Default: No printing.

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 fopen and fclose instead of fopen and fclose.

Description

This function reads an Imsls_d_NN_Network data structure, a neural network previously stored as an ASCII file using mlffNetworkWrite. If the optional argument 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, mlffNetworkRead 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 mlffNetworkWrite (see Example 1 of mlffNetworkWrite).

from __future__ import print_function
from numpy import empty, double
from pyimsl.stat.dataSets import dataSets
from pyimsl.stat.mlffNetworkForecast import mlffNetworkForecast
from pyimsl.stat.mlffNetworkRead import mlffNetworkRead

filename = "NeuralNetworkEx1.txt"
n_patterns = 13
n_inputs = 4
categoricalAtt = None
continuous = empty((13, 4), dtype=double)
contAtt = empty((4), dtype=double)
y = empty((13), dtype=double)
forecasts = empty((13), dtype=double)

draperSmithData = dataSets(5)

for i in range(0, n_patterns):
    y[i] = draperSmithData[i, 4]
    for j in range(0, 4):
        continuous[i, j] = draperSmithData[i, j]

network = mlffNetworkRead(filename, t_print=True)

for i in range(0, n_patterns):
    for j in range(0, n_inputs):
        contAtt[j] = continuous[i, j]
    forecast = mlffNetworkForecast(network, categoricalAtt, contAtt)
    forecasts[i] = forecast[0]

print("OBS    X1      X2      X3      X4      Y    FORECAST")

ss = 0
for i in range(0, n_patterns):
    ss = ss + (y[i] - forecasts[i]) * (y[i] - forecasts[i])
    print("%2d %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f" %
          (i, continuous[i, 0], continuous[i, 1], continuous[i, 2],
           continuous[i, 3], y[i], forecasts[i]))

print("Sum of Squared Residuals: %7.2f\n" % (ss))

Output

Notice that the forecasts produced using mlffNetworkForecast are identical to the original forecasts in Example 1 of mlffNetworkWrite.

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.30
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.90
5   11.00   55.00    9.00   22.00  109.20  109.20
6    3.00   71.00   17.00    6.00  102.70  102.70
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.10
9   21.00   47.00    4.00   26.00  115.90  115.90
10    1.00   40.00   23.00   34.00   83.80   83.80
11   11.00   66.00    9.00   12.00  113.30  113.30
12   10.00   68.00    8.00   12.00  109.40  109.40
Sum of Squared Residuals:    0.00

Example 2

This example illustrates the use of the optional argument file to read multiple neural networks previously stored into a single file using mlffNetworkWrite. 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 mlffNetworkWrite named NeuralNetworkEx2.txt (see Example 2 of mlffNetworkWrite).

from __future__ import print_function
from numpy import empty, double, zeros
from pyimsl.stat.dataSets import dataSets
from pyimsl.stat.fclose import fclose
from pyimsl.stat.fopen import fopen
from pyimsl.stat.mlffNetworkRead import mlffNetworkRead
from pyimsl.stat.mlffNetworkForecast import mlffNetworkForecast

filename = "NeuralNetworkEx2.txt"
n_patterns = 13
n_inputs4 = 4
n_inputs3 = 3
n_cont4 = 4
n_cont3 = 3
n_outputs = 1
n_networks = 2
cont3 = zeros((13, 3), dtype=double)
cont4 = zeros((13, 4), dtype=double)
contAtt3 = zeros((3), dtype=double)
contAtt4 = zeros((4), dtype=double)
y = empty((13), dtype=double)
forecasts = empty((2, 13), dtype=double)

n_outputs = 1
categoricalAtt = None
draperSmithData = dataSets(5)

for i in range(0, n_patterns):
    y[i] = draperSmithData[i, 4]
    for j in range(0, 4):
        cont4[i, j] = draperSmithData[i, j]
        if (j < 3):
            cont3[i, j] = draperSmithData[i, j]

# open filestream
file = fopen(filename, "r")
print("File", filename, " Opened")

n_networks = 2
neural_network = []
print("Reading Networks and Preparing Forecasts...")
for n in range(0, n_networks):
    network = mlffNetworkRead(None, file=file)
    neural_network.append(network)
    n_continuous = network[0].layers[0].n_nodes
    print("Preparing forecasts for network with ",
          n_continuous, "continuous attributes")

    for i in range(0, n_patterns):
        if (n_continuous == 3):
            for j in range(0, 3):
                contAtt3[j] = cont3[i, j]
            forecast = mlffNetworkForecast(neural_network[n],
                                           categoricalAtt, contAtt3)
        else:
            for j in range(0, 4):
                contAtt4[j] = cont4[i, j]
            forecast = mlffNetworkForecast(neural_network[n],
                                           categoricalAtt, contAtt4)
        forecasts[n, i] = forecast[0]

fclose(file)
print("File ", filename, "Closed.\n")
print("")
print("FORECAST  FORECAST")
print("OBS    X1      X2      X3      X4      Y   n_cont=3  n_cont=4")

ss4 = 0
ss3 = 0
for i in range(0, n_patterns):
    ss4 += (y[i] - forecasts[0, i]) * (y[i] - forecasts[0, i])
    ss3 += (y[i] - forecasts[1, i]) * (y[i] - forecasts[1, i])
    print("%2d %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %9.2f" %
          (i, cont4[i, 0], cont4[i, 1], cont4[i, 2],
           cont4[i, 3], y[i], forecasts[1, i], forecasts[0, i]))

print("Sum of Squared Residuals for X1-X3: %7.2f" % (ss3))
print("Sum of Squared Residuals for X1-X4: %7.2f" % (ss4))

Output

Notice that the forecasts produced using mlffNetworkForecast are identical to the original forecasts in Example 2 of mlffNetworkWrite.

File NeuralNetworkEx2.txt  Opened
Reading Networks and Preparing Forecasts...
Preparing forecasts for network with  4 continuous attributes
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.50     78.57
1    1.00   29.00   15.00   52.00   74.30   74.30     74.18
2   11.00   56.00    8.00   20.00  104.30  104.30    104.37
3   11.00   31.00    8.00   47.00   87.60   87.60     87.61
4    7.00   52.00    6.00   33.00   95.90   95.90     95.85
5   11.00   55.00    9.00   22.00  109.20  109.19    109.20
6    3.00   71.00   17.00    6.00  102.70  102.70    102.70
7    1.00   31.00   22.00   44.00   72.50   72.50     72.53
8    2.00   54.00   18.00   22.00   93.10   93.10     93.10
9   21.00   47.00    4.00   26.00  115.90  115.90    115.81
10    1.00   40.00   23.00   34.00   83.80   83.80     83.84
11   11.00   66.00    9.00   12.00  113.30  113.30    113.35
12   10.00   68.00    8.00   12.00  109.40  109.40    109.40
Sum of Squared Residuals for X1-X3:    0.00
Sum of Squared Residuals for X1-X4:    0.04

Fatal Errors

IMSLS_FILE_OPEN_FAILURE Unable to open file for reading neural network.