繁体   English   中英

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

[英]How I can use fileWrite summary in tensorFlow C++ API to view it in Tensorboard

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

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])

会给你一个空图。 您对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;
}

使用tensorboard --logdir .给你一个很好的损耗曲线tensorboard --logdir .

在此处输入图片说明

编辑添加直方图的方法可以相同:

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


void write_histogram(tensorflow::EventsWriter* writer, double wall_time, tensorflow::int64 step,
                     const std::string& tag, tensorflow::HistogramProto *hist) {
  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_allocated_histo(hist);
  writer->WriteEvent(event);


}

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

  std::string envent_file = "./events";
  tensorflow::EventsWriter writer(envent_file);


  // write histogram
  for (int time_step = 0; time_step < 150; ++time_step) {

    // a very simple histogram
    tensorflow::histogram::Histogram h;
    for (int i = 0; i < time_step; i++)
      h.Add(i);

    // convert to proto
    tensorflow::HistogramProto *hist_proto = new tensorflow::HistogramProto();
    h.EncodeToProto(hist_proto, true);

    // write proto
    write_histogram(&writer, time_step * 20, time_step, "some_hist", hist_proto);
  }

  return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM