我如何在 tensorFlow C++ API 中使用 fileWrite 摘要在 Tensorboard 中查看它

Jia*_*Luo 4 c++ tensorflow

无论如何我可以获得与 FileWriter 对应的张量名称,以便我可以写出我的摘要以在 Tensorboard 中查看它们?我的应用程序是基于 C++ 的,所以我必须使用 C++ 进行培训。

Pat*_*wie 5

FileWriter 不是张量。

import tensorflow as tf

with tf.Session() as sess:
    writer = tf.summary.FileWriter("test", sess.graph)
    print([n for n in tf.get_default_graph().as_graph_def().node])
Run Code Online (Sandbox Code Playgroud)

会给你一个空图。您对 EventsWriter 感兴趣。(https://github.com/tensorflow/tensorflow/blob/994226a4a992c4a0205bca9e2f394cb644775ad7/tensorflow/core/util/events_writer_test.cc#L38-L52)。

一个最小的工作示例是

#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;
}
Run Code Online (Sandbox Code Playgroud)

这为您提供了一个很好的损失曲线 tensorboard --logdir .

在此处输入图片说明

编辑 添加直方图可以用同样的方式完成:

import tensorflow as tf

with tf.Session() as sess:
    writer = tf.summary.FileWriter("test", sess.graph)
    print([n for n in tf.get_default_graph().as_graph_def().node])
Run Code Online (Sandbox Code Playgroud)