tensorflow - 无法恢复模型 - "Couldn't match files for checkpoint"

tensorflow - cannot restore model - "Couldn't match files for checkpoint"

这是我保存到磁盘的模型:

import tensorflow as tf
import numpy as np


BATCH_SIZE = 3
VECTOR_SIZE = 1
LEARNING_RATE = 0.1

x = tf.placeholder(tf.float32, [BATCH_SIZE, VECTOR_SIZE],
                   name='input_placeholder')
y = tf.placeholder(tf.float32, [BATCH_SIZE, VECTOR_SIZE],
                   name='labels_placeholder')

W = tf.get_variable('W', [VECTOR_SIZE, BATCH_SIZE])
b = tf.get_variable('b', [VECTOR_SIZE], initializer=tf.constant_initializer(0.0))

y_hat = tf.matmul(W, x) + b
predict = tf.add(tf.matmul(W, x), b, name='predict')
total_loss = tf.reduce_mean(y-y_hat)
train_step = tf.train.AdagradOptimizer(LEARNING_RATE).minimize(total_loss)
X = np.ones([BATCH_SIZE, VECTOR_SIZE])
Y = np.ones([BATCH_SIZE, VECTOR_SIZE])
all_saver = tf.train.Saver() 

sess= tf.Session()
sess.run(tf.global_variables_initializer())
sess.run([train_step], feed_dict = {x: X, y:Y})
save_path = r'C:\tmp\tmp\'
all_saver.save(sess,save_path)

正在尝试恢复

checkpoint_path = r'C:\tmp\tmp\'
tf.train.latest_checkpoint(checkpoint_path)

我收到以下错误消息:

ERROR:tensorflow:Couldn't match files for checkpoint C:\tmp\tmp\

C:\tmp\tmp\ 我有以下文件:

.data-00000-of-00001
.index
.meta
checkpoint

有什么想法吗?

这些文件只是命名行吗?从点开始?

如果是这种情况,您应该考虑以不同方式保存它们,因为这可能是问题所在。

试试:

NUMBER_OF_CKPT = 60 saver.save(sess,save_path,global_step=NUMBER_OF_CKPT)

通常做的是将 global_step 也保存为 ckpt 的编号。

希望已经解决了!

来自saver.savetensorflowapi:

save_path: String. Path to the checkpoint filename. If the saver is sharded, this is the prefix of the sharded checkpoint filename.

save_path 中你没有指定检查点文件名。

为了将来使用,请尝试设置: checkpoint_path = r'C:\tmp\tmp\my-model'.

如果要加载以前保存的模型,请执行以下操作:

  1. 为这些文件添加字符串 my-model
.data-00000-of-00001
.index
.meta
  1. 修改 checkpoint 文件,使其指向您的检查点:
model_checkpoint_path: "C:\tmp\tmp\my-model"
all_model_checkpoint_paths: "C:\tmp\tmp\my-model"

现在应该可以加载检查点了。

FWIW 我在 AI Platform (Cloud ML Engine) 上训练自定义估算器时看到了这个错误。我的问题是由我保存 checkpoints/model 元数据的 GCS 存储桶的 region 引起的。

当此存储桶的 region 设置为 us (multiple regions in United States) 时,我在评估期间看到了此错误。将 GCS 存储桶的 region 设置为与 AI Platform 作业 运行 相同的 region(在我的例子中是 us-central1 (Iowa))解决了问题。