Hauptinhalt

reload

R2026b

Reload PyTorch model from stored model loading properties

Since R2026b

    Description

    reload(model) reloads the PyTorch® model object from the stored model loading properties in model. Use this function to restore the Python® model after terminating the Python environment, or to switch to a different model file while retaining the existing data-transfer and function configuration settings.

    example

    Examples

    collapse all

    Reload the PyTorch model after the Python environment has been terminated and the underlying Python model object has been deleted.

    Load a model.

    model = pyTorchModel("model1OnPath.pt");
    model.PythonObject
    ans =
      Python MyModelClass1 with properties:
    
        training: 1
    
        MyModelClass1(
          (fc1): Linear(in_features=2, out_features=3, bias=True)
          (relu): ReLU()
          (fc2): Linear(in_features=3, out_features=5, bias=True)
        )

    Terminate the Python environment. The Python model is deleted.

    terminate(pyenv)
    model.PythonObject
    ans =
      handle to deleted MyModelClass1

    Reload the Python model.

    reload(model);
    model.PythonObject
    ans =
      Python MyModelClass1 with properties:
    
        training: 1
    
        MyModelClass1(
          (fc1): Linear(in_features=2, out_features=3, bias=True)
          (relu): ReLU()
          (fc2): Linear(in_features=3, out_features=5, bias=True)
        )

    Change the PyTorch model to a different one without specifying the data-transfer and added function settings again.

    Load a model, specifying data-transfer settings.

    model = pyTorchModel("mimo32_Full.pt",...
        NumInputs=3, ...
        InputDimensionOrder={[3 4 1 2],[2 3 1],[]},...
        InputNumDimensions={4 [] 3},...
        InputDataType={"float32","float32","float32"})
    model =
      PyTorchModel with properties:
    
             PythonObject: [1x1 py.mimo32.MIMO32]
                ModelType: "torch.nn.Module"
                 FileName: "mimo32_Full.pt"
               ModulePath: ""
               ModuleName: ""
        PyTorchFunctions: [1x1 struct]
                NumInputs: 3
      InputDimensionOrder: {[3 4 1 2]  [2 3 1]  []}
       InputNumDimensions: {[4]  []  [3]}
            InputDataType: ["float32"    "float32"    "float32"]
           InputKeyNames: [1x0 string]
     OutputDimensionOrder: {[]}
           OutputDataType: ""

    Change the model filename and reload. The data-transfer settings are preserved.

    model.FileName = "mimo32_PT26.pt2";
    reload(model)
    model =
      PyTorchModel with properties:
    
             PythonObject: [1x1 py.torch.fx.graph_module.GraphModule]
                ModelType: "ExportedProgram"
                 FileName: "mimo32_PT26.pt2"
               ModulePath: ""
               ModuleName: ""
        PyTorchFunctions: [1x1 struct]
                NumInputs: 3
      InputDimensionOrder: {[]  []  []}
       InputNumDimensions: {[4]  [3]  [3]}
            InputDataType: ["float32"    "float32"    "float32"]
           InputKeyNames: [1x0 string]
     OutputDimensionOrder: {[]}
           OutputDataType: ""

    Input Arguments

    collapse all

    Reference to a PyTorch model, specified as a PyTorchModel object. The model loading properties (FileName, ModulePath, ModuleName, or ConstructorCommand) stored in the object are used to reload the Python model.

    Version History

    Introduced in R2026b