主要内容

PyTorchExportedProgram

PyTorch ExportedProgram model

Since R2026a

    Description

    Add-On Required: This feature requires the MATLAB Coder Support Package for PyTorch and LiteRT Models add-on.

    You can use PyTorchExportedProgram objects to execute the model or to generate code in MATLAB® or Simulink®.

    Creation

    Create a PyTorchExportedProgram object by using the loadPyTorchExportedProgram function.

    Properties

    expand all

    Model path, specified as a character vector.

    Function names, specified as a cell array of character vectors.

    Object Functions

    summaryDisplay input and output specifications of PyTorch ExportedProgram or LiteRT model
    inputSpecificationsReturn input specifications for functions in PyTorch ExportedProgam or LiteRT model
    outputSpecificationsReturn output specifications for functions in PyTorch ExportedProgram or LiteRT model
    invokeRun inference on a PyTorch ExportedProgram or LiteRT model input to compute output

    Examples

    collapse all

    Suppose that you have a PyTorch ExportedProgram model named siso.pt2 that takes one input and returns one output. Create a PyTorchExportedProgram object.

    myModel = loadPyTorchExportedProgram("siso.pt2")
    Loading the model. This may take a few minutes.
    
    myModel = 
      PyTorchExportedProgram contained in siso.pt2: 
    
                      Input Specifications               
        _________________________________________________
    
        Input    Name            Size              Type  
        _____    _____    ___________________    ________
                                                         
          1      "in1"    "1 x 3 x 128 x 128"    "single"
    
    
                     Output Specifications             
        _______________________________________________
    
        Output     Name          Size            Type  
        ______    ______    _______________    ________
                                                       
          1       "out1"    "1 x 8 x 2 x 1"    "single"
    
    
        properties: 
            ModelPath                         -      Path to the model file
            FcnNames                          -      Names of the functions in the model
        methods: 
            invoke                            -      Performs forward inference by invoking a function in the model
            inputSpecifications               -      Returns the input specifications of the model or of a specific function in the model
            outputSpecifications              -      Returns the output specifications of the model or of a specific function in the model
            summary                           -      Displays the input and output specifications of the model or of a specific function in the model
    
    

    Limitations

    You cannot save the PyTorchExportedProgram object from workspace to a MAT-file.

    Extended Capabilities

    expand all

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    GPU Code Generation
    Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

    Version History

    Introduced in R2026a