【问题标题】:Train and validate using tensorflow estimator使用 tensorflow 估计器进行训练和验证
【发布时间】:2018-06-06 15:45:22
【问题描述】:

我使用 tf.estimator API 创建了一个浅层神经网络。我想要类似于在 TensorFlow 开发峰会 https://www.youtube.com/watch?time_continue=948&v=eBbEDRsCmv4 中解释的超参数搜索。

我找不到任何有关如何执行此操作的更新文档。我有以下代码(我会尽量简化):

# Define nn architecture
def neural_net(features):
    input_layer = tf.cast(features['x'], tf.float32)
    hidden_layer = nn_layer(input_layer, 10, 'hidden_layer', act=tf.nn.relu)
    out_layer = nn_layer(hidden_layer, 2, 'out_layer', act=tf.nn.relu)
    return out_layer

# Define model function
def model_fn(features, labels, mode):
    # Build the neural network
    logits = neural_net(features<9


    with tf.name_scope('loss'):
    # Define loss and optimizer
        loss = tf.losses.sparse_softmax_cross_entropy(logits=logits, labels=labels)

    # Configure the Training
    if mode == tf.estimator.ModeKeys.TRAIN:
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
        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)


nn_classifier = tf.estimator.Estimator(model_fn=model_fn)


train_input_fn = tf.estimator.inputs.numpy_input_fn(
            x={"x": train_data},
            y=train_labels,
            batch_size=100,
            num_epochs=None,
            shuffle=True)

nn_classifier.train(
        input_fn=train_input_fn,
        steps=20000
        )

执行此代码,我可以获得损失的摘要并在 Tensorboard 中观察它。但是想象一下我想获得不同的曲线。假设我想看看损失如何随着样本数量的变化而变化,所以我会训练两个具有不同样本大小的模型。或者两个具有不同架构的模型......随便。

如何在 Tensorboard 中获得这两条曲线?

【问题讨论】:

    标签: python tensorflow machine-learning neural-network


    【解决方案1】:

    将所有模型保存到父目录的子目录中,并将该父目录作为 tensorboard 中的 logdir 传递;这会将所有曲线放在同一个图中。

    【讨论】:

      【解决方案2】:

      您可以简单地运行任何您想要的架构。验证和训练数据将保存在您的工作文件夹中,并可在 TensorBoard 中查看。您可以为您运行的所有模型架构选择过滤器。

      【讨论】:

        猜你喜欢
        • 2018-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-04
        • 1970-01-01
        • 1970-01-01
        • 2019-06-05
        • 2020-05-11
        相关资源
        最近更新 更多