在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

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

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