Neural Networks
-----
 /
Neural Networks
*Features
*Examples
*Table of Contents
*Q&A
*Buy Online

Q&A




Q: How can I see the parameters of a neural network?
 
Download this answer as a Mathematica notebook »

The parameters, or synaptic weights as they are also called, are always stored in the first element of a network, as described in 3.2.3: Network Format. The meaning of the individual parameters is better explained in 13.1: Change the Parameter Values of an Existing Network. Most neural networks can be evaluated on a symbolic input vector; then the meaning of the parameters can be illustrated as in the example below.

Load the package.

NeuralNetworks

Generate a network for demonstration purposes.

fdfrwrd = InitializeFeedForwardNet[{{1, 1}}, {{2}}, {2}, 
RandomInitialization -> True, Neuron -> Tanh, 
RandomInitialization -> True]

FeedForwardNet[{{w1, w2}}, {Neuron -> Tanh, 
FixedParameters -> None, AccumulatedIterations -> 0, 
CreationDate -> {2003, 3, 4, 20, 53, 23}, 
OutputNonlinearity -> None, NumberOfInputs -> 2}]

The numerical values of the parameters are all placed in the first element of the network.

fdfrwrd[[1]]

{{{{0.866501, 0.119947},
{-0.811215, -0.984193}, {-0.143205, 0.968248}}, 
{{0.177434}, {0.826812}, {-0.367582}}}}

You can extract the first element and perform whatever process you want with Mathematica.

An easy way to identify the meaning of the individual parameter is obtained by applying the network to a symbolic input.

fdfrwrd[{x1, x2}][[1]]

-0.367582 + 0.826812 Tanh[0.968248 + 0.119947 x1 - 0.984193 x2] - 
0.177434 Tanh[0.143205 - 0.866501 x1 + 0.811215 x2]

If you need to implement the trained network in, for example, a C program, you might find the following command useful.

fdfrwrd[{x1, x2}][[1]] // CForm

-0.36758202492667336 + 0.8268122850621071*
    Tanh(0.9682476245672822 + 0.11994745409360652*x1 -
      0.9841928909337702*x2) -
    0.17743436966754977*Tanh(0.1432051850856072 -
      0.866500737817343*x1 + 0.8112145157992191*x2)
 

 
Q: Can I use several data sets simultaneously to train a neural network?
 
Download this answer as a Mathematica notebook »

For most types of neural networks you concatenate the data sets into one set before the training. This is shown in the first example below. For dynamic neural networks, neural ARX and neural AR, it is slightly more complicated because the individual data items are correlated. How you manage in this case is shown in the second example.

Nondynamic Neural Networks

The general data format is described in 3.2.1: Data Format.

Load the package.

NeuralNetworks

Generate a network for demonstration purposes.

NeuralNetworks

one2twodimfunc.dat

In this data file, the input and output matrices are assigned to the variable names x and y respectively. Once the data set has been loaded, you can query the data using Mathematica commands. To better understand the data format and variable name assignment, you may also want to open the data file itself.

Load a data file containing input data x and output data y.

one2twodimfunc.dat

Show the contents of the input and output matrices.

x

{{0.}, {0.5}, {1.}, {1.5}, {2.}, {2.5}, {3.}, {3.5}, {4.}, {4.5}, 
{5.}, {5.5}, {6.}, {6.5}, {7.}, {7.5}, {8.}, {8.5}, {9.}, {9.5}}

y

{{0., 1.}, {0.479426, 0.877583}, {0.841471, 0.540302}, 
{0.997495, 0.0707372}, {0.909297, -0.416147}, 
{0.598472, -0.801144}, {0.14112, -0.989992},
{-0.350783, -0.936457}, {-0.756802, -0.653644}, 
{-0.97753, -0.210796}, {-0.958924, 0.283662}, 
{-0.70554, 0.70867}, {-0.279415, 0.96017}, 
{0.21512, 0.976588}, {0.656987, 0.753902}, {0.938, 0.346635}, 
{0.989358, -0.1455}, {0.798487, -0.602012}, 
{0.412118, -0.91113}, {-0.0751511, -0.997172}}

Check the number of data items and the number of inputs and outputs for each.

Dimensions[x] Dimensions[y]

{20, 1}

{20, 2}

There are 20 data samples; the input has one dimension, and the output has two. Introduce a second data set.

x2 = {{3}, {4}}; 
y2 = {{2, 4}, {3, 5}};

The dimensionality of the input and output must be the same in the two data sets.

Dimensions[x2] 
Dimensions[y2]

{2, 1}

{2, 2}

A new, joined data set can now be constructed by concatenating the two input data sets, and similarly with the output data sets.

xnew = Join[x, x2]

{{0.}, {0.5}, {1.}, {1.5}, {2.}, {2.5}, {3.}, 
{3.5}, {4.}, {4.5}, {5.}, {5.5}, {6.}, {6.5}, 
{7.}, {7.5}, {8.}, {8.5}, {9.}, {9.5}, {3}, {4}}

ynew = Join[y, y2]

{{0., 1.}, {0.479426, 0.877583}, {0.841471, 0.540302}, 
{0.997495, 0.0707372}, {0.909297, -0.416147}, 
{0.598472, -0.801144}, {0.14112, -0.989992}, 
{-0.350783, -0.936457}, {-0.756802, -0.653644}, 
{-0.97753, -0.210796}, {-0.958924, 0.283662}, 
{-0.70554, 0.70867}, {-0.279415, 0.96017}, 
{0.21512, 0.976588}, {0.656987, 0.753902}, 
{0.938, 0.346635}, {0.989358, -0.1455}, 
{0.798487, -0.602012), {0.412118, -0.91113}, 
{-0.0751511, -0.997172}, {2, 4}, {3, 5}}

Now you can start training your network with the combined data set.


Dynamic Neural Networks

Data from dynamic systems are correlated, and if two data sets are concatenated in the way described in the previous example, then a transient is introduced in the data. This transient might be small and unimportant. There is, however, a way to avoid the transient totally. What you have to do is to build the regressor, described in 2.6: Dynamic Neural Networks, for the two data sets and then concatenate the result.

Load some packages.

<<NeuralNetworks`

<<Statistics`ContinuousDistributions`

<<Graphics`MultipleListPlot`

For demonstration purposes, generate a first data set.

Ndata = 30;
u1 = RandomArray[NormalDistribution[0, 3], {Ndata, 2}];
x = FoldList[Function[{xs, uin}, {(uin[[1]] + uin[[2]] +
0.6 * xs[[1]] + 0.8 * xs[[2]])/(1 + xs[[3]]^2), 0.7 * xs[[2]] +
uin[[2]], xs[[1]]}], {0, 0, 5}, Drop[u1, -1]];
y1 = x[[All, {1, 2}]];

Check dimensions of input and output, and the number of data for the first data set.

Dimensions[u1] 
Dimensions[y1]

{30, 2}

{30, 2}

Generate a second data set.

Ndata = 20;
u2 = RandomArray[NormalDistribution[0, 3], {Ndata, 2}];
x = FoldList[Function[{xs, uin}, {(uin[[1]] + uin[[2]] +
0.6 * xs[[1]] + 0.8 * xs[[2]])/(1 + xs[[3]]^2), 0.7 * xs[[2]] + uin[[2]], xs[[1]]}], {0, 0, 5}, Drop[u2, -1]];
y2 = x[[All, {1, 2}]];

Check dimensions of input and output, and the number of data for the second data set.

Dimensions[u2] 
Dimensions[y2]

{20, 2}

{20, 2}

Choose the indices indicating the regressor, as described in 2.6: Dynamic Neural Networks.

na = {2, 1}; 
nb = {1, 1}; 
nk = {1, 1};

With one data set you can train a neural ARX model in the following way.

{model1, fitrecord} = 
NeuralARXFit[u1, y1, {na, nb, nk}, FeedForwardNet, {2}, 2];

[Graphics:HTMLFiles/qa_29.gif]

With two data sets you have to do the following.

Generate the regressors and modified outputs of the two data sets.

{x1, yy1} = MakeRegressor[u1, y1, {na, nb, nk}];
{x2, yy2} = MakeRegressor[u2, y2, {na, nb, nk}];

Concatenate regressors and the modified outputs in the same way as in the previous example.

x = Join[x1, x2]; 
y = Join[yy1, yy2];

Initialize a neural ARX model of your choice (by indicating nil training iterations in NeuralARXFit).

{model1, fitrecord} = 
NeuralARXFit[u1, y1, {na, nb, nk}, FeedForwardNet, 
{2}, 0, CriterionPlot -> False];

Extract the feedforward or the RBF network in the first position of the initialized neural ARX model (as described in 8.1.1: Initializing and Training Dynamic Neural Networks).

ffrwd = model1[[1]]

FeedForwardNet[{{w1, w2}}, {AccumulatedIterations -> 0,
CreationDate -> {2003, 3, 4, 22, 30, 46}, 
Neuron -> Sigmoid, FixedParameters -> None, 
OutputNonlinearity -> None, NumberOfInputs -> 5}]

Train the feedforward or the RBF network.

{ffrwd, fitrecord} = NeuralFit[ffrwd, x, y, 5];

[Graphics:HTMLFiles/qa_36.gif]

Put back the trained network at the first position of the neural ARX model.

model1[[1]] = ffrwd;

You have now obtained a neural ARX model trained on both data sets. The neural ARX model can be used, for example, to simulate the behavior of one of the data sets.

NetComparePlot[u1, y1, model1]

[Graphics:HTMLFiles/qa_39.gif]

[Graphics:HTMLFiles/qa_40.gif]

- GraphicsArray -

   

Q: How can I implement neural networks in other programming languages?
 
Download this answer as a Mathematica notebook »

You can convert a neural network to program code in either C or Fortran using the Mathematica commands CForm or FortranForm. This code can then be inserted into external programs. Here is an illustration of how to do this conversion on a feedforward network.

Load the package.

<< NeuralNetworks`

Generate some data.

Ndata = 20; 
x = Table[10 N[{i/Ndata}], {i, 0, Ndata - 1}]; 
y = Sin[x];

Generate a network.

fdfrwrd = InitializeFeedForwardNet[x, y, {4}]

FeedForwardNet[{{w1, w2}}, 
{Neuron -> Sigmoid, FixedParameters -> None, AccumulatedIterations -> 0
CreationDate -> 2004, 2, 18, 17, 49, 30.9538441}, 
OutputNonlinearity -> None, NumberOfInputs -> 1}]

You can now obtain the program code by first producing a symbolic expression of the network and then using the Mathematica commands CForm or FortranForm.

Clear[xx]

The input to the network should be a list of symbols, one symbol for each input of the neural network. In this example there is only one input.

fdfrwrd[{xx}]

{16.0892 + 2.31601/(1 + e^(6.99244 - 1.82813 xx)) -
18.2488/(1 + e^(0.181153 - 1.73427 xx)) + 
11.3897/(1 + e^(-2.5769 + 1.35391 xx)) - 
27.7393/(1 + e^(-0.66048 + 1.40005 xx))}

You can then produce the C code using the CForm command.

CForm[fdfrwrd[{xx}]]

List(16.089224963527823 + 2.316011672835048/
     (1 + Power(E,6.99243912182352 - 1.8281339324526809*xx)) -
    18.248785203200498/(1 + Power(E,0.18115270188825505 - 1.7342669596549924*xx)) +
    11.389669725578603/(1 + Power(E,-2.5768975618284284 + 1.353905670349015*xx)) -
    27.739273072885055/(1 + Power(E,-0.6604795224465034 + 1.4000466309075252*xx)))

Similarly, to create Fortran code use the FortranForm command.

FortranForm[CForm[fdfrwrd[{xx}]]]

         CForm(List(16.089224963527823 +
      -    2.316011672835048/(1 + E**(6.99243912182352 - 1.8281339324526809*xx)) -
      -    18.248785203200498/(1 + E**(0.18115270188825505 - 1.7342669596549924*xx)) +
      -    11.389669725578603/(1 + E**(-2.5768975618284284 + 1.353905670349015*xx)) -
      -    27.739273072885055/(1 + E**(-0.6604795224465034 + 1.4000466309075252*xx))))