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.8.
Load a Python model into the block by specifying the path to a PyTorch model file that you
saved in Python using torch.save(), torch.jit.save(),
torch.export.save(), or a .safetensors file. 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 PyTorch Model Predict block also allows you to specify the
execution device for the Python 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 from the provided model file.
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
pyenvfunction to specify theExecutionModename-value argument as"OutOfProcess".To load a model that has been saved as a full model file (using
torch.save) or a weight file, the PyTorch model class needs to be defined in a standalone.pyfile. To do this, define the PyTorch model class in a file with a.pyextension. The model class file must be on the Python path or in the current working directory. Next, import the model into Python using theimportcommand and save it using thetorch.save()function. The following code shows an example of saving a PyTorch model of classMyClassdefined in the fileMyModule.py.import MyModule mdl = MyModule.MyClass() torch.save(mdl,"savedMdl.pt")
If you execute the model class file in a script, notebook or at the command line instead of using the
importcommand, the PyTorch Model Predict block throws the following error:This happens becauseAttributeError: Can't get attribute 'MyClass' on <module '__main__' (built-in)>
torch.save()stores references to the class by its module path (e.g.,myModule.MyClass). If the class was defined while running the file as a script or in a notebook, its module is__main__instead of an importable module name. To fix the error do either one of the following :-Recreate the model definition
MyClassin a standalone.pyfile, for e.g.MyModule.pyand resave the model.import MyModule mdl = MyModule.MyClass() torch.save(mdl,"savedMdl.pt")
To reuse an existing saved model, whose model class was defined in the
__main__scope, do the following:Reload the saved model in the Python script or notebook containing the model class definition. Then, save only the model weights to a new file.
class MyClass ... model = torch.load("savedMdl.pt") torch.save(model.state_dict(),"savedMdlWeights.pth")
Copy the model class definition
MyClassinto a standalone.pyfile, e.g.MyModule.pyand add that file to the Python path or current working directory.python MyModule.py class MyClass ...
Then provide the path to the weight file and model class constructor command in the Block Parameters dialog box.
Version History
Introduced in R2024aSee Also
Blocks
- ONNX Model Predict | TensorFlow Model Predict | Custom Python Model Predict | Scikit-learn Model Predict (Statistics and Machine Learning Toolbox)

