<jupyter_start><jupyter_text>This notebook contains an excerpt from the [PyTorch Pocket Reference](http://pytorchbook.com) book by [<NAME>](http://joepapa.ai); content is available [on GitHub](https://github.com/joe-papa/pytorch-book). [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/joe-papa/pytorch-book/blob/main/07_03_Deploying_Mobile.ipynb)  Chapter 7 - Deploy to Production  Mobile<jupyter_code>import torch
import torchvision
from torch.utils.mobile_optimizer \
  import optimize_for_mobile

model = torchvision.models.vgg16(pretrained=True)
model.eval()
example = torch.rand(1, 3, 224, 224) # <1>

traced_script_module = \
  torch.jit.trace(model, example) # <2>
torchscript_model_optimized = \
  optimize_for_mobile(traced_script_module) # <3>
torchscript_model_optimized.save("model.pt") # <4><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13842746400885936, 'fraction_numerical': 0.021040974529346623, 'mean_word_length': 6.232, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '349013', 'license': 'MIT', 'n_tokens_mistral': 338, 'n_tokens_neox': 327, 'n_words': 52, 'path': '07_03_Deploying_Mobile.ipynb', 'repo_name': 'kingslyt/pytorch-book', 'size': '905'}