tensorflow加载多个计算图的冲突解决
2021/4/23 18:58:18
本文主要是介绍tensorflow加载多个计算图的冲突解决,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
需求:顺序加载多个计算图时,会导致第二个计算图后变量 不可用,在程序初始化中解决该问题(一下代码没有做优化,请读者自行修正)
class BertEncoder(object): """ model """ def __init__(self, OUTPUT_GRAPH, OUT_TENSOR): self.max_length = 30 self.tokenizer = TOKENIZER self.out_graph = os.path.join(CURRENT_DIR, "pb_model", OUTPUT_GRAPH) self.out_tensor = OUT_TENSOR self.model_graph = {} graph = tf.Graph() with graph.as_default(): self.model_graph['output_graph_def'] = tf.compat.v1.GraphDef() with open(self.out_graph, "rb") as f: self.model_graph['output_graph_def'].ParseFromString(f.read()) self.model_graph['sess'] = tf.Session(graph=graph) with self.model_graph['sess'].as_default(): with graph.as_default(): self.model_graph['sess'].run(tf.compat.v1.global_variables_initializer()) tf.import_graph_def(self.model_graph['output_graph_def'], name="") self.input_ids_p = self.model_graph['sess'].graph.get_tensor_by_name("input_ids:0") self.input_mask_p = self.model_graph['sess'].graph.get_tensor_by_name("input_mask:0") self.output_tensor = self.model_graph['sess'].graph.get_tensor_by_name(self.out_tensor) def predict(self, to_predict): """pb predict """ sentence = [each.lower() for each in to_predict] input_ids, input_mask, = self.convert(sentence) feed_dict = {self.input_ids_p: input_ids, self.input_mask_p: input_mask} sess = self.model_graph['sess'] output_emb = sess.run(self.output_tensor, feed_dict) return output_emb
这篇关于tensorflow加载多个计算图的冲突解决的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-30tensorflow是什么-icode9专业技术文章分享
- 2024-10-15成功地使用本地的 NVIDIA GPU 运行 PyTorch 或 TensorFlow
- 2024-01-23供应链投毒预警 | 恶意Py包仿冒tensorflow AI框架实施后门投毒攻击
- 2024-01-19attributeerror: module 'tensorflow' has no attribute 'placeholder'
- 2024-01-19module 'tensorflow.compat.v2' has no attribute 'internal'
- 2023-07-17【2023年】第33天 Neural Networks and Deep Learning with TensorFlow
- 2023-07-10【2023年】第32天 Boosted Trees with TensorFlow 2.0(随机森林)
- 2023-07-09【2023年】第31天 Logistic Regression with TensorFlow 2.0(用TensorFlow进行逻辑回归)
- 2023-07-01【2023年】第30天 Supervised Learning with TensorFlow 2(用TensorFlow进行监督学习 2)
- 2023-06-18【2023年】第29天 Supervised Learning with TensorFlow 1(用TensorFlow进行监督学习 1)