import torch
  class MyModule(torch.nn.Module):

    def __init__(self, N, M):
      super(MyModule, self).__init__()
      self.weight = torch.nn.Parameter(torch.rand(N, M))

    def forward(self, input):
      if input.sum() > 0:
        output = self.weight.mv(input)
      else:
        output = self.weight + input
      return output

    # Compile the model code to a static representation
    my_script_module = torch.jit.script(MyModule(3, 4))

    # Save the compiled code and model data so it can be loaded elsewhere
    my_script_module.save("my_script_module.pt")

TorchScript

借助TorchScript,PyTorch在急切模式下提供了易用性和灵活性,同时无缝过渡到图形模式以实现C ++运行时环境中的速度,优化和功能.

Distributed Training

通过利用对Python和C ++可以访问的集体操作和对等通信的异步执行的本机支持,优化研究和生产中的性能.

  import torch.distributed as dist
  from torch.nn.parallel import DistributedDataParallel
  
  dist.init_process_group(backend='gloo')
  model = DistributedDataParallel(model)
  ## Save your model
  torch.jit.script(model).save("my_mobile_model.pt")

  ## iOS prebuilt binary
  pod LibTorch
  ## Android prebuilt binary
  implementation 'org.pytorch:pytorch_android:1.3.0'

  ## Run your model (Android example)
  Tensor input = Tensor.fromBlob(data, new long[]{1, data.length});
  IValue output = module.forward(IValue.tensor(input));
  float[] scores = output.getTensor().getDataAsFloatArray();

Mobile (Experimental)

PyTorch支持从Python到在iOS和Android上部署的端到端工作流. 它扩展了PyTorch API,以涵盖将ML集成到移动应用程序中所需的常见预处理和集成任务.

Tools & Libraries

活跃的研究人员和开发人员社区建立了丰富的工具和库生态系统,用于扩展PyTorch并支持从计算机视觉到强化学习的领域的开发.

  import torchvision.models as models
  resnet18 = models.resnet18(pretrained=True)
  alexnet = models.alexnet(pretrained=True)
  squeezenet = models.squeezenet1_0(pretrained=True)
  vgg16 = models.vgg16(pretrained=True)
  densenet = models.densenet161(pretrained=True)
  inception = models.inception_v3(pretrained=True)
  import torch.onnx
  import torchvision

  dummy_input = torch.randn(1, 3, 224, 224)
  model = torchvision.models.alexnet(pretrained=True)
  torch.onnx.export(model, dummy_input, "alexnet.onnx")

Native ONNX Support

以标准ONNX(开放式神经网络交换)格式导出模型,以直接访问与ONNX兼容的平台,运行时,可视化工具等.

C++ Front-End

C ++前端是PyTorch的纯C ++接口,它遵循已建立的Python前端的设计和体系结构. 它旨在实现高性能,低延迟和裸机C ++应用程序的研究.

  #include <torch/torch.h>

  torch::nn::Linear model(num_features, 1);
  torch::optim::SGD optimizer(model->parameters());
  auto data_loader = torch::data::data_loader(dataset);

  for (size_t epoch = 0; epoch < 10; ++epoch) {
    for (auto batch : data_loader) {
      auto prediction = model->forward(batch.data);
      auto loss = loss_function(prediction, batch.target);
      loss.backward();
      optimizer.step();
    }
  }
  export IMAGE_FAMILY="pytorch-latest-cpu"
  export ZONE="us-west1-b"
  export INSTANCE_NAME="my-instance"
  
  gcloud compute instances create $INSTANCE_NAME \
    --zone=$ZONE \
    --image-family=$IMAGE_FAMILY \
    --image-project=deeplearning-platform-release

Cloud Partners

PyTorch在主要的云平台上得到了很好的支持,可通过预构建的图像进行无摩擦的开发并轻松扩展,在GPU上进行大规模培训,可以在生产规模环境中运行模型,等等.