【发布时间】:2019-01-15 09:40:39
【问题描述】:
我正在使用 keras model.predict 来预测情绪。我正在使用通用句子嵌入。在预测时,我收到了下面描述的错误。 请提供您宝贵的见解。 问候。
我已经为两组输入运行了代码。例如,输入1,获得预测。虽然它不适用于输入 2。
Input 1 is the form : {(a1,[sents1]),....}
Input 2:{((a1,a2),[sents11])),...}
预测的输入是从中提取的[sents1]、[sents11]等。
我可以在 (Keras model.predict function giving input shape error) 中看到相关问题。但是不知道有没有解决。此外,input1 正在工作。
import tensorflow as tf
import keras.backend as K
from keras import layers
from keras.models import Model
import numpy as np
def UniversalEmbedding(x):
return embed(tf.squeeze(tf.cast(x, tf.string)), signature="default", as_dict=True)["default"]
input_text = layers.Input(shape=(1,), dtype=tf.string)
embedding = layers.Lambda(UniversalEmbedding, output_shape=(embed_size,))(input_text)
dense = layers.Dense(256, activation='relu')(embedding)
pred = layers.Dense(category_counts, activation='softmax')(dense)
model = Model(inputs=[input_text], outputs=pred)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
sents1=list(input2.items())
with tf.Session() as session:
K.set_session(session)
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
# model.load_weights(.//)
for i,ch in enumerate(sents1):
new_text=ch[1]
if len(new_text)>1:
new_text = np.array(new_text, dtype=object)[:, np.newaxis]
predicts = model.predict(new_text, batch_size=32)
InvalidArgumentError: input must be a vector, got shape: [] [[{{node lambda_2/module_1_apply_default/tokenize/StringSplit}} = StringSplit[skip_empty=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](lambda_2/module_1_apply_default/RegexReplace_1, lambda_2/module_1_apply_default/tokenize/Const)]]
【问题讨论】:
-
你能把你的模型定义也粘贴在这里吗?
-
代码段被编辑
-
当 new_text.shape=(33,1) 时出现错误。谁能提供一些见解。
-
embed中的UniversalEmbedding是什么? -
我遇到了同样的问题。 new_text 必须至少有 2 个元素。作为一种解决方法,我在数组中添加了第二个虚拟元素并忽略它的结果。
标签: python-3.x tensorflow keras