modelBaseStatefulRecurrentNeuralNet

Extends from BaseNeuralNet.

Information

Use this base class if you want to include a recurrent neural network in Modelica, which has been created with the flag stateful=True in TensorFlow. Please notice, that TFLite is not capable of handling the stateful states internally. Therefore, the neural network has to be created with access to all states as additional inputs and outputs. In this context, the inputs and outputs have to be additional access points to the neural networks. SMArtInt will handle the updates of the states by storing the values of the state outputs, and feeding them back into the state inputs. Therefore, for all states, matching in- and output have to exist. When creating the neural network the user has to take care of this. The stateful PI controller created in the script ExampleNeuralNets/PIController/PI.py gives an example of how to expose the states as in- an outputs.

Please extend this model in your own model. After extending you have to

  • give the path to the TFLite/ONNX model
  • specify the number of inputs
  • specify the sampling interval
  • create input and output connectors
  • connect input and output connectors to the single input and single output vector of the runInference submodel

Most likely, the stateful RNN will be trained for time discrete data and therefore it has to be called only at discrete time instances. The user has to provide the sampling interval for the discrete time instances. If continuous = false, the model will create events at each of the time instances and will only call the model at these instances. The many events have an impact on simulation performance. To increase performance the user can set continuous = true. In that case the model can be called for any times, as it is demanded by the solver. SMArtInt will internally call the neural network only at the time sampled time points. Hence, no events are created, but the inputs for the neural network have to be interpolated. Additionally, the solution accuracy for the states of the neural network and the impact of the interpolation of the inputs cannot be controlled by the solver directly. Only the impact on the states in the model can be evaluated. Therefore, this approach might create inaccurate solutions, especially if a multi-step solver like DASSL and Cvode are used with a high tolerance value. The user should compare the simulation results to those with continuous = false or to results with continuous = true and lower tolerance and/or a single step solver like (Radau).

For an example take a look at the model Tester.ExamplePI.TF_PI_Stateful_tflite which uses the block EvaluateStatefulRecurrentNeuralNet which extends from this base model

Parameters

TypeNameDefaultDescription
IntegernInputElements (from BaseNeuralNet)product(inputSizes)
IntegernOutputElements (from BaseNeuralNet)product(outputSizes)
Selected Model
StringpathToAIModel (from BaseNeuralNet)""Choose path to AI model
Tensor sizing
IntegerinputDimensions (from BaseNeuralNet)1Number of input dimension
IntegerinputSizes (from BaseNeuralNet){1}Vector with size of tensor in each dimension
IntegeroutputDimensions (from BaseNeuralNet)1Number of output dimension
IntegeroutputSizes (from BaseNeuralNet){1}Vector with size of tensor in each dimension
RNN Timing Settings
Booleanstateful (from BaseNeuralNet)falseActivate state handling for RNN with state in-/outputs
RealsamplePeriod (from BaseNeuralNet)0Fixed sample period for RNNs
Booleancontinuoustrue=true: model operates continuously; input values are delayed
Advanced › Performance
IntegernumberOfThreads (from BaseNeuralNet)1Number of threads used for inference (0: Number of CPU cores)
Advanced › TFLite
BooleanuseFlexOps (from BaseNeuralNet)falseActivate to allow Tensorflow Flex Ops for tflite models
Advanced › ONNX
BooleanuseGPU (from BaseNeuralNet)falseActivate to use a compatible CUDA GPU for inference
IntegergpuDeviceID (from BaseNeuralNet)0CUDA device ID
SMArtInt.Internal.Types.ExecutionModeexecutionMode (from BaseNeuralNet)SMArtInt.Internal.Types.ExecutionMode.SequentialExecution mode for run inference
Input/Output Sizing
IntegernumberOfInputs1Number of input values
IntegernumberOfOutputs1Number of output values
IntegerbatchSize1Number of parallel batched simulations

Components

TypeNameDefaultDescription
Internal.Utilities.Dependencies.OnnxGpuDependenciesonnxGpuDependencies (from BaseNeuralNet)
Internal.Utilities.Dependencies.TFFlexOpsDependenciestFFlexOpsDependencies (from BaseNeuralNet)
Internal.Utilities.RunInferenceFlatInputStatefulRNNrunInferenceFlatInputStatefulRNN
Internal.Utilities.SubModels.Array2DFlatteningModelarray2DFlatteningModel
Internal.Utilities.SubModels.Array2DDeflatteningModelarray2DDeflatteningModel