部署flask,tensorflow服务时报错 Tensor Tensor("Pooler-Dense/BiasAdd:0", shape=(?, 768), dtype=float32) is not an element of this graph
解决方式:
初始化后加 self.graph = tf.get_default_graph() predict时加 graph = self.graph class ChatModel: def __init__(self): # bert配置 config_path = '/root/kg/bert/chinese_simbert_L-12_H-768_A-12/bert_config.json' checkpoint_path = '/root/kg/bert/chinese_simbert_L-12_H-768_A-12/bert_model.ckpt' dict_path = '/root/kg/bert/chinese_simbert_L-12_H-768_A-12/vocab.txt' # 建立分词器 self.tokenizer = Tokenizer(dict_path, do_lower_case=True) # 建立分词器 # 建立加载模型 bert = build_transformer_model( config_path, checkpoint_path, with_pool='linear', application='unilm', return_keras_model=False, ) self.encoder = keras.models.Model(bert.model.inputs, bert.model.outputs[0]) self.graph = tf.get_default_graph() def retrieval_milvus(self, search_title): graph = self.graph encoder = self.encoder tokenizer = self.tokenizer token_ids, segment_ids = tokenizer.encode(search_title, maxlen=maxlen) with graph.as_default(): vec = encoder.predict([[token_ids], [segment_ids]])[0]