pytorch-cpp: Introduction, Linear Regression & Performance

PyTorch supports C++?

One word answer, YES!

Again, YES!

So what? C++ API is very similar and concise like Python. Want to see? Read this post then!

Also, look out for the example links for some C++ code to play with!

At the moment, the C++ API is currently in beta and is under major breaking changes but Facebook internally uses LibTorch (PyTorch C++ API) both in research and production. PyTorch C++ API has a very good documentation available here.

It is packed with 3 major components:

It also has TorchScript and C++ Extensions if you want to play with TorchScript JIT compiler and extend Python API with CUDA routines.

How can I try PyTorch in C++? What is pytorch-cpp?

Last year, I started playing with C++ API of PyTorch and I fell in love with its simplicity. I had a fair amount of C++ experience as it was my primary language for Competitive Programming and during ACM ICPC. I was able to pick up quickly through the docs but soon I felt like I needed a TODO list of topics to play with this C++ API. I found this amazing list of pytorch tutorials for Python and I thought how about creating pytorch-cpp, a C++ implementation of PyTorch tutorials for Deep Learning Researchers. Soon, I started collaborating with Markus on daily basis for adding more intermediate and advanced tutorials. (Thanks to Markus as I was able to take this project ahead as I was busy with my day job).

So…Its very easy to get started with PyTorch in C++. Just make sure you have all these requirements and you have forked/cloned pytorch-cpp and follow the options given in README to setup your environment.

Steps for running Linear Regression tutorial:

Voila! This will automatically download libtorch for you and compile and run Linear Regression in C++!

Linear Regression using Python and C++ API



I know right?

Lets talk numbers?

I just ran both above given Python and C++ programs and /usr/bin/time on my Macbook Pro i5 8GB RAM and here are the results:


61.11 real        54.86 user         5.06 sys


31.42 real        27.81 user         3.24 sys

Liked this post? Stay tuned… I got some more exciting C++ API features to share with you!

If you have any queries or facing troubles with the tutorials, please mark them on GitHub Issues (Also if you want to contribute, do send a PR!)

If you got any suggestions shoot me an email and I will definitely get back to you :)

I am becoming seriously allergic to 500-pound websites.