Rashmi Singh
Rashmi Singh

Reputation: 539

Keras predict function throws AttributeError: 'tuple' object has no attribute 'ndim'

I am following this link to train rnn classifier on small dataset to check if the code is working.

While running command rnn.predict(data_test, 'answer.csv'), throws exception:

AttributeError: 'tuple' object has no attribute 'ndim'

Here is the predict function

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)

The word_matrix, char_matrix, additional_features_matrix are of variable length. In my case, the dimensions are (80,), (80, 30) and (1153, 15) respectively. I google it and found that I should add padding to the input numpy array.

But, the code in the link worked fine. I am not able to understand what am I doing wrong. Can somebody help me with this?

Upvotes: 1

Views: 2370

Answers (1)

Rashmi Singh
Rashmi Singh

Reputation: 539

I found out my own mistake. If you follow this link then you will find the following line of code:

_, additional_features_matrix_test = collect_additional_features(x.iloc[idx_train], x_test)

The function collect_additional_features returns a tuple of two ndarrays. My mistake was that I missed _ and hence the line of code became:

additional_features_matrix_test = collect_additional_features(x.iloc[idx_train], x_test)

Thus the additional_features_matrix_test became a tuple instead of an ndarray and while passing the additional_features_matrix_test to the LSTM it threw the error AttributeError: 'tuple' object has no attribute 'ndim'

Upvotes: 1

Related Questions