首页 > 解决方案 > Keras标签的问题

问题描述

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

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

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

from keras.preprocessing.text import one_hot
from keras.preprocessing.text import text_to_word_sequence
import pandas as pd
import numpy as np
from keras.layers import Dense
from keras.models import Sequential
from keras.preprocessing.text import Tokenizer
from tensorflow.keras.layers.experimental import preprocessing
data = ["Good", "Great", 'Bad', 'Awefull']
t = Tokenizer()
# fit the tokenizer on the documents
t.fit_on_texts(data)
# integer encode documents
x = 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]

我该如何解决这个错误?

标签: pythontensorflowmachine-learningkerasartificial-intelligence

解决方案


更改此行:

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

推荐阅读