C++ 前端#
PyTorch C++ 前端是一個 C++17 庫,用於 CPU 和 GPU Tensor 計算,具備自動微分和用於最先進機器學習應用的高階構建塊。
描述#
PyTorch C++ 前端可以被視為 PyTorch Python 前端的 C++ 版本,提供了自動微分以及用於機器學習和神經網路的各種高階抽象。具體來說,它包含以下元件:
元件 |
描述 |
|---|---|
|
自動微分、高效的 CPU 和 GPU Tensor |
|
一組可組合的模組,用於神經網路建模 |
|
用於訓練模型的最佳化演算法,例如 SGD、Adam 或 RMSprop |
|
資料集、資料管道以及多執行緒、非同步資料載入器 |
|
用於儲存和載入模型檢查點的序列化 API |
|
將 C++ 模型繫結到 Python 的膠水程式碼 |
|
對 TorchScript JIT 編譯器的純 C++ 訪問 |
端到端示例#
這裡是一個在 MNIST 資料集上定義和訓練簡單神經網路的端到端示例:
#include <torch/torch.h>
// Define a new Module.
struct Net : torch::nn::Module {
Net() {
// Construct and register two Linear submodules.
fc1 = register_module("fc1", torch::nn::Linear(784, 64));
fc2 = register_module("fc2", torch::nn::Linear(64, 32));
fc3 = register_module("fc3", torch::nn::Linear(32, 10));
}
// Implement the Net's algorithm.
torch::Tensor forward(torch::Tensor x) {
// Use one of many tensor manipulation functions.
x = torch::relu(fc1->forward(x.reshape({x.size(0), 784})));
x = torch::dropout(x, /*p=*/0.5, /*train=*/is_training());
x = torch::relu(fc2->forward(x));
x = torch::log_softmax(fc3->forward(x), /*dim=*/1);
return x;
}
// Use one of many "standard library" modules.
torch::nn::Linear fc1{nullptr}, fc2{nullptr}, fc3{nullptr};
};
int main() {
// Create a new Net.
auto net = std::make_shared<Net>();
// Create a multi-threaded data loader for the MNIST dataset.
auto data_loader = torch::data::make_data_loader(
torch::data::datasets::MNIST("./data").map(
torch::data::transforms::Stack<>()),
/*batch_size=*/64);
// Instantiate an SGD optimization algorithm to update our Net's parameters.
torch::optim::SGD optimizer(net->parameters(), /*lr=*/0.01);
for (size_t epoch = 1; epoch <= 10; ++epoch) {
size_t batch_index = 0;
// Iterate the data loader to yield batches from the dataset.
for (auto& batch : *data_loader) {
// Reset gradients.
optimizer.zero_grad();
// Execute the model on the input data.
torch::Tensor prediction = net->forward(batch.data);
// Compute a loss value to judge the prediction of our model.
torch::Tensor loss = torch::nll_loss(prediction, batch.target);
// Compute gradients of the loss w.r.t. the parameters of our model.
loss.backward();
// Update the parameters based on the calculated gradients.
optimizer.step();
// Output the loss and checkpoint every 100 batches.
if (++batch_index % 100 == 0) {
std::cout << "Epoch: " << epoch << " | Batch: " << batch_index
<< " | Loss: " << loss.item<float>() << std::endl;
// Serialize your model periodically as a checkpoint.
torch::save(net, "net.pt");
}
}
}
}
要檢視更多使用 PyTorch C++ 前端的完整示例,請參閱示例倉庫。
設計理念#
PyTorch 的 C++ 前端在設計時遵循的理念是:Python 前端非常出色,應儘可能使用;但在某些場景下,效能和可移植性要求使得使用 Python 直譯器不可行。例如,對於低延遲、高效能或多執行緒環境(如影片遊戲或生產伺服器),Python 不是一個好的選擇。C++ 前端的目標是解決這些用例,同時不犧牲 Python 前端的使用者體驗。
因此,C++ 前端在編寫時遵循了幾個設計理念:
在設計、命名、約定和功能上密切模仿 Python 前端。雖然兩個前端之間可能偶爾存在差異(例如,我們放棄了 Python 前端中的已棄用功能或修復了“缺點”),但我們保證將 Python 模型移植到 C++ 的工作應僅限於翻譯語言特性,而不是修改功能或行為。
優先考慮靈活性和使用者友好性,而非微觀最佳化。 在 C++ 中,通常可以獲得最最佳化的程式碼,但這往往以犧牲使用者體驗為代價。靈活性和動態性是 PyTorch 的核心,C++ 前端力求保留這種體驗,在某些情況下會犧牲效能(或“隱藏”效能調節器)以保持 API 的簡單和易於理解。我們希望不以 C++ 為生的研究人員也能使用我們的 API。
友情提示:Python 未必比 C++ 慢!Python 前端幾乎所有計算密集型操作(尤其是任何型別的數值運算)都會呼叫 C++,這些操作會佔用程式執行的大部分時間。如果您更喜歡編寫 Python,並且能夠承受使用 Python 的成本,我們建議使用 PyTorch 的 Python 介面。但是,如果您更喜歡編寫 C++,或者需要編寫 C++(因為多執行緒、延遲或部署要求),PyTorch 的 C++ 前端提供了一個與 Python 對應介面大致一樣方便、靈活、友好和直觀的 API。這兩個前端服務於不同的用例,協同工作,任何一方都不能無條件取代另一方。