我正在尝试结合SKLearn的Tfidfvectorizer和Keras,但是我遇到了以下错误:
异常:在检查模型输入时出错:期望dense_input_1的形状为(None,126),但得到的数组形状为(700,116)
我理解这与矩阵的形状有关,但我无法解决它。
我理解这与矩阵的形状有关,但我无法解决它。
vectorizer = TfidfVectorizer(analyzer=self.identity, use_idf=True, max_features=2000)
#a list of sentences
x_train_vec = vectorizer.fit_transform(x_train).toarray()
x_test_vec = vectorizer.fit_transform(self.x_test[i]).toarray()
#labels
y_train = np_utils.to_categorical(y_train, self.nb_classes)
y_test = np_utils.to_categorical(y_test, self.nb_classes)
#get model
model = self.build_model(x_train_vec.shape[1])
model.fit(x_train_vec, y_train, nb_epoch=self.n_epochs, batch_size=self.batch_size, shuffle='batch', verbose=1, validation_data=(x_test_vec, y_test), )
建立模型:
def build_model(self, nb_features):
print("Building model...")
model = Sequential()
model.add(Dense(input_dim = nb_features, output_dim = self.hidden_units_1))
model.add(Activation('relu'))