【问题标题】:In TensorFlow's C++ api, how to generate a graph file to visualization with tensorboard?在 TensorFlow 的 C++ api 中,如何使用 tensorboard 生成图形文件进行可视化?
【发布时间】:2017-08-30 03:59:56
【问题描述】:

有一种方法可以用 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 接口,但我没有看到任何示例。是同一个界面吗?

【问题讨论】:

    标签: api tensorflow tensorboard


    【解决方案1】:

    请参阅我的answer here,它为您提供了一个 26-liner in 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;
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2020-01-22
        • 1970-01-01
        • 2018-04-13
        • 2021-02-24
        • 2020-01-26
        • 2020-08-26
        • 2021-06-16
        • 2016-03-28
        • 2020-03-09
        相关资源
        最近更新 更多