在TensorFlow的C++中api,如何生成图形文件用tensorboard可视化?

In TensorFlow's C++ api, how to generate a graph file to visualization with tensorboard?

有一种使用Python创建文件的方法,可以通过TensorBoard可视化(参见here)。我试过这段代码,效果很好。

import tensorflow as tf

a = tf.add(1, 2,)
b = tf.multiply(a, 3)
c = tf.add(4, 5,)
d = tf.multiply(c, 6,)
e = tf.multiply(4, 5,)
f = tf.div(c, 6,)
g = tf.add(b, d)
h = tf.multiply(g, f)

with tf.Session() as sess:
    print(sess.run(h))
with tf.Session() as sess:
    writer = tf.summary.FileWriter("output", sess.graph)
    print(sess.run(h))
    writer.close()

现在我正在使用 TensorFlow API 来创建我的计算。 如何使用 TensorBoard 可视化我的计算?

在 C++ api 中也有一个 FileWrite 接口,但我没有看到任何例子。是一样的界面吗?

看起来你想要 tensorflow/core/util/events_writer.htensorflow::EventsWriter。不过,您需要手动创建一个事件对象才能使用它。

tf.summary.FileWriter 中的 python 代码为您处理了很多细节,但我建议仅在绝对必要时才使用 C++ API...在 C++ 中实施培训的令人信服的理由?

请参阅我的 answer here,它为您提供了一个 26 行的 C++ 代码来执行此操作:

#include <tensorflow/core/util/events_writer.h>
#include <string>
#include <iostream>


void write_scalar(tensorflow::EventsWriter* writer, double wall_time, tensorflow::int64 step,
                  const std::string& tag, float simple_value) {
  tensorflow::Event event;
  event.set_wall_time(wall_time);
  event.set_step(step);
  tensorflow::Summary::Value* summ_val = event.mutable_summary()->add_value();
  summ_val->set_tag(tag);
  summ_val->set_simple_value(simple_value);
  writer->WriteEvent(event);
}


int main(int argc, char const *argv[]) {

  std::string envent_file = "./events";
  tensorflow::EventsWriter writer(envent_file);
  for (int i = 0; i < 150; ++i)
    write_scalar(&writer, i * 20, i, "loss", 150.f / i);

  return 0;
}