Contenido principal

export

R2026b

Save underlying PyTorch model in different PyTorch file format

Since R2026b

    Description

    export(model,format,fileName) saves the underlying PyTorch® model to the specified file in the specified format. When using this syntax, format is "full", "weights(zipped)", "weights(safetensors)", or "scripted". The ModelType of the model must be "torch.nn.Module".

    example

    export(___,X1,...,XM) saves the PyTorch model as an Exported Program or Traced model, passing example input data to trace the model. When using this syntax, format is "exportedProgram" or "traced".

    example

    pythonObject = export(___) also returns the Python® object saved to a file.

    example

    Examples

    collapse all

    Export a PyTorch model containing a torch.nn.Module as a full model, weights-only, or scripted model file.

    Load a multi-input, multi-output model, specifying data-transfer settings.

    model = pyTorchModel("mimo32_Full.pt",...
        NumInputs=3, ...
        InputDimensionOrder={[3 4 1 2],[2 3 1],[2 3 1]},...
        InputNumDimensions={4 3 3},...
        InputDataType={"float32","float32","float32"},...
        OutputDimensionOrder={[1 2 3 4],[1 2 3]});

    Save as a full model.

    export(model,"full","mimo32_full_2.pt")

    Save as a scripted model.

    export(model,"scripted","mimo32_scripted_2.pt")

    Save as a weights-only model using torch.save (zipped format).

    export(model,"weights(zipped)","mimo32_weights_2.pth")

    Save as a weights-only model using safetensors.

    export(model, "weights(safetensors)","mimo32_weights_2.safetensors")

    Export a PyTorch model containing a torch.nn.Module as an exported program or traced model file, providing example inputs.

    Load a multi-input, multi-output model, specifying data-transfer settings.

    model = pyTorchModel("mimo32_Full.pt",...
        NumInputs=3, ...
        InputDimensionOrder={[3 4 1 2],[2 3 1],[2 3 1]},...
        InputNumDimensions={4 3 3},...
        InputDataType={"float32","float32","float32"},...
        OutputDimensionOrder={[1 2 3 4],[1 2 3]});

    Create example input to trace the model.

    X1 = rand([3 4 1 2]);
    X2 = rand([6 1 5]);
    X3 = rand([8 1 7]);

    Save as an exported program.

    export(model,"exportedProgram","mimo32_exported_2.pt2",X1,X2,X3)

    Save as a traced model.

    export(model,"traced","mimo32_traced_2.pt",X1,X2,X3)

    Export a model as an exported program and capture the returned Python ExportedProgram object.

    Load a model and export it as an exported program, capturing the returned Python object.

    model = pyTorchModel("fooBarModel.pt",InputDataType="float32");
    X = rand(17,2);
    ep = export(model,"exportedProgram","fooBarModel.pt2",X);

    Input Arguments

    collapse all

    Reference to a PyTorch model, specified as a PyTorchModel object. ModelType of the model must be "torch.nn.Module".

    Target file format, specified as one of these values:

    • "full" — Full model (architecture and weights saved with torch.save(model,file)).

    • "weights(zipped)" — Weights-only in zipped format (saved with torch.save(model.state_dict(),file)).

    • "weights(safetensors)" — Weights-only in safetensors format.

    • "scripted" — Scripted model (saved with torch.jit.save).

    • "exportedProgram" — Exported Program (saved with torch.export.save). Requires example input X1,...,XM.

    • "traced" — Traced model (saved with torch.jit.save). Requires example input X1,...,XM.

    Data Types: string | char

    Name of the file for saving the model, specified as a string or character vector.

    Data Types: char | string

    Example input data on which to trace the model, specified as MATLAB® numeric arrays, logical arrays, gpuArray objects, py.* objects, or other MATLAB data types compatible with the MATLAB Python interface. This argument is required when format is "exportedProgram" or "traced".

    Numeric arrays are converted to torch.Tensor objects using the data-transfer settings stored in the model.

    Output Arguments

    collapse all

    Python object that was saved to file, returned as a py.* object. Depending on the format, this can be a subclass of torch.nn.Module, a dict, an ExportedProgram, or a RecursiveScriptModule.

    Algorithms

    collapse all

    Version History

    Introduced in R2026b