Getting an error TypeError: cannot unpack non-iterable float object

Issue

I want to evaluate my ML model and I am getting this error:

TypeError: cannot unpack non-iterable float object

My code follows:

# mlp for the blobs multi-class classification problem with cross-entropy loss
from sklearn.datasets import make_blobs
from keras.layers import Dense
from keras.models import Sequential
from keras.optimizers import SGD
from tensorflow.keras.utils import to_categorical
from matplotlib import pyplot

# evaluate the model
_, train_acc = model.evaluate(trainX, trainY, verbose=2)
_, test_acc = model.evaluate(testX, testY, verbose=2)
print('Train: %.3f, Test: %.3f' % (train_acc, test_acc))

Solution

It is likely that your model has not accuracy metric, and model.evaluate() returns only loss. You can check the available metrics like this:

print(model.metrics_names)

And probably it’s output is just ['loss'], and there is not accuracy metric, since you didn’t provide it on model.compile().

Since it just returns loss, you should change this line like this:

train_loss = model.evaluate(trainX, trainY, verbose=2)
test_loss = model.evaluate(testX, testY, verbose=2)

If you want to get accuracy, you should add it to your model compile:

model.compile(loss='...',metrics=['accuracy'],optimizer='adam')
.
.
train_loss, train_acc = model.evaluate(trainX, trainY, verbose=2)
test_loss, test_acc = model.evaluate(testX, testY, verbose=2)

Answered By – Kaveh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published