Home > OS >  Tensorflow NN architecture incompatible shape
Tensorflow NN architecture incompatible shape

Time:11-07

I am trying to create a Multitask NN using Tensorflow. Following is the architecture that I am trying to develop:

METRICS= [tf.keras.metrics.TruePositives(name='TP'),
         tf.keras.metrics.FalsePositives(name='FP'),
         tf.keras.metrics.TrueNegatives(name='TN'),
         tf.keras.metrics.FalseNegatives(name='FN'),
         tf.keras.metrics.Precision(name='precision'),
         tf.keras.metrics.Recall(name='recall'),
         tf.keras.metrics.AUC(curve='PR', name='PR-AUC')]

input_shape = (X_train.shape[1],)
inputlayer = tf.keras.layers.Input(shape=input_shape)
l1 = tf.keras.layers.Dense(input_shape[0]*2, activation= 'relu')(inputlayer)
l2 = tf.keras.layers.Dropout(0.1)(l1)
l3 =  tf.keras.layers.Dense(int(input_shape[0]/2), activation='relu')(l2)
output1 = tf.keras.layers.Dense(1, activation='sigmoid', name = 'output1')(l3)
output2 = tf.keras.layers.Dense(10, activation='softmax', name = 'output2')(l3)
output3 = tf.keras.layers.Dense(12, activation='softmax', name = 'output3')(l3)

model = tf.keras.Model(inputs=inputlayer, outputs=[output1, output2, output3])

model.compile(loss={"output1": 'binary_crossentropy',
                    "output2": 'categorical_crossentropy',
                    "output3": 'categorical_crossentropy'},
              optimizer=tf.keras.optimizers.Adam(learning_rate=.01), 
              metrics = METRICS, loss_weights = [1, 1e-1, 1e-1])

And this is the model architecture:

enter image description here

Then I tried to train the model like this:

BATCH_SIZE= 20

model.fit(X_train, [y1_train,y2_train,y3_train], batch_size=BATCH_SIZE, epochs=10, verbose=0)

But I got the following issue:

ValueError: Shapes (None, 1) and (None, 10) are incompatible

I already verified the labels of each output and they are respectively 2, 10 and 12 I couldn't understood what the problem is exactly, can anyone give me a suggestion please?

CodePudding user response:

I think you might have mixed up the order of your labels. Here is a working example:

import tensorflow as tf 

METRICS= [tf.keras.metrics.TruePositives(name='TP'),
         tf.keras.metrics.FalsePositives(name='FP'),
         tf.keras.metrics.TrueNegatives(name='TN'),
         tf.keras.metrics.FalseNegatives(name='FN'),
         tf.keras.metrics.Precision(name='precision'),
         tf.keras.metrics.Recall(name='recall'),
         tf.keras.metrics.AUC(curve='PR', name='PR-AUC')]

input_shape = (31,)
inputlayer = tf.keras.layers.Input(shape=input_shape)
l1 = tf.keras.layers.Dense(input_shape[0]*2, activation= 'relu')(inputlayer)
l2 = tf.keras.layers.Dropout(0.1)(l1)
l3 =  tf.keras.layers.Dense(int(input_shape[0]/2), activation='relu')(l2)
output1 = tf.keras.layers.Dense(1, activation='sigmoid', name = 'output1')(l3)
output2 = tf.keras.layers.Dense(10, activation='softmax', name = 'output2')(l3)
output3 = tf.keras.layers.Dense(12, activation='softmax', name = 'output3')(l3)

model = tf.keras.Model(inputs=inputlayer, outputs=[output1, output2, output3])

model.compile(loss={"output1": 'binary_crossentropy',
                    "output2": 'categorical_crossentropy',
                    "output3": 'categorical_crossentropy'},
              optimizer=tf.keras.optimizers.Adam(learning_rate=.01), 
              metrics = METRICS, loss_weights = [1, 1e-1, 1e-1])

y1_train, y2_train, y3_train = tf.random.uniform((50, 1), maxval=2), tf.random.uniform((50, 10), maxval=11), tf.random.uniform((50, 12), maxval=13)
model.fit(tf.random.normal((50, 31)), [y1_train,y2_train,y3_train], batch_size=20, epochs=10)

You need to make sure that y1_train, y2_train, and y3_train are in the correct order and have the correct shape, that is (samples, 1), (samples, 10), and (samples, 12).

  • Related