构建我自己的 tf.Estimator,model_params 是如何覆盖 model_dir 的?运行时警告?

Building my own tf.Estimator, how did model_params overwrite model_dir? RuntimeWarning?

最近我使用 TFLearn 构建了一个定制的深度神经网络模型,它声称将深度学习引入 scikit-learn 估计器 API。我可以训练模型并做出预测,但我无法让评分(评估)功能发挥作用,所以我无法进行交叉验证。我试图在各个地方询问有关 TFLearn 的问题,但没有得到任何回应。

看来 TensorFlow 本身就有一个估计器 class。所以我把 TFLearn 搁置一旁,并尝试按照 https://www.tensorflow.org/extend/estimators 上的指南进行操作。我设法以某种方式获取不属于它们的变量。谁能发现我的问题?我将 post 代码和输出。

注意:当然,我可以在输出的顶部看到 RuntimeWarning。我在网上找到了对这个警告的引用,但到目前为止每个人都声称它是无害的。也许不是...

代码:

import tensorflow as tf
from my_library import Database, l2_angle_distance


def my_model_function(topology, params):

    # This function will eventually be a function factory.  This should
    # allow easy exploration of hyperparameters.  For now, this just
    # returns a single, fixed model_fn.

    def model_fn(features, labels, mode):

        # Input layer
        net = tf.layers.conv1d(features["x"], topology[0], 3, activation=tf.nn.relu)
        net = tf.layers.dropout(net, 0.25)
        # The core of the network is here (convolutional layers only for now).
        for nodes in topology[1:]:
            net = tf.layers.conv1d(net, nodes, 3, activation=tf.nn.relu)
            net = tf.layers.dropout(net, 0.25)
        sh = tf.shape(features["x"])
        net = tf.reshape(net, [sh[0], sh[1], 3, 2])
        predictions = tf.nn.l2_normalize(net, dim=3)

        # PREDICT EstimatorSpec
        if mode == tf.estimator.ModeKeys.PREDICT:
            return tf.estimator.EstimatorSpec(mode=mode,
                    predictions={"vectors": predictions})

        # TRAIN or EVAL EstimatorSpec
        loss = l2_angle_distance(labels, predictions)
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=params["learning_rate"])
        train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
        return tf.estimator.EstimatorSpec(mode, predictions, loss, train_op)

    return model_fn

##===================================================================

window = "whole"
encoding = "one_hot"
db = Database("/home/bwllc/Documents/Files for ML/compact")

traindb, testdb = db.train_test_split()
train_features, train_labels = traindb.values(window, encoding)
test_features, test_labels = testdb.values(window, encoding)

# Create the model.
tf.logging.set_verbosity(tf.logging.INFO)
LEARNING_RATE = 0.01
topology = (60,40,20)
model_params = {"learning_rate": LEARNING_RATE}
model_fn = my_model_function(topology, model_params)
model = tf.estimator.Estimator(model_fn, model_params)
print("\nmodel_dir?  No?  Why not? ", model.model_dir, "\n")  # This documents the error

# Input function.
my_input_fn = tf.estimator.inputs.numpy_input_fn({"x" : train_features}, train_labels, shuffle=True)

# Train the model.
model.train(input_fn=my_input_fn, steps=20)

输出

/usr/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
  return f(*args, **kwds)
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_model_dir': {'learning_rate': 0.01}, '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7f0b55279048>, '_task_type': 'worker', '_task_id': 0, '_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}

model_dir?  No?  Why not?  {'learning_rate': 0.01} 

INFO:tensorflow:Create CheckpointSaverHook.
Traceback (most recent call last):
  File "minimal_estimator_bug_example.py", line 81, in <module>
    model.train(input_fn=my_input_fn, steps=20)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/estimator/estimator.py", line 302, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/estimator/estimator.py", line 756, in _train_model
    scaffold=estimator_spec.scaffold)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/basic_session_run_hooks.py", line 411, in __init__
    self._save_path = os.path.join(checkpoint_dir, checkpoint_basename)
  File "/usr/lib/python3.6/posixpath.py", line 78, in join
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not dict

------------------
(program exited with code: 1)
Press return to continue

我可以确切地看到哪里出了问题,model_dir(我保留为默认值)以某种方式绑定到我为 model_params 设定的值。这在我的代码中是如何发生的?我看不到。

如果有人有意见或建议,我将不胜感激。谢谢!

只是因为当您构建 Estimator.

时,您将 model_param 作为 model_dir 喂养

来自 tensorflow documentation :

估计器__init__函数:

__init__(
    model_fn,
    model_dir=None,
    config=None,
    params=None
)

注意第二个参数是 model_dir。如果只想指定 params 一个,则需要将其作为关键字参数传递。

model = tf.estimator.Estimator(model_fn, params=model_params)

或指定所有先前的位置参数:

model = tf.estimator.Estimator(model_fn, None, None, model_params)