构建我自己的tf.Estimator,model_params如何覆盖model_dir? RuntimeWarning?

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

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

似乎TensorFlow本身有一个估算器类。所以我把TFLearn放在一边,我试图按照https://www.tensorflow.org/extend/estimators的指南。不知何故,我设法得到他们不属于的变量。谁能发现我的问题?我将发布代码和输出。

注意:当然,我可以看到输出顶部的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)

OUTPUT

/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的值。这是怎么发生在我的代码中的?我看不出来。

如果有人有意见或建议,我将非常感谢他们。谢谢!

python-3.x tensorflow tensorflow-estimator
1个回答
2
投票

只是因为当你构建你的model_param时,你正在喂你的model_dir作为Estimator

来自tensorflow documentation

Estimator __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)
© www.soinside.com 2019 - 2024. All rights reserved.