Tensorboard不会显示估算器的任何标量摘要

问题描述 投票:0回答:1

按照tf custom estimator的说明操作

我创建了一个cnn估算器并试图训练它。在训练时,我初步确定了张量板,并希望看到有关训练步骤的一些可视化。但是,tensorboard只显示了我的自定义估算器的图形,但没有显示我定义的标量值。

这大致是我在代码中所拥有的

def model_fn(features, labels, mode, params=None):
    tf.logging.set_verbosity(tf.logging.INFO)
    n_classes = params['n_classes']
    base_learning_rate = params['learning_rate']
    decay_rate = params['decay_rate']
    embedding_dim = params['embedding_dim']

    x = VGG_block1(features, (3, 3), 64, name='block1_1')
    x = VGG_block1(x, (3, 3), 128, name='block1_2')
    x = VGG_block1(x, (3, 3), 256, name='block1_3', regularizer=tf.contrib.layers.l1_regularizer(.1))
    x = VGG_block2(x, (3, 3), 512, name='block2_4')
    x = VGG_block2(x, (3, 3), 1024, name='block2_5')
    x = conv2d(x, 512, (5, 5), padding='valid', normalizer_fn=batch_norm, activation_fn=tf.nn.leaky_relu,
               weights_initializer=he_uniform())
    x = flatten(x)
    embedding = fully_connected(x, embedding_dim)
    logits = fully_connected(embedding, n_classes)

    # make predictions
    predictions = {
        'classes': tf.argmax(logits, axis=1, name='classes'),
        'probabilities': tf.nn.softmax(logits, name='softmax'),
        'embeddings':embedding
    }

    # if we are in prediction mode
    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

    # otherwise define losses for training
    c_loss, center = center_loss(embedding, labels, .9, n_classes)
    xent_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits))
    total_loss = xent_loss + 0.5 * c_loss

    # evaluation methods
    accuracy, update_op = tf.metrics.accuracy(labels=labels, predictions=predictions['classes'], name='accuracy')
    batch_acc = tf.reduce_mean(tf.cast(tf.equal(tf.cast(labels, tf.int64), predictions['classes']), tf.float32))
    tf.summary.scalar('batch_acc', batch_acc)
    tf.summary.scalar('streaming_acc', update_op)
    tf.summary.scalar('total_loss', total_loss)
    tf.summary.scalar('center_loss', c_loss)
    tf.summary.scalar('xent_loss', xent_loss)

    # training mode
    if mode  == tf.estimator.ModeKeys.TRAIN:
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
        global_step = tf.Variable(0, trainable=False)
        global_step_op = tf.assign(global_step, global_step + 1)
        learning_rate = tf.train.exponential_decay(base_learning_rate, global_step, 8000, decay_rate, staircase=True)
        optimizer = tf.train.AdamOptimizer(learning_rate)
        with tf.control_dependencies(update_ops+[global_step_op]):
            objective = optimizer.minimize(total_loss)

        return tf.estimator.EstimatorSpec(mode=mode, loss=total_loss, train_op=objective)

    eval_metric_ops = {
        'accuracy': (accuracy, update_op)
    }
    return tf.estimator.EstimatorSpec(mode=mode, loss=total_loss, eval_metric_ops=eval_metric_ops)

X_train, X_test, y_train, y_test = load_data()

epochs = 10
batch_size = 64
n_classes = len(classes)

model_params = {'n_classes':n_classes,
                'learning_rate':0.0001,
                'decay_rate':0.5,
                 'embedding_dim':128}
model_dir = 'output'
face_classifier = tf.estimator.Estimator(model_fn=model_fn, params=model_params, model_dir=model_dir)

我的Tensorflow版本是1.12.0

编辑忘了提及我正在使用热切的执行来进行此练习,原因不明是造成此错误的原因

python tensorflow tensorboard tensorflow-estimator
1个回答
0
投票

正如在编辑中提到的,禁用急切执行解决了问题

© www.soinside.com 2019 - 2024. All rights reserved.