votes up 5

Found unexpected keys that do not correspond to any Model output: (param1). Expected: (param1)

Package:
keras
github stars 52268
Exception Class:
ValueError

Raise code

                     not any(tf.nest.is_nested(y_p) for y_p in y_pred))

  if (single_output or outputs_are_flat_list) and isinstance(struct, dict):
    output_names = output_names or create_pseudo_output_names(y_pred)
    struct = copy.copy(struct)
    new_struct = [struct.pop(name, None) for name in output_names]
    if struct:
      raise ValueError('Found unexpected keys that do not correspond '
                       'to any Model output: {}. Expected: {}'.format(
                           struct.keys(), output_names))
    if len(new_struct) == 1:
      return new_struct[0]
    return new_struct
  else:
    return struct


def 
😲 Agile task management is now easier than calling a taxi. #Tracklify
🙏 Scream for help to Ukraine
Today, 2nd July 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 2 votes down

Summary:

This exception can arise when calling the map_to_output_names function. This function takes in 3 parameters: y_pred, output_names, and struct. y_pred is a list of values. output_names is a list of names as strings. struct is a dictionary. The important thing to keep in mind is that the keys of the struct must match the output_names. If they are different from each other you will get this exception. It is important not to reverse the order of the keys and values in the dictionary. The keys must be the output_names, not the values.

Code to Reproduce the Error (Wrong):

from keras.engine.compile_utils import map_to_output_names

y_pred = [1,2,3,4,5]
output_names = ['a', 'b', 'c', 'd', 'e']
struct = {1:'a', 2:'b', 3:'c', 4:'d', 5:'e'}
map_to_output_names(y_pred, output_names, struct)

Error Message:

ValueError                                Traceback (most recent call last)
<ipython-input-68-38522024d135> in <module>()
      4 output_names = ['a', 'b', 'c', 'd', 'e']
      5 struct = {1:'a', 2:'b', 3:'c', 4:'d', 5:'e'}
----> 6 map_to_output_names(y_pred, output_names, struct)

/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py in map_to_output_names(y_pred, output_names, struct)
    649       raise ValueError('Found unexpected keys that do not correspond '
    650                        'to any Model output: {}. Expected: {}'.format(
--> 651                            struct.keys(), output_names))
    652     if len(new_struct) == 1:
    653       return new_struct[0]

ValueError: Found unexpected keys that do not correspond to any Model output: dict_keys([1, 2, 3, 4, 5]). Expected: ['a', 'b', 'c', 'd', 'e']

Working Version (Right):

from keras.engine.compile_utils import map_to_output_names

y_pred = [1,2,3,4,5]
output_names = ['a', 'b', 'c', 'd', 'e']
# keys and values have been swapped
updated_struct = {'a': 1, 'b': 2, 'c': 3, 'd': 4,'e': 5}
map_to_output_names(y_pred, output_names, updated_struct)
Jul 14, 2021 codingcomedyig answer

Add a possible fix

Please authorize to post fix