python - Tensor("StatefulPartitionedCall_8:0) 并期望任何非张量类型,得到一个张量而不是异常
问题描述
我一直在尝试使用 Elmo 与下载的 imdb 数据集进行词嵌入。我找到了这个例子: https ://www.analyticsvidhya.com/blog/2019/03/learn-to-use-elmo-to-extract-features-from-text/
但是,我遇到了不同的问题。我正在处理的文本如下:
20316 Simple story why say more It nails it s pr...
13180 It s cheesy it s creepy it s gross but that...
23353 Astounding This may have been A poor attem...
13658 In a penitentiary four prisoners occupy a cel...
19625 An unusual take on time travel instead of tra...
24173 Is this your typical women in chains navy tran...
4522 I don t know much about Tobe Hooper or why he...
1718 what kind of sh t is this Power rangers vs Fr...
10006 I have a piece of advice for the people who ma...
12360 I went in not knowing anything about this movi...
Name: texts, Length: 24999, dtype: object
形状为 (24999,)
elmo=hub.load("https://tfhub.dev/google/elmo/2")
listTrain=[X_train[i:i+10] for i in range(0,X_train.shape[0],10)]
elmoTrain=[getElmo(elmo,i)for i in listTrain]
我的函数 getElmo 如下:
def getElmo(elmo,x):
embed=elmo.signatures["default"](tf.constant(x))
tf.compat.v1.disable_eager_execution()
with tf.compat.v1.Session() as ses:
ses.run(tf.compat.v1.global_variables_initializer())
ses.run(tf.compat.v1.tables_initializer())
return ses.run(tf.reduce_mean(embed,1))
教程链接的原始源代码如下:
def elmo_vectors(x):
embeddings = elmo(x.tolist(), signature="default", as_dict=True)["elmo"]
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.tables_initializer())
# return average of ELMo features
return sess.run(tf.reduce_mean(embeddings,1))
但是,我有以下例外:
值错误:张量(“StatefulPartitionedCall_10:0”,形状=(无,1024),dtype=float32)
和
TypeError: 期望任何非张量类型,而是得到一个张量。
我该如何修复它们?
实际上我使用的是 Python 3.7 和 tensorflow 2.3。