Skip to content

Instantly share code, notes, and snippets.

@driazati
Created October 1, 2019 03:59
Show Gist options
  • Save driazati/6c50d8e15b18d2c043618a8a2432a99e to your computer and use it in GitHub Desktop.
Save driazati/6c50d8e15b18d2c043618a8a2432a99e to your computer and use it in GitHub Desktop.
class M(nn.Module):
# Only empty lists/dicts and TorchScript classes need types
__annotations__ = {
my_list: List[int],
}
def __init__(self, some_values):
super(M, self).__init__()
for key in some_values:
setattr(self, key, some_values[key])
def forward(self):
pass
my_values_to_save = {
'a': 20,
'b': [1, 2, 3],
'c': torch.randn(2, 2)
}
torch.jit.script(M(my_values_to_save)).save('out.pt')
# In C++, load `out.pt` as a script::Module and use `get_attribute` to extract
# values by name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment