我如何在 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 不是张量.
将 tensorflow 导入为 tf使用 tf.Session() 作为 sess:writer = tf.summary.FileWriter("test", sess.graph)打印([n for n in tf.get_default_graph().as_graph_def().node])
会给你一个空图.您对 EventsWriter 感兴趣.(
编辑添加直方图可以用同样的方式完成:
#include #include #include <字符串>#include #include void write_histogram(tensorflow::EventsWriter* writer, double wall_time, tensorflow::int64 step,const std::string&标签,tensorflow::HistogramProto *hist) {张量流::事件事件;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(事件);}int main(int argc, char const *argv[]) {std::string envent_file = "./events";tensorflow::EventsWriter writer(envent_file);//写直方图for (int time_step = 0; time_step <150; ++time_step) {//一个非常简单的直方图张量流::直方图::直方图h;for (int i = 0; i < time_step; i++)h.添加(i);//转换为原型tensorflow::HistogramProto *hist_proto = new tensorflow::HistogramProto();h.EncodeToProto(hist_proto, true);//写原型write_histogram(&writer, time_step * 20, time_step, "some_hist", hist_proto);}返回0;}
Is there anyway I can get the tensor name corresponding to FileWriter so that I can write my summary out to view them in Tensorboard? My application is C++ based, so I have to use C++ to do training.
The FileWriter is not a tensor.
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])
will give you an empty graph. You are interested in the EventsWriter. (https://github.com/tensorflow/tensorflow/blob/994226a4a992c4a0205bca9e2f394cb644775ad7/tensorflow/core/util/events_writer_test.cc#L38-L52).
A minimal working example is
#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;
}
This gives you a nice loss curve using tensorboard --logdir .
edit Adding a histogram can be done in the same way:
#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;
}
这篇关于我如何在 tensorFlow C++ API 中使用 fileWrite 摘要在 Tensorboard 中查看它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!