Build a neural network in 7 steps
In this tutorial, we build a neural network that approximates a function defined by a set of data points.
The data for this application can be obtained from the
To solve this application, follow the next steps:
- Create an approximation model.
- Configure data set.
- Set network architecture.
- Train neural network.
- Improve generalization performance.
- Test results.
- Deploy model.
1. Create approximation model
Open Neural Designer.
The start page is shown.
Click on the button New approximation model.
Save the project file in the same folder as the data file.
The main view of Neural Designer is shown.
2. Configure data set
In the Data set page, click on the Import data file button.
A file chooser dialogue will appear.
Select the file data.csv.
Click on the Finish button.
The program loads the data and sets the default configuration.
This data set has 2 variables and 50 samples.
We define the use of the variables:
-The first column represents the independent variable, or input variable, x.
-The second column represents the dependent variable, or target variable, y.
We leave that default values.
Once the data set is configured, we can run some dataset analysis related tasks.
For instance, in the Task Manager, click on Data set> Plot scatter chart.
The viewer window will appear with a chart of the data.
As we can see, the shape of this data is a sinus function.
You can check the data quality by running different data set tasks.
3. Set network architecture
Click on the Neural network tab to configure the
The next screenshot shows this page.
The Perceptron layers section is the most important one here.
By default, the number of layers is 2 (hidden layer and output layer).
By default, the hidden layer has 3 neurons with a hyperbolic tangent activation function.
As we have 1 target (y), the output layer must have 1 neuron. The default activation function here is the linear activation function.
We leave that default values.
4. Train neural network
The most important task is the so-called Perform training, which appears in the Task manager’s list of Training strategy tasks.
Running that task, the optimization algorithm
minimizes the loss index,
i.e., makes the neural network to fit the data set.
The following figure shows the results from the Perform training task in the Viewer window.
The figure above shows the training results.
It displays how both the training (blue) and selection (orange) errors decrease during the training process.
5. Improve generalization performance
The model selection tab shows the options that allow configuring the model selection process.
The next image shows the content of this page.
and growing inputs
are the default options for Neurons selection and Inputs selection, respectively.
Since there is only one input, Input selection will not be needed.
To perform the Neurons selection, double click on Task manager> Model selection> Perform neurons selection
We leave the default values.
The figure below shows the Viewer windows with the results of this task.
6. Test results
There are several tasks to test the model that has been previously trained. These tasks are grouped under
Testing analysis in the Task manager.
To test this model, double click on Task manager> Testing analysis> Perform linear regression analysis
The next figure shows the results of this task.
As we can observe in the table shown in the Viewer, the correlation is very close to 1, so we can say the model predicts well.
7. Deploy model
Once the model has been tested, it is ready to make predictions. The tasks for this purpose are found under
Model deployment in the Task manager.
Double click on Task manager> Model deployment> Plot directional output
to see the output variation as a function of a single input.
The picture below shows the results after performing this task.
Model deployment tasks also include the option of writing the expression of the predictive model, in addition to exporting this expression to Python or C.
The Python code corresponding to this model is presented below.
import numpy as np def scaling_layer(inputs): outputs = [None] * 1 outputs = inputs*0.3183101416+0 return outputs; def perceptron_layer_0(inputs): combinations = [None] * 3 combinations = 0.0544993 +1.84365*inputs combinations = -1.40552 -1.52111*inputs combinations = 1.47629 -1.67521*inputs activations = [None] * 3 activations = np.tanh(combinations) activations = np.tanh(combinations) activations = np.tanh(combinations) return activations; def perceptron_layer_1(inputs): combinations = [None] * 1 combinations = -0.094707 +1.89925*inputs +1.47562*inputs +1.54961*inputs activations = [None] * 1 activations = combinations return activations; def unscaling_layer(inputs): outputs = [None] * 1 outputs = inputs*1.270824432+0.2996354103 return outputs def bounding_layer(inputs): outputs = [None] * 1 outputs = inputs return outputs def neural_network(inputs): outputs = [None] * len(inputs) outputs = scaling_layer(inputs) outputs = perceptron_layer_0(outputs) outputs = perceptron_layer_1(outputs) outputs = unscaling_layer(outputs) outputs = bounding_layer(outputs) return outputs;
To learn more, see the next example: