import_meta_graph因数据丢失而失败:不是稳定的(错误的魔术数字)

Jos*_*ert 5 python serialization python-3.x tensorflow

语境

我在尝试解决另一个错误时遇到了这个问题。第一个错误(原始问题)是,当我尝试还原元图时,我会得到Cannot find KeyError: "The name 'multi_rnn_cell_6' refers to an Operation not in the graph."。在尝试为该问题创建MVCE时,我发现了此错误。

问题

创建一些操作,保存元图和变量,然后尝试加载图和变量的简单脚本失败。该问题似乎与TF使用的格式有关。

MVCE

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)
Run Code Online (Sandbox Code Playgroud)

版画

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
Run Code Online (Sandbox Code Playgroud)

错误是:

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?
Run Code Online (Sandbox Code Playgroud)

版本号

Python 3.6 Fedora 64位Linux TF 1.4

nnr*_*les 8

是的,必须在没有 .data-00000-of-00001 的情况下指定检查点,该文件似乎已添加到 V2 tf 图形保存方法中创建的所有检查点的末尾。


Max*_* F. 0

您可能想检查问题 2676 另外为什么不直接使用 saver.restore 功能(将立即恢复整个检查点)而不是通过元图来执行此操作?