Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Keras ValueError: Shapes (None, 3, 2) and (None, 2) are incompatible?

The following code gives me error ValueError: Shapes (None, 3, 2) and (None, 2) are incompatible. What I want to do is to construct a multi-task network. How should I resolve it? I am using Tensorflow 2.3.0.

import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout
from tensorflow.keras import Model


base_model = tf.keras.applications.EfficientNetB7(input_shape=(32,32, 3), weights='imagenet',
                                                  include_top=False)  # or weights='noisy-student'

for layer in base_model.layers[:]:
    layer.trainable = False

x = GlobalAveragePooling2D()(base_model.output)
dropout_rate = 0.3


x = Dense(256, activation='relu')(x)
x = Dropout(dropout_rate)(x)
x = Dense(256, activation='relu')(x)
x = Dropout(dropout_rate)(x)


all_target = []
loss_list = []
test_metrics = {}
for name, node in  [("task1", 2), ("task2", 2), ("task3", 2)]:
    y1 = Dense(128, activation='relu')(x)
    y1 = Dropout(dropout_rate)(y1)
    y1 = Dense(64, activation='relu')(y1)
    y1 = Dropout(dropout_rate)(y1)
    # y1 = Dense(64, activation='relu')(y1)
    # y1 = Dropout(dropout_rate)(y1)
    y1 = Dense(node, activation='softmax', name=name)(y1)
    all_target.append(y1)
    loss_list.append('categorical_crossentropy')
    test_metrics[name] = "accuracy"

#    model = Model(inputs=model_input, outputs=[y1, y2, y3])
model = Model(inputs=base_model.input, outputs=all_target)

model.compile(loss=loss_list, optimizer='adam', metrics=test_metrics)

res=np.random.randint(2, size=3072).reshape(32, 32, 3)
res=np.expand_dims(res, 0)

lab=np.array([[[0,1], [0,1], [0,1]]])

history = model.fit(res, y=lab, epochs=1, verbose=1)
like image 593
william007 Avatar asked Nov 07 '22 04:11

william007


1 Answers

As you can imagine, the error is caused by the shape of your target. Keras expects the following:

A list of 3 NumPy arrays (for your three tasks), of shape (n_samples, n_categories)

The training will successfully run with this line:

lab = [np.array([[0, 1]]), np.array([[0, 1]]), np.array([[0, 1]])]

We have a different version, but when running your code, I had an error that was more informative:

ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 3 array(s), for inputs ['task1', 'task2', 'task3'] but instead got the following list of 1 arrays: [array([[[0, 1], [0, 1], [0, 1]]])]...

like image 75
Nicolas Gervais Avatar answered Nov 14 '22 08:11

Nicolas Gervais