PyTorch Coexecution
Before you can run simulations in MATLAB® and PyTorch®, you must first configure your computer for coexecution. You can then:
Use MATLAB to generate data sets to train Python® models by leveraging the Wireless Communications toolboxes.
Test Python models in link and system level simulations.
Integrate your work in Model Based Design workflows. For more information, see Deep Learning with Simulink (Deep Learning Toolbox).
Leverage the software-defined radio (SDR) support to test your design with over-the-air (OTA) signals.
After qualifying your design, you can import and deploy your system onto several possible platforms. For more information, see Interoperability Between Deep Learning Toolbox, TensorFlow, PyTorch, and ONNX (Deep Learning Toolbox).
Try some sample AI applications using the Example Workflows Using Coexecution.
Set Up Environment for Coexecution
Make sure your computer is set up for configuration:
To call Python modules in MATLAB, you must have a supported version of the reference implementation (CPython) installed on your system. For more information, see Configure Your System to Use Python.
Use the
pyenvfunction to change the default version or execution mode of the Python interpreter. To check the interpreter configuration, view thePythonEnvironmentobject returned by thepyenvfunction.By default, MATLAB selects the version of Python based on your system path. To use a specific Python version, specify the
Versionproperty when you call thepyenvfunction.MATLAB selects and loads a Python interpreter when you type a Python expression from MATLAB using the
pynamespace, for example,py.list.
To debug Python code from MATLAB, see How can I debug Python code using MATLABs Python Interface and Visual Studio Code in MATLAB Answers™.
Example Workflows Using Coexecution
These examples demonstrate AI for wireless applications using PyTorch coexecution.
Preprocess Data for AI-Based CSI Feedback Compression (5G Toolbox)
Preprocess Data for AI-Based CSI Prediction (5G Toolbox)
Train PyTorch Channel Prediction Models (5G Toolbox)
Train PyTorch Channel Prediction Models with Online Training (5G Toolbox)
Offline Training and Testing of PyTorch Model for CSI Feedback Compression (5G Toolbox)
Online Training and Testing of PyTorch Model for CSI Feedback Compression (5G Toolbox)
Apply Transfer Learning on PyTorch Model to Identify 5G and LTE Signals (5G Toolbox)
Verify Performance of 6G AI-Native Receiver Using MATLAB and PyTorch Coexecution (5G Toolbox)
Each example includes a requirements supporting file to identify the Python libraries that you need and their specific versions. You can use the requirements files with pip to install all the required libraries. For more information, see https://pip.pypa.io/en/stable/user_guide/.
To run each example, you can use Python installed in a virtual environment or any other installation that includes the required Python libraries. To avoid potential conflicts between Python versions, follow the guidance in Install Python in Virtual Environment to create a virtual environment.
You can use the Python view in the External Languages panel to configure and maintain your Python environments directly from MATLAB. For more information, see Manage Python Environments Using External Languages Panel.
To permit easy reconfiguring of the environment for initial simulation runs, the application examples have
ExecutionModeset to"OutOfProcess"for thepyenvfunction. Running Python functions out-of-process introduces overhead between MATLAB and Python that increases run time. For more information, see Out-of-Process Execution of Python Functionality.Once you optimize the environment settings, switch the
ExecutionModeproperty toInProcessto avoid overhead between the MATLAB and Python processes.
Note
After first loading pyenv with
ExecutionMode set to
"InProcess", you must restart MATLAB to change the execution mode to
"OutOfProcess" and to change the Python version.