如何在不运行训练或评估的情况下使用 TensorFlow 的 Estimator API 在 TensorBoard 上可视化图形?
我知道当您有权访问 Graph 对象但无法找到 Estimator API 的任何内容时,如何使用 session API 实现这一点。
最佳答案
估算器为您创建和管理 tf.Graph
和 tf.Session
对象。因此,这些对象不容易访问。请注意,默认情况下,当您调用 estimator.train
时,图表会导出到事件文件内。
但是,您可以做的是在 tf.estimator
之外调用您的 model_function
,然后使用经典的 tf.summary.FileWriter()
导出图表。
这是一个代码片段,其中包含一个非常简单的估计器,仅将密集层应用于输入:
import tensorflow as tf
import numpy as np
# Basic input_fn
def input_fn(x, y, batch_size=4):
dataset = tf.data.Dataset.from_tensor_slices((x, y))
dataset = dataset.batch(batch_size).repeat(1)
return dataset
# Basic model_fn that just apply a dense layer to an input
def model_fn(features, labels, mode):
global_step = tf.train.get_or_create_global_step()
y = tf.layers.dense(features, 1)
increment_global_step = tf.assign_add(global_step, 1)
return tf.estimator.EstimatorSpec(
mode=mode,
predictions={'preds':y},
loss=tf.constant(0.0, tf.float32),
train_op=increment_global_step)
# Fake data
x = np.random.normal(size=[10, 100])
y = np.random.normal(size=[10])
# Just to show that the estimator works
estimator = tf.estimator.Estimator(model_fn=model_fn)
estimator.train(input_fn=lambda: input_fn(x, y), steps=1)
# Classic way of exporting the graph using placeholders and an outside call to the model_fn
with tf.Graph().as_default() as g:
# Placeholders
features = tf.placeholder(tf.float32, x.shape)
labels = tf.placeholder(tf.float32, y.shape)
# Creates the graph
_ = model_fn(features, labels, None)
# Export the graph to ./graph
with tf.Session() as sess:
train_writer = tf.summary.FileWriter('./graph', sess.graph)
关于tensorflow - 如何在不使用估计器 API 运行训练/评估的情况下可视化 TensorFlow 图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54772504/