gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:30:04 30 4
gpt4 key购买 nike

有没有办法获取FileWriter对应的tensor名称,这样我就可以把总结写出来在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 可以得到很好的损失曲线。

enter image description here

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

#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;
}

关于c++ - 我如何使用 tensorFlow C++ API 中的 fileWrite 摘要在 Tensorboard 中查看它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48610803/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com