我在尝试解决另一个错误时遇到了这个问题。第一个错误(原始问题)是当我尝试恢复元图时,我会得到Cannot find KeyError: "The name 'multi_rnn_cell_6' refers to an Operation not in the graph."
。在尝试为该问题创建MVCE时,我发现了这个错误。
创建一些操作的简单脚本,保存元图和变量,然后尝试加载图形和变量失败。 问题似乎与TF正在使用的格式有关。
import tensorflow as tf
import numpy as np
import os
import glob
class ImportIssue(object):
def __init__(self,load=False,model_scope = 'model',checkpoint='checkpoint'):
try:
os.makedirs(checkpoint)
except:
pass
save_file = os.path.join(checkpoint,'model')
print("Save file: {}".format(save_file))
graph = tf.Graph()
with graph.as_default():
if load:
# load model if requested
model_to_load = "{}.meta".format(tf.train.latest_checkpoint(checkpoint))
print("Loading model: {}".format(model_to_load))
rest = tf.train.import_meta_graph(model_to_load)
else:
# else create one
with tf.variable_scope(model_scope):
inputs = tf.placeholder(shape=(None,10,10),dtype=tf.float32)
cell = self._build_cell(10)
# this cell is failing to be fond
#print(cell.name)
rnn,state = tf.nn.dynamic_rnn(cell,inputs,dtype=tf.float32)
train_op = self._build_training_op(inputs,rnn)
saver = tf.train.Saver(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES), max_to_keep=1)
with tf.Session(graph=graph) as sess:
if load:
rest.restore(sess, model_to_load)
else:
sess.run(tf.global_variables_initializer())
sess.run(train_op,feed_dict={inputs:np.random.normal(size=[3,10,10])})
saver.save(sess, save_file)
print("Saved model and graph")
print("Files in checkpoint dir: {}".format(glob.glob("{}/*".format(checkpoint))))
def _build_cell(self,size):
with tf.variable_scope("decoder"):
cells = []
cells.append(tf.nn.rnn_cell.GRUCell(size,activation=tf.nn.tanh))
for res_block_i in range(1):
res_block = tf.nn.rnn_cell.MultiRNNCell([tf.nn.rnn_cell.LSTMCell(size, use_peepholes=True) for i in range(2)])
res_block = tf.nn.rnn_cell.ResidualWrapper(res_block)
res_block = tf.nn.rnn_cell.DropoutWrapper(res_block, input_keep_prob = 1.0,
output_keep_prob = 0.5, state_keep_prob = 0.5,
variational_recurrent = True, dtype=tf.float32)
cells.append(res_block)
cell = tf.nn.rnn_cell.MultiRNNCell(cells)
return cell
def _build_training_op(self,inputs,rnn):
o = tf.train.AdamOptimizer(1e-3)
loss = tf.reduce_mean(tf.square(inputs - rnn))
return o.minimize(loss)
if __name__ == '__main__':
ImportIssue()
ImportIssue(load=True)
打印
Saved model and graph
Files in checkpoint dir: ['checkpoint/model.data-00000-of-00001', 'checkpoint/model.meta', 'checkpoint/checkpoint', 'checkpoint/model.index']
Save file: checkpoint/model
Loading model: checkpoint/model.meta
错误是:
tensorflow.python.framework.errors_impl.DataLossError: Unable to open table file checkpoint/model.meta: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?
Python 3.6 Fedora 64位Linux TF 1.4
答案 0 :(得分:1)
必须指定年检查点,而不能将.data-00000-of-00001似乎添加到V2 tf图形保存方法中创建的所有检查点的末尾。
答案 1 :(得分:0)
您可能想查看issue 2676 另外,为什么不直接使用saver.restore功能(将立即恢复整个检查点)而不是通过元图执行?
答案 2 :(得分:0)
问题出现是因为Saver.restore
正在尝试从元文件中恢复。这回答了这个问题,但遗憾的是代码现在正常运行,MVCE还没有复制我试图创建的原始错误。