8000 `optim.LoadStateDict` from existing StateDict doesn't clone tensors · Issue #1172 · dotnet/TorchSharp · GitHub
[go: up one dir, main page]

Skip to content
optim.LoadStateDict from existing StateDict doesn't clone tensors #1172
Closed
@shaltielshmid

Description

@shaltielshmid

When I call optim.LoadStateDict from a state dictionary of an exiting optimizer, the tensors are copied by reference, so if the old optimizer gets disposed the tensors are invalid.

Sample code:

var lin1 = torch.nn.Linear(10, 10);

var optim1 = torch.optim.Adam(lin1.parameters());
var optim2 = torch.optim.Adam(lin1.parameters());
optim2.load_state_dict(optim1.state_dict());
optim1.Dispose();

torch.nn.functional.mse_loss(lin1.call(torch.rand(10)), torch.rand(10)).backward();
optim2.step();

Throws:

System.InvalidOperationException: 'Tensor invalid -- empty handle.'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0