TensorBoard标量摘要是单个数据点。如何解决?

时间:2019-04-02 12:39:11

标签: tensorflow tensorboard

tf.train.MonitoredSession

我正在使用tf.summary.scalar方法记录并使用tf.train.LoggingTensorHook记录某些张量。这就是tf.estimator.Estimator框架。

tf.train.LoggingTensorHook东西甚至没有显示AFAIK。其他内容正在显示,但显然没有时间步长。

图形和其他所有(权重)在张量板中看起来都很好。

更新:似乎多次调用train会在图形中产生结果。关于stepsevery_n_iter的某些东西没有按预期的方式交互?

import numpy as np
import tensorflow as tf

m = 10000
n = 5
X = np.random.randn(m, n)
A = np.random.randn(n)
y = X.dot(A) + np.random.randn(m) * 0.1

batch_size = 1024

def input_fn(batch_size):
    ds = tf.data.Dataset.from_tensor_slices(dict(X=X, y=y))
    ds = ds.repeat(-1)
    ds = ds.batch(batch_size)
    return ds

def model_fn(features, labels, mode, params):
    X = features['X']
    y = features['y']
    l = X
    for i, k in enumerate([32, 16, 16]):
        l = tf.layers.dense(inputs=l, units=k, name=f'l_{i}', activation=tf.nn.tanh)
    some_thing = tf.reduce_sum(l, axis=1, name='some_thing')
    l = tf.layers.dense(inputs=l, units=1, name='l_final')
    predictions = tf.squeeze(l, axis=-1)
    loss = tf.losses.mean_squared_error(y, predictions, weights=1.0)
    metric_ops = {"mse": tf.metrics.mean_squared_error(labels=y, predictions=predictions)}
    if mode == tf.estimator.ModeKeys.TRAIN:
        optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
        train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
        return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op, eval_metric_ops=metric_ops)
    if mode == tf.estimator.ModeKeys.PREDICT:
        predictions = {}
        return tf.estimator.EstimatorSpec(mode, predictions=predictions)
    if mode == tf.estimator.ModeKeys.EVAL:
        return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=metric_ops)
    raise Exception('should not hit this')

model = tf.estimator.Estimator(
        model_fn=model_fn,
        model_dir='/tmp/junk',
        config=None,
        params=dict(),
        warm_start_from=None
        )

tensors_to_log = dict(some_thing='some_thing')
logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=10)

train_input_fn = lambda: input_fn(batch_size)
test_input_fn = lambda: input_fn(batch_size)

train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn, hooks=[logging_hook], max_steps=100)
eval_spec = tf.estimator.EvalSpec(input_fn=test_input_fn, hooks=[logging_hook])
out = tf.estimator.train_and_evaluate(model, train_spec, eval_spec)

更新:这一点直到运行结束才在张量板上显示,然后也只显示一个点。

import numpy as np
import tensorflow as tf
# tf.enable_eager_execution()
tf.logging.set_verbosity(tf.logging.INFO)

m = 10000
n = 5
X = np.random.randn(m, n)
A = np.random.randn(n)
y = X.dot(A) + np.random.randn(m) * 0.1

steps = 1000
batch_size = 1024

def input_fn(repeat, batch_size):
    ds = tf.data.Dataset.from_tensor_slices(dict(X=X, y=y))
    ds = ds.repeat(repeat)
    ds = ds.batch(batch_size)
    return ds

def model_fn(features, labels, mode, params):
    X = features['X']
    y = features['y']
    l = X
    for i, k in enumerate([32, 16, 16]):
        l = tf.layers.dense(inputs=l, units=k, name=f'l_{i}', activation=tf.nn.tanh)
    some_thing = tf.reduce_sum(l, axis=1, name='some_thing')
    l = tf.layers.dense(inputs=l, units=1, name='l_final')
    predictions = tf.squeeze(l, axis=-1)
    loss = tf.losses.mean_squared_error(y, predictions, weights=1.0)
    metric_ops = {"mse": tf.metrics.mean_squared_error(labels=y, predictions=predictions)}

    tf.summary.scalar('summary_loss', loss) # plot a dist across the batch
    if mode == tf.estimator.ModeKeys.TRAIN:
        optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
        train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
        return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op, eval_metric_ops=metric_ops)
    if mode == tf.estimator.ModeKeys.PREDICT:
        predictions = {}
        return tf.estimator.EstimatorSpec(mode, predictions=predictions)
    if mode == tf.estimator.ModeKeys.EVAL:
        return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=metric_ops)
    raise Exception('should not hit this')

model = tf.estimator.Estimator(
        model_fn=model_fn,
        model_dir='/tmp/junk',
        config=None,
        params=dict(),
        warm_start_from=None
        )

tensors_to_log = dict(some_thing='some_thing')
logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=10)

train_input_fn = lambda: input_fn(steps, batch_size)
test_input_fn = lambda: input_fn(steps, batch_size)

train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn, hooks=[logging_hook], max_steps=None)
eval_spec = tf.estimator.EvalSpec(input_fn=test_input_fn, hooks=[logging_hook])
out = tf.estimator.train_and_evaluate(model, train_spec, eval_spec)

1 个答案:

答案 0 :(得分:1)

我遇到了一个非常相似的问题。我很惊讶该解决方案非常简单。关闭TensorBoard并再次启动,然后等待几分钟。赶上需要时间。由于某些原因,如果您在培训期间启动TensorBoard,它将卡住。我希望这将有所帮助。 我正在Google云上运行代码

from google.datalab.ml import TensorBoard
TensorBoard().start('gs://{}/directoy_where_my_models_are'.format(BUCKET))

enter image description here