Easy to use well documented c++ tensorflow api

2,879 views
Skip to first unread message

Dailos Guerra

unread,
Jan 31, 2017, 9:10:27 AM1/31/17
to Discuss
I feel far more comfortable using c++ than python.
I've read that in the official webpage that the library is going to be supported by the both languages Python and C++ but I don't see any usable "hello world" c++ only example. 
A simple linear regression algorithm in tensorflow with a few lines of code as in python would suffice.

I've seen the c++ documentation for tensorflow is very sparse despite the fact that the library is internally build with that language.
  • Building a graph is only possible with Python.
  • Optimizer functions are not as easy to use in c++ as in Python.
  • Many other useful features such batch norm, dropout are not available for c++.

Do you think it is worth waiting for a whole c++ api to be developed or this is not going to happen any time soon?

basuam

unread,
Jan 31, 2017, 10:38:26 AM1/31/17
to Discuss
I think it is not worth it because Python is a language with a higher abstraction and allows the implementation of other interfaces/programs easily (e.g. Keras, Magenta)

I like C++ (was my first programming language I learned) but so far Python looks more useful for allowing the use of other tools which expands the power of Tensorflow even to other areas.

Martin Wicke

unread,
Jan 31, 2017, 10:54:06 AM1/31/17
to Dailos Guerra, Discuss
The C++ API is coming along. It is already possible to build graphs.

It does not offer, and will not, for the foreseeable future, offer the same amount of stuff as the python API. 

That said, it should be good enough for most use cases within months, if you're willing to live without higher level things like layers etc. 

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.
To post to this group, send email to dis...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/602f8bf3-3345-4644-b37f-6499a61c015c%40tensorflow.org.

dr...@drorm.com

unread,
Jun 27, 2017, 11:40:19 AM6/27/17
to Discuss
Hello Dailos,
Any news regarding this issue? I'm too struggle to understand the API in C++, currently I'm reading the source code and some of the comments inside do help...
Do you know where can I find simple examples for setting ::tensorflow::Variable values with my own random number generator?
And where can I find examples for executing the Adam optimizer with the C++ API?

Thank you!

Dendi Suhubdy

unread,
Jun 27, 2017, 11:47:20 AM6/27/17
to dr...@drorm.com, Discuss
Check this out https://github.com/cjweeks/tensorflow-cmake

Dendi 
CS Ph.D Student
Université de Montréal

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.
To post to this group, send email to dis...@tensorflow.org.

dr...@drorm.com

unread,
Jun 28, 2017, 3:20:03 AM6/28/17
to Discuss
The link related to cmake in GitHub deals with the installation: Downloading, extracting, compiling, linking and integrating the tensorflow libraries into a C++ product.

However, the subject of this discussion is about the missing documentation and tutorial for building and training a computational graph.

Dailos Guerra Ramos

unread,
Jul 3, 2017, 9:43:03 AM7/3/17
to dr...@drorm.com, Discuss
Hi!, I'm still looking for the examples in C++. 

A simple convolutional neural network construction and training would be a good starting point for me.
Reading data, saving network state, tensorboard, etc...

Like the one written in Python from the earliest versions of tensorflow in their webpage.

I see in the official C++ API tensorflow page that there are already many functions and classes defined for the language.
But I cannot find examples anywhere.

I'm eager to start using c++ API intensively but I don't see much documentation so far.
Best regards,
Dailos


On Wed, Jun 28, 2017 at 8:20 AM, <dr...@drorm.com> wrote:
The link related to cmake in GitHub deals with the installation: Downloading, extracting, compiling, linking and integrating the tensorflow libraries into a C++ product.

However, the subject of this discussion is about the missing documentation and tutorial for building and training a computational graph.
--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+unsubscribe@tensorflow.org.

To post to this group, send email to dis...@tensorflow.org.

Dror Meirovich

unread,
Jul 3, 2017, 10:47:28 AM7/3/17
to Dailos Guerra Ramos, Discuss
Currently I'm working on a sort of autoencoder,  and I'm trying to make sense from the very few examples on the web.
You can view my progress here:



I didn't run it even once so I'm not sure that it works at all...

Dmitry

unread,
Jul 4, 2017, 8:24:49 AM7/4/17
to Dror Meirovich, Dailos Guerra Ramos, Discuss
For now I can only share

On Mon, Jul 3, 2017 at 5:47 PM Dror Meirovich <dr...@drorm.com> wrote:
Currently I'm working on a sort of autoencoder,  and I'm trying to make sense from the very few examples on the web.
You can view my progress here:



I didn't run it even once so I'm not sure that it works at all...
On Jul 3, 2017 16:42, "Dailos Guerra Ramos" <dailos....@gmail.com> wrote:
Hi!, I'm still looking for the examples in C++. 

A simple convolutional neural network construction and training would be a good starting point for me.
Reading data, saving network state, tensorboard, etc...

Like the one written in Python from the earliest versions of tensorflow in their webpage.

I see in the official C++ API tensorflow page that there are already many functions and classes defined for the language.
But I cannot find examples anywhere.

I'm eager to start using c++ API intensively but I don't see much documentation so far.
Best regards,
Dailos

On Wed, Jun 28, 2017 at 8:20 AM, <dr...@drorm.com> wrote:
The link related to cmake in GitHub deals with the installation: Downloading, extracting, compiling, linking and integrating the tensorflow libraries into a C++ product.

However, the subject of this discussion is about the missing documentation and tutorial for building and training a computational graph.

--

You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.

To post to this group, send email to dis...@tensorflow.org.

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.

To post to this group, send email to dis...@tensorflow.org.
--
Sent from Inbox By Gmail

Dmitry

unread,
Jul 5, 2017, 5:22:01 AM7/5/17
to Dror Meirovich, Dailos Guerra Ramos, Discuss
It looks like I have found something interesting and high-level, take a look at

namespace tensorflow {
namespace grappler {

// A cluster represents of collection of hardware resources available to run
// the TensorFlow model.
// A process can only create a single cluster at a time.
class Cluster ...

or

namespace tensorflow {
namespace grappler {
// A TensorFlow model to optimize.
// Models are represented by the combination of a graph, one of more fetch
// nodes, and potentially a set of nodes to feed.
// TODO(volunteer_needed): turn this struct into a class.
struct GrapplerItem ...

Dmitry

unread,
Jul 5, 2017, 6:16:18 AM7/5/17
to Dror Meirovich, Dailos Guerra Ramos, Discuss
And here is something about gradients:
tensorflow/tensorflow/cc/framework/gradients.h
tensorflow/tensorflow/core/graph/gradients.h

Dailos Guerra Ramos

unread,
Jul 9, 2017, 4:10:22 AM7/9/17
to Dmitry, Dror Meirovich, Discuss
Hi, 
In order to initialize a variable I had to run the graph twice.
First only run the branch of the graph that assigns values to variables and then execute the graph as usual.

#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"
int main()
{
  using namespace tensorflow;
  using namespace tensorflow::ops;
  Scope root = Scope::NewRootScope();

  //Sum one to the current value of tt and updates the content
  auto tt = Variable(root.WithOpName("tt"), {}, DT_DOUBLE);
  auto plus_one = Add(root, tt, 1.0);
  auto update = Assign(root, tt, plus_one);
  
  //Initialize the variable to value 2.9
  auto init_t = Assign(root, tt, 2.9);


  std::vector<Tensor> outputs;
  ClientSession session(root);
  session.Run({init_t}, &outputs);

  for(unsigned int i=0;i<10;++i)
  {
    TF_CHECK_OK(session.Run({update}, &outputs));
    LOG(INFO) << outputs[0].scalar<double>();
  }
  return 0;
}


To unsubscribe from this group and stop receiving emails from it, send an email to discuss+unsubscribe@tensorflow.org.

To post to this group, send email to dis...@tensorflow.org.

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+unsubscribe@tensorflow.org.
--
Sent from Inbox By Gmail
--
Sent from Inbox By Gmail
--
Sent from Inbox By Gmail

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+unsubscribe@tensorflow.org.

To post to this group, send email to dis...@tensorflow.org.

alberts...@gmail.com

unread,
Oct 20, 2017, 1:21:26 PM10/20/17
to Discuss, dr...@drorm.com, dailos....@gmail.com


On Monday, July 3, 2017 at 6:43:03 AM UTC-7, Dailos Guerra Ramos wrote:
Hi!, I'm still looking for the examples in C++. 

A simple convolutional neural network construction and training would be a good starting point for me.
Reading data, saving network state, tensorboard, etc...

What you want to do is VERY easy if you use Keras.   Not only is the Python API at a higher level of abstraction Keras moves the Python abstraction level up even higher.   

I use C++ for many things but I use Python because of the libraries and the speed at which I can get things done.

That said, dynamic types languages are not good for large scale software development and there is some slight performance issues with Python and some big performance issues if you abuse python (by doing things like using a for loop to iterates over an array) 

So write in Python that as you transition to production from experimental prototype, use Cython

Thomas Ho

unread,
Feb 8, 2018, 1:15:21 PM2/8/18
to Discuss, dailos....@gmail.com
I have written a sample program that build and train a CNN using only Tensorflow C++. The code can be found in https://github.com/tho15/tfplusplus

Rock Zhuang

unread,
Jan 5, 2019, 8:08:20 AM1/5/19
to Discuss, dailos....@gmail.com
Some C++ examples such as dataset, rnn, dnn, training, freeze/save/restore model here: https://github.com/rockzhuang/tensorflow
It's still going on. 

Reply all
Reply to author
Forward
0 new messages