我正在尝试将SKLearn Tfidfvectorizer与Keras结合使用,但我陷入以下错误:异常:检查模型输入时出错:预期dense_input_1具有形状(无,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'))
当你对测试集进行矢量化时,你需要调用transform
而不是fit_transform
:
x_train_vec = vectorizer.fit_transform(x_train).toarray()
x_test_vec = vectorizer.transform(self.x_test[i]).toarray()
问题是x_train和x_test的维度差异。更改 tfidfvectorizer 中的最大特征解决了这个问题。
vectorizer = TfidfVectorizer(analyzer=self.identity, use_idf=True, max_features=100)