Custom Python Model Predict - Predict responses using pretrained custom Python model - Simulink (original) (raw)

Predict responses using pretrained custom Python model

Since R2024a

Libraries:
Statistics and Machine Learning Toolbox / Deep Learning Toolbox / Python Models / Python Neural Networks

Description

The Custom Python Model Predict block predicts responses using a pretrained custom Python® model running in the MATLAB® Python environment. MATLAB supports the reference implementation of Python, often called CPython. If you use a Mac or Linux® platform, you already have Python installed. If you use Windows®, you need to install a distribution, such as those found at https://www.python.org/downloads/. For more information, see Configure Your System to Use Python. The Custom Python Model Predict block has been tested using Python version 3.10.

Load a custom Python model into the block by specifying the path to a Python file that defines load_model() and predict() functions. TheCustom Python Model Predict block calls the load_model() function once in Python with arguments you specify in Arguments to load_model(), and then calls thepredict() function at every simulation step. The input portIn1 receives input data, and the output port Out1 returns predicted responses for the input data. You can add input and output ports using theInputs and Outputs tabs of the Block Parameters dialog box (see Inputs and Outputs).

Note

You cannot run the Custom Python Model Predict block in Rapid Accelerator mode.

Examples

Ports

Input

expand all

Input data, specified as a numeric array. You can rearrange the dimensions of the input data that the block passes to the Python predict() function by specifying a permutation vector on the Inputs tab of the Block Parameters dialog box (see Inputs).

Data Types: single | double | half | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point

Output

expand all

Predicted responses, returned as a numeric array. You can rearrange the dimensions of the output data returned by the Python model by specifying a permutation vector on the Outputs tab of the Block Parameters dialog box (see Outputs).

Data Types: single | double | half | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean

Parameters

expand all

To edit block parameters interactively, use theProperty Inspector. From the Simulink® Toolstrip, on the Simulation tab, in thePrepare gallery, select Property Inspector.

Specify model file

Specify the name or path of a Python model file, or click theBrowse button. The file must contain the definitions of two functions: load_model() and predict(), and can optionally contain a reset_model() function.

Programmatic Use

Block Parameter: PythonModulePath
Type: character vector
Values: Python file | path to Python file
Default: "untitled"

Specify a comma-separated list of strings or numeric arrays that theCustom Python Model Predict block passes to the load_model() function specified by Path to Python file defining load_model() and predict().

Programmatic Use

Block Parameter: LoadModelArgs
Type: comma-separated list
Values: string | numeric array

Specify the discrete interval between sample time hits or specify another type of sample time, such as continuous (0) or inherited (–1). For more options, see Types of Sample Time (Simulink).

By default, the Custom Python Model Predict block inherits sample time based on the context of the block within the model.

Programmatic Use

Block Parameter: SampleTime
Type: string scalar or character vector
Values: scalar
Default: "–1"

Inputs

Input port properties, specified as a table. Each row of the table corresponds to an input port of the Custom Python Model Predict block. Double-click a table cell entry to edit its value, and use the Up andDown buttons to reorder the table rows. Add and delete input ports by clicking the New and Delete buttons, respectively. If you specify multiple input ports, their order must correspond to the inputList order in the Pythonpredict() function (see Path to Python file defining load_model() and predict()).

The table has the following columns:

Programmatic Use

Block Parameter: InputTable
Type: cell array

Outputs

Output port properties, specified as a table. Each row of the table corresponds to an individual output port of the Custom Python Model Predict block. Double-click a table cell entry to edit its value, and use the Up andDown buttons to reorder the table rows. Add or delete output ports by clicking the New and Delete buttons, respectively. If you specify multiple output ports, their order must correspond to the output order in the Python model.

The table has the following columns:

Programmatic Use

Block Parameter: OutputTable
Type: cell array

Block Characteristics

Data Types Boolean | double enumerated fixed point half integer single
Direct Feedthrough yes
Multidimensional Signals yes
Variable-Size Signals yes
Zero-Crossing Detection no

Version History

Introduced in R2024a

See Also

Blocks