Pytorch Cpp Tutorial

This blog post is about how to write a basic deep neural network in Cpp Pytorch.

saltbae_pytorch.jpg

I was wondering last weekend that how to train a Deep Neural Network using only Pytorch/TensorFlow C++. I know why we prefer python, because it is super easy to prototype in python. Python frontend of Pytorch or even Tensorflow 2.0 gives you great flexibility to iterate over your model architectures and designs. I would also recommend to use python whenever you can however there are some cases where you would need to write your models in Cpp. Now before I say anything else, keep in mind that python frontend talks to the Pytorch core(for any expensive tasks) which is written in cpp itself so python is not necessarily slow here.

Moving forward, I needed to decide whether I am going to write my simple Neural Network in Tensorflow or Pytorch. I did my “weekend”(read lazy) research and found out that in order to write model in Tensorflow, bazel is required So I dropped the idea of investigating more in Tensorflow. Note – I am not saying bazel or Tensorflow is bad, I simple wanted to do something quick.

However, I observed that Pytorch’s cpp frontend was extremely user friendly and it could be compiled with CMake so I went with pytorch. Now Let’s see how to write a simple neural network in pytorch. All the code related to this blog can be found here – https://github.com/vaibhawvipul/simpleNN-pytorch-cpp


First we set up our CMakeLists.txt  

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(simpleNN)

find_package(Torch REQUIRED)

set( SOURCES dataset.cpp dataset.h)

add_executable(simpleNN ${SOURCES} simpleNN.cpp)
target_link_libraries(simpleNN “${TORCH_LIBRARIES}”)
set_property(TARGET simpleNN PROPERTY CXX_STANDARD 11)

Before moving forward, you can download libtorch from here – https://pytorch.org/cppdocs/installing.html

Now we write our basic Neural Network –

struct Net : torch::nn::Module {

Net() {

// Construct and register two Linear submodules.
fc1 = register_module(“fc1”, torch::nn::Linear(4, 10));
fc2 = register_module(“fc2”, torch::nn::Linear(10, 3));

}

// Implement the Net’s algorithm.

torch::Tensor forward(torch::Tensor x) {

// Use one of many tensor manipulation functions.
x = torch::relu(fc1->forward(x.view({-1,4})));
x = torch::log_softmax(fc2->forward(x),1);

return x;
}

torch::nn::Linear fc1{nullptr}, fc2{nullptr};
};

As you can very well see the above code looks almost similar to python. We are defining couple of fully connected layers. And if you closely observe, you would see that we are calling net->forward(input) however in python we should call net(input). That is because in python frontend, pytorch inherently calls model.__call__(…). the __call__ method on nn.Module eventually calls forward along with taking care of tracing and hooks.

Also a general note, right now at::tensor is different from torch::tensor.  


Related to data, We are using iris dataset and interestingly I have written a csv data extractor in cpp. You can check the files here.


 

That was from my side folks, if there are any doubts then feel free to comment or email/tweet me.

Happy Coding! 🙂

2 thoughts on “Pytorch Cpp Tutorial

  1. Hey Vipul

    Nice blog post. I wanted to know if you can guide me to some pytorch tutorials and projects you can possible do with pytorch. Completely new to pytorch!!

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s