What? Machine learning is an interesting topic for mechanical engineers, but is often difficult to tackle due to a lack of infrastructure and necessary hardware. This library was built to offer the basic software components required for machine learning in artificial neural networks (as used by Google, etc.) directly in IEC. So using this library one can use methods of machine learning on any SPS or controller programmable with CODESYS without the need for additional hardware.
Who? Regardless of the fact that this library can be easily integrated into IEC on any controller, it still requires a basic understanding of machine learning. A book introducing to this topic in a basic and efficient manner is "Make Your Own Neural Network" by Tariq Rashid.
How? For more efficient use, some FB blocks that reflect possible applications are prepared in the library. In addition, a simpler and a more complex example are implemented and offered along with the library.
Why? The library is open source and can be changed or extended, whereas there is no guaranteed functionality or testing.
Important: The library uses "Dynamic Memory". This option must be activated in the properties of the application:
Examples:
• MLNeuronalNetworkExample.project: The idea is to link any 16 inputs with a light system (red, yellow, green). The FB VariableNeuronalNetwork16To3 is used accordingly. At the push of a button, training data is generated and the network is trained, which can then be queried with a specific input configuration.
• MLNeuronalNetworkComplexExample.project: The idea is to learn vibrational data (as amplitude-time data of length 2048) for anomaly detection. For this purpose, the FB AnomalieDetection2048 is used, which is configured in such a way that it first performs a fourier transformation on the data. Every cycle, training data of the simulated good state is generated, which is used to train the network. Every 10 runs, data of a simulated bad state is generated. In each case, the anomaly factor is displayed for comparison, along with a graphical output of the generated spectra.
1 – Anomaly detection
Idea (Unsupervised Learning): A machine often provides data that characterise its condition. Normally the available data will mostly characterise the "good state" of the machine. Anomaly detection aims to know the good state very well, so that if a bad/different state occurs a changed anomaly factor signals such a change.
The function modules prepared for this have the name "AnomalieDetection[...]" where [...] is replaced by 64, 1024, an so on. The nomenclature implies by the number how large the input data array is, i.e. how many entries the array must have used for training or query. During the query, the FBs provide a REAL value as an anomaly factor, which indicates the "anomaly" of the data.
Methods:
Initialize
Initialisation of a neural network for anomaly detection
Validity area Name Type Description
Input learningrate REAL Learning rate of the network
(default = 0.4, Recommendation: 0.3…0.5)
Input doFFT BOOL TRUE: Input values are Fourier transformed
Input doNormalize BOOL TRUE: Input values are normalised
Input doShuffleShift BOOL TRUE: The input values are shifted cyclically, randomly for training
Train
Train the neural network
Validity area Name Type Description
Input trainData ARRAY [1..uiSize] OF REAL
Note: uiSize = Number of data points (e.g. at AnomalieDetection2048 → uiSize = 2048)
Query
Query of the neural network for anomaly detection
Validity area Name Type Description
Input queryData ARRAY [1..uiSize] OF REAL
Note: uiSize = Number of data points (e.g. at AnomalieDetection2048 → uiSize = 2048)
File_Save_Weights
Saving the configuration of a neural network at a specific time
Validity area Name Type Description
Input Filename STRING(255) File name of the current configuration
File_Load_Weights
Loading a previously saved network configuration
Validity area Name Type Description
Input Filename STRING(255) File name of the configuration to be loaded
File_GetFileList
Return a list with all saved configuration files
Validity area Name Type Description
Input List POINTER TO STRING(255) STRING array into which the file list is written
File_Delete_Weights
Delete a specific configuration file
Validity area Name Type Description
Input Filename STRING(255) File name of the configuration to be deleted
File_Delete_WeightsALL
Delete all configuration files (no parameters)
2 – Variable neural network
Idea (Supervised Learning): In many situations, the constellation of sensor values is used to recognise, for example, whether a component is good or bad. This classification takes place manually or within rough limits. A "classical neural network" can be used to classify such data automatically. In this case, any input values are assigned to certain predefined output values and the network is trained. The goal is to learn an estimate for the output values for unknown output values.
The function modules prepared for this have the name "VariableNeuronalNetwork[...]" where [...] is replaced by 1024To10, 16To3, and so on. The nomenclature implies how large the input data array (e.g. 16) and how long the output data array (e.g. 3) is. When queried, the FBs return an output data array that is interpreted according to the use case.
Methods:
Initialize
Initialisation of a variable neural network
Validity area Name Type Description
Input learningrate REAL Learning rate of the network
(default = 0.4, Recommendation: 0.3…0.5)
Input hiddenLayers POINTER TO UINT Address of the array with the number of nodes of the respective layer
Input hiddenTypes POINTER TO UINT Address of the array with the type code of the respective layer
Input hiddenLayersCount UINT Number of hidden layers (without in- and outlayers)
Train
Training the variable neural network
Validity area Name Type Description
Input InputTrain POINTER TO REAL Address of the array with the train inputs
Input TargetsTrain POINTER TO REAL Address of the array with the train targets
Query
Querying the variable neural network
Validity area Name Type Description
Input InputQuery POINTER TO REAL Address of the array with the query inputs
Input OutputQuery POINTER TO REAL Address of an empty array with the query outputs
Further methods for saving and loading network configurations:
- File_Save_Weights
- File_Load_Weights
- File_Get_FileList
- File_Delete_Weights
- File_Delete_WeightsALL
For a description, see "1-Anomaly detection" above.