跳轉到主要內容

端到端機器學習框架

PyTorch 透過使用者友好的前端、分散式訓練以及工具和庫的生態系統,實現快速、靈活的實驗和高效的生產。

入門

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,PyTorch 在即時模式下提供易用性和靈活性,同時無縫轉換為圖形模式,以在 C++ 執行時環境中實現速度、最佳化和功能。

TorchServe

TorchServe 是一款易於使用的工具,用於大規模部署 PyTorch 模型。它與雲和環境無關,支援多模型服務、日誌記錄、指標以及為應用程式整合建立 RESTful 端點等功能。


## Convert the model from PyTorch to TorchServe format
torch-model-archiver --model-name densenet161 \
--version 1.0 --model-file serve/examples/image_classifier/densenet_161/model.py \
--serialized-file densenet161-8d451a50.pth \
--extra-files serve/examples/image_classifier/index_to_name.json \
--handler image_classifier

## Host your PyTorch model

torchserve --start --model-store model_store --models densenet161=densenet161.mar

import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel

dist.init_process_group(backend='gloo')
model = DistributedDataParallel(model)

分散式訓練

透過利用對集體操作和點對點通訊非同步執行的本機支援(可從 Python 和 C++ 訪問),最佳化研究和生產中的效能。

移動端(實驗性)

PyTorch 支援從 Python 到在 iOS 和 Android 上部署的端到端工作流。它擴充套件了 PyTorch API,涵蓋了在移動應用程式中整合機器學習所需的常見預處理和整合任務。


## 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();

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)

強大的生態系統

活躍的研究人員和開發人員社群構建了豐富的工具和庫生態系統,用於擴充套件 PyTorch 並支援從計算機視覺到強化學習等領域的發展。

原生 ONNX 支援

以標準 ONNX(開放神經網路交換)格式匯出模型,以便直接訪問 ONNX 相容平臺、執行時、視覺化工具等。


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")

#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();
  }
}

C++ 前端

C++ 前端是 PyTorch 的純 C++ 介面,遵循既有 Python 前端的設計和架構。它旨在支援高效能、低延遲和裸機 C++ 應用程式的研究。

雲支援

PyTorch 在主要的雲平臺上得到了很好的支援,透過預構建映象、在 GPU 上進行大規模訓練、在生產規模環境中執行模型等,提供了無摩擦的開發和輕鬆的擴充套件。


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