How to generate code for a Deep Learning network imported from PyTorch?

I have imported a model into MATLAB using "importNetworkFromPyTorch". The network works in MATLAB and I try to generate code using the following documentation example: Generate Generic C/C++ Code for Deep Learning Networks
However, I receive errors such as:
Unsupported custom layer 'aten__linear0'. Code generation does not support custom layers without '%#codegen' defined in the class definition.
The 'nnet.layer.AutogeneratedFromPyTorch' class does not support code generation.
Why am I getting this error, and how can I fix it?

 採用された回答

MathWorks Support Team
MathWorks Support Team 2026 年 4 月 14 日
The errors are caused by the network containing custom layers that were automatically generated by the importNetworkFromPyTorch function, which are not supported for code generation. Use the analyzeNetworkForCodegen function to see which layers in your network are compatible for code generation.
You can try the following steps to make your network ready for code generation:
1. Pass input sizes when importing the network:
Try passing input sizes to the importer when you import the model using importNetworkFromPyTorch. This will help Deep Learning Toolbox produce more built-in layers. We also recommend giving Deep Network Designer a try, as it will prompt you to specify parameters such as input sizes, make editing the network easier, and connect to the next step in your workflow, like export to Simulink or code generation analysis. 
2. Upgrade to a newer MATLAB release: 
Try upgrading to a newer MATLAB release to see if the importNetworkFromPyTorch function can import more layers into built-in layers rather than custom layers.
3. Try the Exported Program PyTorch format (R2026a+)
You can try to upgrade to R2026a, where the PyTorch importer supports a new model format from PyTorch called Exported Program. These files are created using the torch.export.export command in Python, and typically end with the file extension .pt2. Use the new model format if they can. It stores more information than the older "traced" model format, which helps to better import the network into built-in layers. It's usually a 2-line code difference to switch from using the old model format to the new one:
# Export the model and save it in PyTorch version 2.8 (must use PyTorch 2.8).
exported_model = torch.export.export(model, (X,))
torch.export.save(exported_model, 'myModel.pt2')
4. Convert your PyTorch model to ONNX:
Another option is to convert your PyTorch model to ONNX using torch.onnx.export and then import it for code generation. Code generation support for custom layers created by the ONNX Importer was introduced in R2025a:
5. Further Assistance:
If none of the above helps, the only workaround is to write a version of the layer that is supported for code generation. This is very difficult and not recommended. An enhancement has been submitted to support code generation for custom layers in a future version of MATLAB. Contact MathWorks Technical Support for more help.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDeep Learning with GPU Coder についてさらに検索

製品

リリース

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by