Search code examples
pythonvalueerror

Getting the "too many values to unpack (expected 2)" error when evaluating a model


I keep getting the error "too many values to unpack (expected 2)" whenever I try to evaluate my model. I have no clue what is causing this error and how to fix it. Can anyone help me with this issue?

model = keras.Sequential([
  keras.layers.Conv3D(input_shape=(44,64,64,1), filters=8, kernel_size=3, 
                      strides=2, activation='relu', name='Conv1'),
  keras.layers.Flatten(),
  keras.layers.Dense(2, activation=tf.nn.softmax, name='Softmax')
  ])
model.summary()

batch_size = 16
testing = False
epochs = 3

model.compile(optimizer='adam', 
              loss=keras.losses.SparseCategoricalCrossentropy(),
              metrics=[keras.metrics.SparseCategoricalAccuracy(), 'accuracy'])

model.fit(train_images,
          train_labels,
          validation_data=(test_images, test_labels),
          epochs=epochs)

test_loss, test_acc = model.evaluate(test_images, test_labels) #This is where I am getting the error
print('\nTest accuracy: {}'.format(test_acc))
model.save('my_model.h5')

Note: Thanks to Tuqay for pointing out the issue.


Solution

  • Always use a catch-all placeholder while unpacking. See example behavior below.

    test_loss, test_acc, *is_anything_else_being_returned = model.evaluate(test_images, test_labels) #This is where I am getting the error
    
    >>> a = [1,2,3,4,5,6,7]
    >>> b,c,*d = a
    >>> b
    1
    >>> c
    2
    >>> d
    [3, 4, 5, 6, 7]
    >>>