【问题标题】:ValueError: Error when checking input: expected embedding_2_input to have shape (500,) but got array with shape (1,)ValueError:检查输入时出错:预期 embedding_2_input 的形状为 (500,),但得到的数组的形状为 (1,)
【发布时间】:2020-05-29 05:01:27
【问题描述】:

我已经使用 keras 训练了模型,并且在尝试使用 keras 情感分析使用原始文本数据预测值时 从 SQL 服务器获取原始数据

这是我的代码

x_data = [clean_text]
x_data_series = pd.Series(x_data)
raw_text = tokenizer.texts_to_sequences(x_data_series)
raw_text = pad_sequences(raw_text, maxlen=1, dtype='int32', value=0)
for x_t in raw_text:
  sentiment = model.predict(x_t,batch_size=2)[0]
  y_classes = sentiment.argmax(axis=-1) 

获取此行的错误

sentiment = model.predict(x_t,batch_size=2)[0]

错误

ValueError:检查输入时出错:预期 embedding_2_input 的形状为 (500,),但得到的数组的形状为 (1,)

【问题讨论】:

    标签: python tensorflow keras sentiment-analysis


    【解决方案1】:

    model.predict() 需要小批量数据,但您只传递了一个样本。您可以通过在 x_t 中创建附加维度来将样本转换为大小为 1 的小批量:

    sentiment = model.predict(x_t[np.newaxis, :],batch_size=2)[0]
    

    【讨论】:

    • 现在出现以下错误:ValueError:检查输入时出错:预期 embedding_2_input 的形状为 (500,) 但数组的形状为 (14,)
    • 请分享您的模型代码,没有它是无法确定错误原因的。
    猜你喜欢
    • 1970-01-01
    • 2020-04-11
    • 2020-05-17
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 2019-12-16
    • 2022-10-16
    相关资源
    最近更新 更多