mlffNetworkWrite¶
Writes a trained neural network to an ASCII file for later retrieval using
mlffNetworkRead
.
Synopsis¶
mlffNetworkWrite (network, filename)
Required Arguments¶
- Imsls_d_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
file
is used,filename
is ignored.
Optional Arguments¶
t_print
, (Input)Prints status of file open, writing and closing.
Default: no printing.
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 fopen and fclose instead offopen
andfclose
.
Description¶
This function stores an Imsls_d_NN_Network data structure containing a
trained neural network into an ASCII file. If the optional argument file
is provided, mlffNetworkWrite
writes the data structure and returns
without closing the file. If this argument is not provided,
mlffNetworkWrite
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
.
from __future__ import print_function
from numpy import empty, double
from pyimsl.stat.dataSets import dataSets
from pyimsl.stat.mlffNetworkInit import mlffNetworkInit
from pyimsl.stat.mlffNetwork import mlffNetwork
from pyimsl.stat.mlffNetworkTrainer import mlffNetworkTrainer
from pyimsl.stat.mlffNetworkWrite import mlffNetworkWrite
from pyimsl.stat.randomSeedSet import randomSeedSet
filename = "NeuralNetworkEx1.txt"
n_patterns = 13
n_inputs = 4
n_continuous = 4
n_outputs = 1
nominalAtt = None
continuous = empty((13, 4), dtype=double)
y = 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]
networkStructure = mlffNetworkInit(n_inputs, n_outputs)
mlffNetwork(networkStructure, createHiddenLayer=4, linkAll=True)
randomSeedSet(5555)
forecasts = []
trainStats = mlffNetworkTrainer(networkStructure,
nominalAtt, continuous, y,
stageI={'nEpochs': 100, 'epochSize': 13},
forecasts=forecasts)
print("OBS X1 X2 X3 X4 Y FORECAST")
ss = 0
for i in range(0, n_patterns):
ss = ss + (y[i] - forecasts[i][0]) * (y[i] - forecasts[i][0])
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][0]))
print("Sum of Squared Residuals: %7.2f\n" % (ss))
mlffNetworkWrite(networkStructure, filename)
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.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 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
.
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.mlffNetwork import mlffNetwork
from pyimsl.stat.mlffNetworkInit import mlffNetworkInit
from pyimsl.stat.mlffNetworkFree import mlffNetworkFree
from pyimsl.stat.mlffNetworkTrainer import mlffNetworkTrainer
from pyimsl.stat.mlffNetworkWrite import mlffNetworkWrite
from pyimsl.stat.randomSeedSet import randomSeedSet
filename = "NeuralNetworkEx2.txt"
n_patterns = 13
n_inputs4 = 4
n_inputs3 = 3
n_cont4 = 4
n_cont3 = 3
n_outputs = 1
cont3 = zeros((13, 3), dtype=double)
cont4 = zeros((13, 4), dtype=double)
y = empty((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]
networkStructure = mlffNetworkInit(n_inputs4, n_outputs)
mlffNetwork(networkStructure, createHiddenLayer=4, linkAll=True)
randomSeedSet(5555)
forecasts4 = []
trainStats = mlffNetworkTrainer(networkStructure,
categoricalAtt, cont4, y,
maxStep=100.0,
stageI={'nEpochs': 50,
'epochSize': n_patterns},
forecasts=forecasts4)
# open filestream
file = fopen(filename, "w")
# Write the number of network being placed into this file
print("Writing network for model with 4 continuous attributes")
mlffNetworkWrite(networkStructure, None, file=file)
# Create second neural network
mlffNetworkFree(networkStructure)
networkStructure = mlffNetworkInit(n_inputs3, n_outputs)
mlffNetwork(networkStructure, createHiddenLayer=4, linkAll=True)
randomSeedSet(5555)
forecasts3 = []
trainStats = mlffNetworkTrainer(networkStructure,
categoricalAtt, cont3, y,
maxStep=100.0,
stageI={'nEpochs': 50,
'epochSize': n_patterns},
forecasts=forecasts3)
print("Writing network for model with 3 continuous attributes")
mlffNetworkWrite(networkStructure, None, file=file)
fclose(file)
print("File ", filename, " Closed.")
print("\nPrinting Forecasts for models with 3 and 4 continuous attributes:")
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] - forecasts4[i]) * (y[i] - forecasts4[i])
ss3 += (y[i] - forecasts3[i]) * (y[i] - forecasts3[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], forecasts3[i][0], forecasts4[i][0]))
print("Sum of Squared Residuals for X1-X3: %7.2f" % (ss3))
print("Sum of Squared Residuals for X1-X4: %7.2f" % (ss4))
Output¶
Writing network for model with 4 continuous attributes
Writing network for model with 3 continuous attributes
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.45 78.57
1 1.00 29.00 15.00 52.00 74.30 74.36 74.18
2 11.00 56.00 8.00 20.00 104.30 104.28 104.37
3 11.00 31.00 8.00 47.00 87.60 87.61 87.61
4 7.00 52.00 6.00 33.00 95.90 95.89 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.68 102.70
7 1.00 31.00 22.00 44.00 72.50 72.49 72.53
8 2.00 54.00 18.00 22.00 93.10 93.14 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.76 83.84
11 11.00 66.00 9.00 12.00 113.30 113.29 113.35
12 10.00 68.00 8.00 12.00 109.40 109.41 109.40
Sum of Squared Residuals for X1-X3: 0.01
Sum of Squared Residuals for X1-X4: 0.04
Fatal Errors¶
IMSLS_FILE_OPEN_FAILURE |
Unable to open file for writing network. |