面试当中一般也会问到这个问题,多线程与多进程,此处不表。我只想解决我的问题:
两个都是基于tf的模型,一个模型with下结束后,另外一个运行就出错,说模型定义什么的。
搜索了下,发现tf模型需要在各自的图定义下运行,问题是一个是有定义的,一个是库,这个库依赖的是keras,不好搞啊,并不都是with tf.Session() as sess能解决的。
不妨先按照别人的方法试试看,搞个类。
import tensorflow as tf
class ImportGraph():
""" Importing and running isolated TF graph """
def __init__(self, loc):
# Create local graph and use it in the session
self.graph = tf.Graph()
self.sess = tf.Session(graph=self.graph)
with self.graph.as_default():
# Import saved model from location 'loc' into local graph
# 从指定路径加载模型到局部图中
saver = tf.train.import_meta_graph(loc + '.meta',