Python 通用句子编码器中的Tensorflow会话错误

Python 通用句子编码器中的Tensorflow会话错误,python,tensorflow,tensorflow2.0,tensorflow-serving,sentence-similarity,Python,Tensorflow,Tensorflow2.0,Tensorflow Serving,Sentence Similarity,我有以下通用句子编码器代码,当我将模型加载到烧瓶api中并尝试点击它时,它会给出以下错误(检查如下): ''' ''' 将模型用于烧瓶api时,会出现以下错误: tensorflow.python.framework.errors\u impl.InvalidArgumentError:图形无效,包含一个包含1个节点的循环,包括:StatefulPartitionedCall 尽管此代码在colab笔记本中运行时没有任何错误 我正在使用tensorflow 2.2.0版。 import tens

我有以下通用句子编码器代码,当我将模型加载到烧瓶api中并尝试点击它时,它会给出以下错误(检查如下):

'''

'''

将模型用于烧瓶api时,会出现以下错误: tensorflow.python.framework.errors\u impl.InvalidArgumentError:图形无效,包含一个包含1个节点的循环,包括:StatefulPartitionedCall 尽管此代码在colab笔记本中运行时没有任何错误

我正在使用tensorflow 2.2.0版。

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
这两条线用于将tensorflow 2.x转换为tensorflow 1.x

对于Tensorflow 1.x,这是与flask、django等一起使用时常见的问题。 您必须定义用于推理的图表和会话

导入tensorflow作为tf 将tensorflow_hub导入为hub

# Create graph and finalize (finalizing optional but recommended).
g = tf.Graph()
with g.as_default():
  # We will be feeding 1D tensors of text into the graph.
  text_input = tf.placeholder(dtype=tf.string, shape=[None])
  embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder/2")
  embedded_text = embed(text_input)
  init_op = tf.group([tf.global_variables_initializer(), tf.tables_initializer()])
g.finalize()

# Create session and initialize.
session = tf.Session(graph=g)
session.run(init_op)
输入请求可以通过

result = session.run(embedded_text, feed_dict={text_input: ["Hello world"]})
详情

对于tensorflow 2.x会话,不需要图形

import tensorflow as tf
module_url = "https://tfhub.dev/google/universal-sentence-encoder-large/5"
model_2 = hub.load(module_url)
print ("module %s loaded" % module_url)

def embed(input):
    return model_2(input)
#pass messages as list
def universalModel(messages):
    accuracy = []
    message_embeddings_= embed(messages)
    corr = np.inner(message_embeddings_, message_embeddings_)
    accuracy.append(corr[0,1])
    # print(corr[0,1])
    return "%.2f" % accuracy[0]

同样的代码也在使用tensorflow 2的jupyter笔记本上工作,所以我不明白为什么它在api中不工作@vivek Ananthant这是TF 1.x与Flask api的已知版本。如果您想与TF2.x一起使用,就不需要像TF1.x那样构造图和会话。使用import tensorflow重新安装前2行,以便tf 2.x将直接使用,而不是tf1.x功能。我在查找关联和加载模型5时出错,请将我的模型5代码深入到flask api代码中@vivek Ananthan将前2条导入语句替换为“import tensorflow”让我知道您在这之后遇到了什么错误我得到了占位符错误,因为它在v2中不存在,我还需要删除什么@维韦克·阿南森
import tensorflow as tf
module_url = "https://tfhub.dev/google/universal-sentence-encoder-large/5"
model_2 = hub.load(module_url)
print ("module %s loaded" % module_url)

def embed(input):
    return model_2(input)
#pass messages as list
def universalModel(messages):
    accuracy = []
    message_embeddings_= embed(messages)
    corr = np.inner(message_embeddings_, message_embeddings_)
    accuracy.append(corr[0,1])
    # print(corr[0,1])
    return "%.2f" % accuracy[0]