gpt4 book ai didi

python - 失败前提条件错误: GetNext() failed after loading a Tensorflow Saved_Model

转载 作者:行者123 更新时间:2023-12-01 01:54:42 26 4
gpt4 key购买 nike

我建立了一个专门的类来构建、训练、保存然后加载我的模型。保存是通过 tf.saved_model.simple_save 完成的,然后通过 tf.saved_model.loader.load 恢复的。

训练和推理是使用数据集 API 完成的。使用经过训练的模型时一切正常。

但是,如果我恢复保存的模型,则推理会中断并引发此错误:

FailedPreconditionError (see above for traceback): GetNext() failed because the iterator has not been initialized. Ensure that you have run the initializer operation for this iterator before getting the next element.

[[Node: datasets/cond/IteratorGetNext_1 = IteratorGetNextoutput_shapes=[[?,?,30], [?,5]], output_types=[DT_INT32, DT_INT32], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

我确定迭代器已初始化(print 按预期显示,请参阅下面的代码)。这可能与图表变量所属有关吗?还有其他想法吗?我有点被困在这里

(简化)代码:

class Model():
def __init__(self):
self.graph = tf.Graph()
self.sess = tf.Session(graph=self.graph)
with self.graph.as_default():
model.features_data_ph = tf.Placeholder(...)
model.labels_data_ph = tf.Placeholder(...)

def build(self):
with self.graph.as_default():
self.logits = my_model(self.input_tensor)
self.loss = my_loss(self.logits, self.labels_tensor)

def train(self):
my_training_procedure()

def set_datasets(self):
with self.graph.as_default():
with tf.variable_scope('datasets'):
self.dataset = tf.data.Dataset.from_tensor_slices((self.features_data_ph, self.labels_data_ph))
self.iter = self.dataset.make_initializable_iterator()
self.input_tensor, self.labels_tensor = self.iter.get_next

def initialize_iterators(self, inference_data):
with self.graph.as_default():
feats = inference_data
labs = np.zeros((len(feats), self.hp.num_classes))
self.sess.run(self.iter.initializer,
feed_dict={self.features_data_ph: feats,
self.labels_data_ph: labs})
print('Iterator ready to infer')

def infer(self, inference_data):
self.initialize_iterators(inference_data)
return sess.run(self.logits)

def save(self, path):
inputs = {"features_data_ph": self.features_data_ph,
"labels_data_ph": self.labels_data_ph}
outputs = {"logits": self.model.logits}
tf.saved_model.simple_save(self.sess, path)

@staticmethod
def restore(path):
model = Model()
tf.saved_model.loader.load(model.sess, [tag_constants.SERVING], path)
model.features_data_ph = model.graph.get_tensor_by_name("features_data_ph:0")
model.labels_data_ph = model.graph.get_tensor_by_name("labels_data_ph:0")
model.logits = model.graph.get_tensor_by_name("model/classifier/dense/BiasAdd:0")
model.set_datasets()
return model

失败例程:

model1 = Model()
model1.build()
model1.train()
model1.save(model1_path)

...

model2 = Model.restore(model1_path)
model2.infer(some_numpy_array) # Error here, after print, at sess.run()

(恢复模型有效,原始模型和恢复模型之间的张量值匹配)

最佳答案

我遇到了同样的问题,我相信问题是您正在初始化一个新的数据集对象,而不是初始化与模型一起保存的迭代器。

尝试:

make_iter = model.get_operation_by_name("YOURPREFIX/MakeIterator")
sess.run(make_iter, feed_dict)
model.infer(some_numpy_array)

关于python - 失败前提条件错误: GetNext() failed after loading a Tensorflow Saved_Model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50354306/

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