更新时间:2023-12-02 19:38:16
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
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;
}