通用句子编码器中的 Tensorflow 会话错误
Tensorflow session error in universal sentence encoder
我有以下通用句子编码器代码,一旦我将模型 加载到烧瓶中 api 并尝试,它就会出现以下错误(检查下方)击中它:
'''
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
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)
def universalModel(messages):
accuracy = []
similarity_input_placeholder = tf.placeholder(tf.string, shape=(None))
similarity_message_encodings = embed(similarity_input_placeholder)
with tf.Session() as session:
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
message_embeddings_ = session.run(similarity_message_encodings, feed_dict={similarity_input_placeholder: messages})
corr = np.inner(message_embeddings_, message_embeddings_)
accuracy.append(corr[0,1])
# print(corr[0,1])
return "%.2f" % accuracy[0]
'''
将模型放入烧瓶时出现以下错误api:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph无效,包含1个节点的循环,包括:StatefulPartitionedCall
尽管此代码在 colab notebook 中运行时没有任何错误。
我正在使用 tensorflow 版本 2.2.0.
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
这两行是为了让tensorflow2.x变成tensorflow1.x。
对于 Tensorflow 1.x,这是使用 flask、django 等服务时的常见问题。
您必须定义用于推理的图形和会话,
将 tensorflow 导入为 tf
导入 tensorflow_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"]})
详情
https://www.tensorflow.org/hub/common_issues
对于 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]
我有以下通用句子编码器代码,一旦我将模型 加载到烧瓶中 api 并尝试,它就会出现以下错误(检查下方)击中它:
'''
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
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)
def universalModel(messages):
accuracy = []
similarity_input_placeholder = tf.placeholder(tf.string, shape=(None))
similarity_message_encodings = embed(similarity_input_placeholder)
with tf.Session() as session:
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
message_embeddings_ = session.run(similarity_message_encodings, feed_dict={similarity_input_placeholder: messages})
corr = np.inner(message_embeddings_, message_embeddings_)
accuracy.append(corr[0,1])
# print(corr[0,1])
return "%.2f" % accuracy[0]
'''
将模型放入烧瓶时出现以下错误api: tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph无效,包含1个节点的循环,包括:StatefulPartitionedCall 尽管此代码在 colab notebook 中运行时没有任何错误。
我正在使用 tensorflow 版本 2.2.0.
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
这两行是为了让tensorflow2.x变成tensorflow1.x。
对于 Tensorflow 1.x,这是使用 flask、django 等服务时的常见问题。 您必须定义用于推理的图形和会话,
将 tensorflow 导入为 tf 导入 tensorflow_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"]})
详情 https://www.tensorflow.org/hub/common_issues
对于 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]