Tensorflow 估计器 InvalidArgumentError

Tensorflow Estimator InvalidArgumentError

我正在尝试找到一种方法来查找和修复我的 TF 代码中的错误。下面的代码片段成功训练了模型,但在调用最后一行 (model.evaluate(input_fn)) 时生成以下错误:

InvalidArgumentError: Restoring from checkpoint failed. This is most likely due to a mismatch between the current graph and the graph from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:
/var/folders/kx/y9syv3f91b1c6tzt3fgzc7jm0000gn/T/tmp_r6c94ni/model.ckpt-667.data-00000-of-00001; Invalid argument
     [[node save/RestoreV2 (defined at ../text_to_topic/train/nn/nn_tf.py:266)  = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_INT64], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/RestoreV2/tensor_names, save/RestoreV2/shape_and_slices)]]

Caused by op 'save/RestoreV2', defined at:
  File "/Users/foo/miniconda3/envs/tt/lib/python3.6/runpy.py", line 193, in _run_module_as_main

完全相同的代码在用于 MNIST 数据集时有效,但在用于我自己的数据集时不起作用。我该如何调试或可能是什么原因。从检查点恢复模型后,图表似乎不匹配,但我不确定如何继续解决此问题。我试过 TF 版本 1.11 和 1.13

model = tf.estimator.Estimator(get_nn_model_fn(num_classes))

# Define the input function for training
input_fn = tf.estimator.inputs.numpy_input_fn(
    x=X_train, y=y_train,
    batch_size=batch_size,
    num_epochs=None, shuffle=True)

# Train the Model
model.train(input_fn, steps=num_steps)

# Evaluate the Model
# Define the input function for evaluating
input_fn = tf.estimator.inputs.numpy_input_fn(
    x=X_test, y=y_test,
    batch_size=batch_size, shuffle=False)

# Use the Estimator 'evaluate' method
e = model.evaluate(input_fn) 

当您修改图形的某些部分时,通常会发生此错误,例如更改隐藏层或 remove/add 某些层的大小,估算器会尝试加载之前的检查点。您有两种解决问题的方法:

1) 更改模型目录 (model_dir):

config = tf.estimator.RunConfig(model_dir='./NEW_PATH/', ) # new path
model_estimator = tf.estimator.Estimator(model_fn=model_fn, config=config)

2) 删除之前保存在模型目录中的检查点(model_dir)。


你确定图表没有被改动过?

可以肯定的是,新数据集与以前的 Data-type 相同。如果您之前为输入加载浮点数,在新数据集中它们也应该是浮点数。