实施 tensorflow 高级时出错 api

Error when implementing tensorflow high level api

我正在尝试实现提供高级 api 的张量流,特别是基线分类器。但是,在尝试训练模型时,我得到以下结果

错误:

NotFoundError (see above for traceback): Key baseline/bias not found in checkpoint
     [[Node: save/RestoreV2 = RestoreV2[dtypes=[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)]]

代码:

import tensorflow as tf
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split

def digit_cross():
    # Number of classes, one class for each of 10 digits.
    num_classes = 10

    digit = datasets.load_digits()
    x = digit.data
    y = digit.target
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.3, random_state=42)
    y_train_index = np.arange(y_train.size)

    train_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": np.array(x_train)},
        y=np.array(y_train),
        num_epochs=None,
        shuffle=False)

    # Build BaselineClassifier
    classifier = tf.estimator.BaselineClassifier(n_classes=num_classes,
                                                 model_dir="./checkpoints_tutorial17-1/")

    # Fit model.
    classifier.train(train_input_fn)

digit_cross()

您似乎在 model_dir="./checkpoints_tutorial17-1/" 中有一个检查点,它来自另一个模型,而不是来自 BaselineClassifier。具体来说,您在该文件夹中有一个检查点文件和 model.ckpt-* 文件。

正如 tensorflow 记录的那样:

  • model_dir: Directory to save model parameters, graph and etc. This can also be used to load checkpoints from the directory into a estimator to continue training a previously saved model. If PathLike object, the path will be resolved. If None, the model_dir in config will be used if set. If both are set, they must be same. If both are None, a temporary directory will be used.

在这里,BaselineClassifier 将首先构建一个使用 baseline/bias 的图形。然后它发现在model_dir中有一个先前的检查点。它将尝试加载此检查点,您应该会看到一条信息(如果您已完成 tf.logging.set_verbosity(tf.logging.INFO)),内容类似于

"INFO:tensorflow:Restoring parameters from .../checkpoints_tutorial17-1\model.ckpt-..."

因为 model_dir 中的这个检查点不是来自 BaselineClassifier,所以它不会有 baseline/biasBaselineClassifier 找不到它,因此会抛出错误。