在Keras中标记数据遇到的问题

我正在使用预处理来标记数据并使用它们来拟合模型

我的资源:https://machinelearningmastery.com/prepare-text-data-deep-learning-keras

这是我的代码(我想从负面词汇中检测出正面词汇):

from keras.preprocessing.text import one_hotfrom keras.preprocessing.text import text_to_word_sequenceimport pandas as pdimport numpy as npfrom keras.layers import Densefrom keras.models import Sequentialfrom keras.preprocessing.text import Tokenizerfrom tensorflow.keras.layers.experimental import preprocessingdata = ["Good", "Great", 'Bad', 'Awefull']t = Tokenizer()# fit the tokenizer on the documentst.fit_on_texts(data)# integer encode documentsx = t.texts_to_matrix(data, mode='count')print(x)y = np.array([[1],[1],[0],[0]])print(y)model = Sequential()model.add(Dense(16, input_dim=2, activation='relu'))model.add(Dense(16, activation='relu'))model.add(Dense(1, activation='sigmoid'))model.compile(loss='mean_squared_error',    optimizer='adam',    metrics=['binary_accuracy'])model.fit(x, y, verbose=2, epochs=500)model.save("good_or_bad.h5")

我遇到了这个错误:

raise ValueError(    ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 2 but received input with shape [None, 5]

我该如何解决这个错误?


回答:

更改这一行:

model.add(Dense(16, input_dim=5, activation='relu'))

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注