When my friend was working on an Age and Gender Detection model using Convolutional Neural Network, he faced this ValueError. He has given the ‘metrics’ argument as a list during model defining, but it said it should have as many entries as the model have. My model has two outputs ‘age’ and ‘gender’.
He was defining his CNN model and when he tried to fit the model on training data it showed an error saying
ValueError: For a model with multiple outputs, when providing the
metrics
argument as a list, it should have as many entries as the model has outputs. Received:
metrics=[‘accuracy’]
of length 1 whereas the model has 2 outputs.
This is my full error:
ValueError Traceback (most recent call last)
Cell In[47], line 1
----> 1 History=Model.fit(X_train,Y_train_2,batch_size=64,validation_data=(X_test,Y_test_2),epochs=10,callbacks=callback_list)
File c:Usersmebub_9a7jdi8DesktopAge_Gender_Detection_Model.envLibsite-packageskerassrcutilstraceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
119 filtered_tb = _process_traceback_frames(e.__traceback__)
120 # To get the full stack trace, call:
121 # `keras.config.disable_traceback_filtering()`
--> 122 raise e.with_traceback(filtered_tb) from None
123 finally:
124 del filtered_tb
File c:Usersmebub_9a7jdi8DesktopAge_Gender_Detection_Model.envLibsite-packageskerassrctrainerscompile_utils.py:250, in CompileMetrics._build_metrics_set(self, metrics, num_outputs, output_names, y_true, y_pred, argument_name)
248 if isinstance(metrics, (list, tuple)):
249 if len(metrics) != len(y_pred):
--> 250 raise ValueError(
251 "For a model with multiple outputs, "
252 f"when providing the `{argument_name}` argument as a "
253 "list, it should have as many entries as the model has "
254 f"outputs. Received:n{argument_name}={metrics}nof "
255 f"length {len(metrics)} whereas the model has "
256 f"{len(y_pred)} outputs."
257 )
258 for idx, (mls, yt, yp) in enumerate(
...
261 if not isinstance(mls, list):
ValueError: For a model with multiple outputs, when providing the `metrics` argument as a list, it should have as many entries as the model has outputs. Received:
metrics=['accuracy']
of length 1 whereas the model has 2 outputs.
This is my code for defining model:
def model(input_shape):
inputs=Input((input_shape))
conv_1=Convolution(inputs,32)
maxp_1=MaxPooling2D(pool_size=(2,2))(conv_1)
conv_2=Convolution(maxp_1,64)
maxp_2=MaxPooling2D(pool_size=(2,2))(conv_2)
conv_3=Convolution(maxp_2,128)
maxp_3=MaxPooling2D(pool_size=(2,2))(conv_3)
conv_4=Convolution(maxp_3,256)
maxp_4=MaxPooling2D(pool_size=(2,2))(conv_4)
flatten= Flatten()(maxp_4)
dense_1=Dense(64,activation='relu')(flatten)
dense_2=Dense(64,activation='relu')(flatten)
drop_1=Dropout(0.2)(dense_1)
drop_2=Dropout(0.2)(dense_2)
output_1=Dense(1,activation='sigmoid',name='sex_out')(drop_1)
output_2=Dense(1,activation='relu',name='age_out')(drop_2)
model=Model(inputs=[inputs],outputs=[output_1,output_2])
model.compile(loss=["binary_crossentropy","mae"],optimizer="Adam",metrics=["accuracy"])
return model
Satya Prakash Mohanty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.