PyTorch Model Predict
Libraries:
Deep Learning Toolbox /
Python Neural Networks
Description
The PyTorch Model Predict block predicts responses using a pretrained Python®
PyTorch® 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. Your MATLAB Python environment must have the
torch
module installed. The PyTorch Model Predict block has been
tested using Python version 3.10 and torch
version 2.0.1.
Load a Python model into the block by specifying the path to a PyTorch model file that you
saved in Python using torch.save()
or torch.jit.save()
.
You can optionally load a Python function to preprocess the input data that Simulink® passes to the Python model, and a Python function to postprocess the predicted
responses from the model.
The input port In1 receives input data, optionally rearranges the input array dimensions, and converts the input data to a Python array. The preprocessing function (if specified) processes the converted data in Python and passes it to the PyTorch model. The model generates predicted responses for the input data in Python and passes the responses to the Python postprocessing function (if specified). The output port Out1 returns the predicted responses.
You can add and configure input and output ports using the Inputs and Outputs tabs of the Block Parameters dialog box (see Inputs and Outputs). The software attempts to automatically populate the table in each tab when you click the Autofill Fields From Model File button on the Specify model file tab.
Note
You cannot run the PyTorch Model Predict block in Rapid Accelerator mode.
Examples
Ports
Input
Output
Parameters
Block Characteristics
Data Types |
|
Direct Feedthrough |
|
Multidimensional Signals |
|
Variable-Size Signals |
|
Zero-Crossing Detection |
|
Tips
If you encounter a Python library conflict, use the
pyenv
function to specify theExecutionMode
name-value argument as"OutOfProcess"
.
Version History
Introduced in R2024a
See Also
Blocks
- ONNX Model Predict | TensorFlow Model Predict | Custom Python Model Predict | Scikit-learn Model Predict (Statistics and Machine Learning Toolbox)