我正在遵循此link来在小型数据集上训练rnn分类器,以检查代码是否正常工作。
运行命令时
rnn.predict(data_test, 'answer.csv')
,引发异常:
AttributeError: 'tuple' object has no attribute 'ndim'
这是预测功能
def predict(self, data_test, answer_filename):
word_matrix, char_matrix, additional_features_matrix = data_test
print("Test example: ")
print(word_matrix[0])
print(char_matrix[0])
print(additional_features_matrix[0])
preds = self.model.predict([word_matrix, char_matrix, additional_features_matrix],
batch_size=self.batch_size, verbose=1)
index_to_author = { 0: "EAP", 1: "HPL", 2: "MWS" }
submission = pd.DataFrame({"id": test["id"], index_to_author[0]: preds[:, 0],
index_to_author[1]: preds[:, 1], index_to_author[2]: preds[:, 2]})
submission.to_csv(answer_filename, index=False)
word_matrix
,char_matrix
,additional_features_matrix
的长度是可变的。在我的情况下,尺寸分别为(80,),(80,30)和(1153,15)。我用Google搜索它,发现我应该在输入的numpy数组中添加填充。
但是,链接中的代码工作正常。我无法理解我在做什么错。有人可以帮我吗?
答案 0 :(得分:0)
我发现了自己的错误。如果您遵循此link,则会发现以下代码行:
_, additional_features_matrix_test = collect_additional_features(x.iloc[idx_train], x_test)
函数collect_additional_features
返回两个ndarray的元组。我的错误是我错过了_
,因此代码行变成了:
additional_features_matrix_test = collect_additional_features(x.iloc[idx_train], x_test)
因此additional_features_matrix_test
变成了元组而不是ndarray,并且在将additional_features_matrix_test
传递给LSTM的同时,抛出了错误AttributeError: 'tuple' object has no attribute 'ndim'