Created
November 16, 2021 17:33
-
-
Save dhruvbird/06f523bed5d1a2b9130f82e70854fdb3 to your computer and use it in GitHub Desktop.
Create a simple PyTorch Model and save it in Lite Format along with a single metadata file (model_info.txt)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import zipfile | |
class AddTensorsModel(torch.nn.Module): | |
def __init__(self): | |
super().__init__(); | |
def forward(self, x, y): | |
return x + y | |
m = AddTensorsModel() | |
# Create a scripted mode so that we can save it for mobile consumption | |
scripted = torch.jit.script(m) | |
# Create an extra_files map so that we can add this additional | |
# metadata when saving the model. | |
extra_files = { | |
"model_info.txt": "This model's forward() method accepts 2 tensors, and returns their sum", | |
} | |
scripted._save_for_lite_interpreter("AddTensorsModel.ptl", _extra_files = extra_files) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment