René's URL Explorer Experiment


Title: The C++ Frontend — PyTorch main documentation

Description: PyTorch C++ Frontend guide — defining models, training loops, and using torch::nn modules in C++.

Keywords:

direct link

Domain: docs.pytorch.org


Hey, it has json ld scripts:
    {
       "@context": "https://schema.org",
       "@type": "Article",
       "name": "The C++ Frontend",
       "headline": "The C++ Frontend",
       "description": "PyTorch C++ Frontend guide \u2014 defining models, training loops, and using torch::nn modules in C++.",
       "url": "/frontend.html",
       "articleBody": "The C++ Frontend# The PyTorch C++ frontend is a C++17 library for CPU and GPU tensor computation, with automatic differentiation and high level building blocks for state of the art machine learning applications. Description# The PyTorch C++ frontend can be thought of as a C++ version of the PyTorch Python frontend, providing automatic differentiation and various higher level abstractions for machine learning and neural networks. Specifically, it consists of the following components: Component Description torch::Tensor Automatically differentiable, efficient CPU and GPU enabled tensors torch::nn A collection of composable modules for neural network modeling torch::optim Optimization algorithms like SGD, Adam or RMSprop to train your models torch::data Datasets, data pipelines and multi-threaded, asynchronous data loader torch::serialize A serialization API for storing and loading model checkpoints torch::python Glue to bind your C++ models into Python torch::jit Pure C++ access to the TorchScript JIT compiler End-to-end example# Here is a simple, end-to-end example of defining and training a simple neural network on the MNIST dataset: #include \u003ctorch/torch.h\u003e // 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\u0027s algorithm. torch::Tensor forward(torch::Tensor x) { // Use one of many tensor manipulation functions. x = torch::relu(fc1-\u003eforward(x.reshape({x.size(0), 784}))); x = torch::dropout(x, /*p=*/0.5, /*train=*/is_training()); x = torch::relu(fc2-\u003eforward(x)); x = torch::log_softmax(fc3-\u003eforward(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\u003cNet\u003e(); // 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\u003c\u003e()), /*batch_size=*/64); // Instantiate an SGD optimization algorithm to update our Net\u0027s parameters. torch::optim::SGD optimizer(net-\u003eparameters(), /*lr=*/0.01); for (size_t epoch = 1; epoch \u003c= 10; ++epoch) { size_t batch_index = 0; // Iterate the data loader to yield batches from the dataset. for (auto\u0026 batch : *data_loader) { // Reset gradients. optimizer.zero_grad(); // Execute the model on the input data. torch::Tensor prediction = net-\u003eforward(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 \u003c\u003c \"Epoch: \" \u003c\u003c epoch \u003c\u003c \" | Batch: \" \u003c\u003c batch_index \u003c\u003c \" | Loss: \" \u003c\u003c loss.item\u003cfloat\u003e() \u003c\u003c std::endl; // Serialize your model periodically as a checkpoint. torch::save(net, \"net.pt\"); } } } } To see more complete examples of using the PyTorch C++ frontend, see the example repository. Philosophy# PyTorch\u2019s C++ frontend was designed with the idea that the Python frontend is great, and should be used when possible; but in some settings, performance and portability requirements make the use of the Python interpreter infeasible. For example, Python is a poor choice for low latency, high performance or multithreaded environments, such as video games or production servers. The goal of the C++ frontend is to address these use cases, while not sacrificing the user experience of the Python frontend. As such, the C++ frontend has been written with a few philosophical goals in mind: Closely model the Python frontend in its design, naming, conventions and functionality. While there may be occasional differences between the two frontends (e.g., where we have dropped deprecated features or fixed \u201cwarts\u201d in the Python frontend), we guarantee that the effort in porting a Python model to C++ should lie exclusively in translating language features, not modifying functionality or behavior. Prioritize flexibility and user-friendliness over micro-optimization. In C++, you can often get optimal code, but at the cost of an extremely unfriendly user experience. Flexibility and dynamism is at the heart of PyTorch, and the C++ frontend seeks to preserve this experience, in some cases sacrificing performance (or \u201chiding\u201d performance knobs) to keep APIs simple and explicable. We want researchers who don\u2019t write C++ for a living to be able to use our APIs. A word of warning: Python is not necessarily slower than C++! The Python frontend calls into C++ for almost anything computationally expensive (especially any kind of numeric operation), and these operations will take up the bulk of time spent in a program. If you would prefer to write Python, and can afford to write Python, we recommend using the Python interface to PyTorch. However, if you would prefer to write C++, or need to write C++ (because of multithreading, latency or deployment requirements), the C++ frontend to PyTorch provides an API that is approximately as convenient, flexible, friendly and intuitive as its Python counterpart. The two frontends serve different use cases, work hand in hand, and neither is meant to unconditionally replace the other. Installation# Instructions on how to install the C++ frontend library distribution, including an example for how to build a minimal application depending on LibTorch, may be found by following this link.",
       "author": {
         "@type": "Organization",
         "name": "PyTorch Contributors",
         "url": "https://pytorch.org"
       },
       "image": "https://pytorch.org/docs/stable/_static/img/pytorch_seo.png",
       "mainEntityOfPage": {
         "@type": "WebPage",
         "@id": "/frontend.html"
       },
       "datePublished": "2023-01-01T00:00:00Z",
       "dateModified": "2023-01-01T00:00:00Z"
     }
 

docsearch:languageen
llm:site-typedocumentation
llm:frameworkPyTorch
llm:descriptionPyTorch C++ Frontend guide — defining models, training loops, and using torch::nn modules in C++.
llm:navigation-filehttps://pytorch.org/docs/stable/llms.txt
llm:sitemaphttps://pytorch.org/docs/stable/sitemap.xml
llm:versionmain
llm:projectPyTorch
llm:page-typedocumentation
og:imagehttps://docs.pytorch.org/docs/stable/_static/img/pytorch_seo.png
None1

Links:

Skip to main contenthttps://docs.pytorch.org/cppdocs/frontend.html#main-content
Home https://docs.pytorch.org/cppdocs/index.html
mainhttps://docs.pytorch.org/cppdocs/index.html
Installing C++ Distributions of PyTorch https://docs.pytorch.org/cppdocs/installing.html
The C++ Frontend https://docs.pytorch.org/cppdocs/frontend.html
C++ API Reference https://docs.pytorch.org/cppdocs/api/index.html
ATen: Tensor Library https://docs.pytorch.org/cppdocs/api/aten/index.html
C10: Core Utilities https://docs.pytorch.org/cppdocs/api/c10/index.html
Autograd: Automatic Differentiation https://docs.pytorch.org/cppdocs/api/autograd/index.html
CUDA Support https://docs.pytorch.org/cppdocs/api/cuda/index.html
XPU Support https://docs.pytorch.org/cppdocs/api/xpu/index.html
Neural Network Modules (torch::nn) https://docs.pytorch.org/cppdocs/api/nn/index.html
Optimizers (torch::optim) https://docs.pytorch.org/cppdocs/api/optim/index.html
Data Loading (torch::data) https://docs.pytorch.org/cppdocs/api/data/index.html
Serialization (torch::serialize) https://docs.pytorch.org/cppdocs/api/serialize/index.html
Torch Library API https://docs.pytorch.org/cppdocs/api/library/index.html
Torch Stable API https://docs.pytorch.org/cppdocs/api/stable/index.html
FAQ https://docs.pytorch.org/cppdocs/faq.html
Go to pytorch.org https://pytorch.org
Xhttps://x.com/PyTorch
GitHubhttps://github.com/pytorch/pytorch
PyTorch Forumhttps://discuss.pytorch.org/
PyPihttps://pypi.org/project/torch/
mainhttps://docs.pytorch.org/cppdocs/index.html
Installing C++ Distributions of PyTorch https://docs.pytorch.org/cppdocs/installing.html
The C++ Frontend https://docs.pytorch.org/cppdocs/frontend.html
C++ API Reference https://docs.pytorch.org/cppdocs/api/index.html
ATen: Tensor Library https://docs.pytorch.org/cppdocs/api/aten/index.html
C10: Core Utilities https://docs.pytorch.org/cppdocs/api/c10/index.html
Autograd: Automatic Differentiation https://docs.pytorch.org/cppdocs/api/autograd/index.html
CUDA Support https://docs.pytorch.org/cppdocs/api/cuda/index.html
XPU Support https://docs.pytorch.org/cppdocs/api/xpu/index.html
Neural Network Modules (torch::nn) https://docs.pytorch.org/cppdocs/api/nn/index.html
Optimizers (torch::optim) https://docs.pytorch.org/cppdocs/api/optim/index.html
Data Loading (torch::data) https://docs.pytorch.org/cppdocs/api/data/index.html
Serialization (torch::serialize) https://docs.pytorch.org/cppdocs/api/serialize/index.html
Torch Library API https://docs.pytorch.org/cppdocs/api/library/index.html
Torch Stable API https://docs.pytorch.org/cppdocs/api/stable/index.html
FAQ https://docs.pytorch.org/cppdocs/faq.html
Go to pytorch.org https://pytorch.org
Xhttps://x.com/PyTorch
GitHubhttps://github.com/pytorch/pytorch
PyTorch Forumhttps://discuss.pytorch.org/
PyPihttps://pypi.org/project/torch/
https://docs.pytorch.org/cppdocs/index.html
#https://docs.pytorch.org/cppdocs/frontend.html#the-c-frontend
#https://docs.pytorch.org/cppdocs/frontend.html#description
#https://docs.pytorch.org/cppdocs/frontend.html#end-to-end-example
the example repositoryhttps://github.com/pytorch/examples/tree/master/cpp
#https://docs.pytorch.org/cppdocs/frontend.html#philosophy
#https://docs.pytorch.org/cppdocs/frontend.html#installation
thishttps://pytorch.org/cppdocs/installing.html
previous Installing C++ Distributions of PyTorch https://docs.pytorch.org/cppdocs/installing.html
next C++ API Reference https://docs.pytorch.org/cppdocs/api/index.html
PyData Sphinx Themehttps://pydata-sphinx-theme.readthedocs.io/en/stable/index.html
previous Installing C++ Distributions of PyTorch https://docs.pytorch.org/cppdocs/installing.html
next C++ API Reference https://docs.pytorch.org/cppdocs/api/index.html
Descriptionhttps://docs.pytorch.org/cppdocs/frontend.html#description
End-to-end examplehttps://docs.pytorch.org/cppdocs/frontend.html#end-to-end-example
Philosophyhttps://docs.pytorch.org/cppdocs/frontend.html#philosophy
Installationhttps://docs.pytorch.org/cppdocs/frontend.html#installation
Show Source https://docs.pytorch.org/cppdocs/_sources/frontend.md.txt
ExecuTorchhttps://docs.pytorch.org/executorch
Helionhttps://docs.pytorch.org/helion
torchaohttps://docs.pytorch.org/ao
kinetohttps://github.com/pytorch/kineto
torchtitanhttps://github.com/pytorch/torchtitan
TorchRLhttps://docs.pytorch.org/rl
torchvisionhttps://docs.pytorch.org/vision
torchaudiohttps://docs.pytorch.org/audio
tensordicthttps://docs.pytorch.org/tensordict
PyTorch on XLA Deviceshttps://docs.pytorch.org/xla
View Docshttps://docs.pytorch.org/docs/stable/index.html
View Tutorialshttps://docs.pytorch.org/tutorials
View Resourceshttps://pytorch.org/resources
Privacy Policyhttps://www.linuxfoundation.org/privacy/
https://www.facebook.com/pytorch
https://twitter.com/pytorch
https://www.youtube.com/pytorch
https://www.linkedin.com/company/pytorch
https://pytorch.slack.com
https://pytorch.org/wechat
Policieshttps://www.linuxfoundation.org/legal/policies
Trademark Usagehttps://www.linuxfoundation.org/trademark-usage
Privacy Policyhttp://www.linuxfoundation.org/privacy
Cookies Policyhttps://opensource.fb.com/legal/cookie-policy
Sphinxhttps://www.sphinx-doc.org/
PyData Sphinx Themehttps://pydata-sphinx-theme.readthedocs.io/en/stable/index.html

Viewport: width=device-width, initial-scale=1


URLs of crawlers that visited me.