gpt4 book ai didi

python - import_meta_graph 失败,数据丢失 : not an sstable (bad magic number)

转载 作者:太空宇宙 更新时间:2023-11-03 10:52:01 27 4
gpt4 key购买 nike

上下文

我在尝试解决另一个错误时遇到了这个问题。第一个错误(原始问题)是,当我尝试恢复元图时,我会得到 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)

打印

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 位 LinuxTF 1.4

最佳答案

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

关于python - import_meta_graph 失败,数据丢失 : not an sstable (bad magic number),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48438122/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com