Search code examples
pythontensorflowkeras

Model.evaluate() returning a float, not a list


I have a multi-task neural network. I want to make sure that when I call Model.evaluate() on my model, that the score I see is the sum of the losses. However, it is returning a scalar rather than a list, so I am not sure what this loss is. According to the documentation, a list of scalars should be returned for multiple outputs or losses. Below is a minimal reproducible example

import numpy as np
from keras.layers import Input, Dense
from keras.models import Model

X = np.random.random((10, 10))
y = {'pi': np.random.random((10,)), 'u':  np.random.random((10,))}

in_layer = Input(shape=X.shape[1:])
out1 = Dense(1, name='pi')(in_layer)
out2 = Dense(1, name='u')(in_layer)
model = Model(inputs=in_layer, outputs=[out1,out2])
model.compile(loss={'pi': 'mean_squared_error', 'u': 'mean_squared_error'}, optimizer = 'adam')

model.fit(X,y)
print(model.evaluate(X, y)) # Returns a float.

I tried passing y as a list but I still get the same result. print(model.metrics_names) returns 'loss'.


Solution

  • For this, you need to specify the metrics individually otherwise Model.evaluate will aggregate the results. Make the following change to the compile function:

    model.compile(
        loss={'pi': 'mean_squared_error', 'u': 'mean_squared_error'}, 
        optimizer='adam',
        metrics={'pi': 'mean_squared_error', 'u': 'mean_squared_error'}
    )
    

    Check this link for more info about the API.