tensorflow使用summary收集tensor数据

博主在用tensorflow训练三维卷积神经网络过程中,希望将网络validation的结果和真实的标签做对比,一开始以为要使用tf.summary.histogram函数,在网上检索了一下以后,在网站上后来使用summary的迭代器和tensorboard:

# This example supposes that the events file contains summaries with a
# summary value tag 'loss'.  These could have been added by calling
# `add_summary()`, passing the output of a scalar summary op created with
# with: `tf.scalar_summary(['loss'], loss_tensor)`.
for e in tf.train.summary_iterator(path_to_events_file):
    for v in e.summary.value:
        if v.tag == 'loss' or v.tag == 'accuracy':
            print(v.simple_value)
tensorboard --inspect --event_file=myevents.out --tag=loss

将数据(因为在实验过程中,所以数据量不大)全打印出来了以后:


发现应该有对应的办法去保存tensor变量,百度了一下以后便发现了tf.summary.tensor_summary,同时在在tensorflow官网中检索到相应的接口将日志文件中的数据迭代输出,却发现:


张量的数据是二进制文件,tensorflow使用summary收集tensor数据存储在日志文件中,但是tensorflow官方文档似乎没有提供读取tensor型二进制数据的接口,在必应中用英文检索到stackoverflow文章,该文章中博主给出了一个函数:

def tensor_summary_value_to_variable(value):
fb = numpy.frombuffer(v.tensor.tensor_content, dtype = numpy.float32)
shape = []
for d in v.tensor.tensor_shape.dim:
    shape.append(d.size)
fb = fb.reshape(shape)
var = tf.Variable(fb)
return var
便成功解决了

猜你喜欢

转载自blog.csdn.net/little_kid_pea/article/details/79199090