Wazaki
Wazaki

Reputation: 899

Keras Custom generator issue when evaluating the model

I am training a CNN LSTM model using Keras, and after the training was done, I tried to evaluate the model on the testing data like I did when I fine-tuned my CNN, however an error appears this time.

After training was done, I tried to following piece of code to evaluate on my testing set:

x, y = zip(*(testgenerator[i] for i in range(len(testgenerator))))

x_test, y_test = np.vstack(x), np.vstack(y)

loss, acc = Bi_LSTM.evaluate(x_test, y_test, batch_size=9)

print("Accuracy: " ,acc)
print("Loss: ", loss)

I have used this code before to evaluate my fine tuned model and it had no issue, but now I get the following error:

TypeError: object of type 'generator' has no len()

I have tried few solutions online like using len(list(generator)) but it did not work. Is it because I am using a custom generator? How can I do to evaluate model in this case ?

Upvotes: 0

Views: 376

Answers (2)

Wazaki
Wazaki

Reputation: 899

The way I solved this is by using a different method. In this case I do not need to extract values for x,y:

loss, acc = Bi_LSTM.evaluate_generator(testgenerator, batch_size=9)

Upvotes: 0

Novak
Novak

Reputation: 2171

I think this line is the problem

x, y = zip(*(testgenerator[i] for i in range(len(testgenerator))))

because you call len on generator object. The solution may be if you just create some counter, increment it and use it as index in testgenerator[i]

Upvotes: 1

Related Questions