0
$\begingroup$

I am Trying to implement Multitask Learning in Keras with share constitutional Layers and task specific FC layers. Following is my model:

input1=Input(shape=(8,8,128))
conv1=Conv2D(128,kernel_size=3, strides=1,
                 padding='SAME', use_bias=True, 
                 activation='relu',kernel_initializer='glorot_normal')

conv2=Conv2D(128,kernel_size=3, strides=1,
                  padding='SAME', use_bias=True, 
                  activation='relu',input_shape=[8,8,128],kernel_initializer='glorot_normal')
maxpool1=MaxPooling2D(pool_size=2,strides=2)

conv3=Conv2D(64,kernel_size=3, strides=1,
                 padding='SAME', use_bias=True, 
                 activation='relu',kernel_initializer='glorot_normal')
conv4=Conv2D(64,kernel_size=3, strides=1,
                  padding='SAME', use_bias=True, 
                  activation='relu',input_shape=[8,8,128],kernel_initializer='glorot_normal')

maxpool2=MaxPooling2D(pool_size=2,strides=2)
model=conv1(input1)
model=conv2(model)
model=maxpool1(model)
model=conv3(model)
model=conv4(model)
model=maxpool2(model)
model=Flatten()(model)

exten1=Dense(1024, activation='relu')(model)
exten1=Dropout(0.2)(exten1)
exten1=Dense(512, activation='relu')(exten1)
exten1=Dropout(0.2)(exten1)
exten1=Dense(256, activation='relu')(exten1)
exten1=Dense(2, activation='sigmoid',name='output1')(exten1)

exten2=Dense(1024, activation='relu')(model)
exten2=Dropout(0.2)(exten2)
exten2=Dense(512, activation='relu')(exten2)
exten2=Dropout(0.2)(exten2)
exten2=Dense(256, activation='relu')(exten2)
exten2=Dense(2, activation='sigmoid',name='output2')(exten2)


model = Model(inputs=[input1], outputs=[exten1, exten2])


model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

Model summary is:

Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_4 (InputLayer)            (None, 8, 8, 128)    0                                            
__________________________________________________________________________________________________
conv2d_24 (Conv2D)              (None, 8, 8, 128)    147584      input_4[0][0]                    
__________________________________________________________________________________________________
conv2d_25 (Conv2D)              (None, 8, 8, 128)    147584      conv2d_24[0][0]                  
__________________________________________________________________________________________________
max_pooling2d_12 (MaxPooling2D) (None, 4, 4, 128)    0           conv2d_25[0][0]                  
__________________________________________________________________________________________________
conv2d_26 (Conv2D)              (None, 4, 4, 64)     73792       max_pooling2d_12[0][0]           
__________________________________________________________________________________________________
conv2d_27 (Conv2D)              (None, 4, 4, 64)     36928       conv2d_26[0][0]                  
__________________________________________________________________________________________________
max_pooling2d_13 (MaxPooling2D) (None, 2, 2, 64)     0           conv2d_27[0][0]                  
__________________________________________________________________________________________________
flatten_2 (Flatten)             (None, 256)          0           max_pooling2d_13[0][0]           
__________________________________________________________________________________________________
dense_14 (Dense)                (None, 1024)         263168      flatten_2[0][0]                  
__________________________________________________________________________________________________
dense_17 (Dense)                (None, 1024)         263168      flatten_2[0][0]                  
__________________________________________________________________________________________________
dropout_8 (Dropout)             (None, 1024)         0           dense_14[0][0]                   
__________________________________________________________________________________________________
dropout_10 (Dropout)            (None, 1024)         0           dense_17[0][0]                   
__________________________________________________________________________________________________
dense_15 (Dense)                (None, 512)          524800      dropout_8[0][0]                  
__________________________________________________________________________________________________
dense_18 (Dense)                (None, 512)          524800      dropout_10[0][0]                 
__________________________________________________________________________________________________
dropout_9 (Dropout)             (None, 512)          0           dense_15[0][0]                   
__________________________________________________________________________________________________
dropout_11 (Dropout)            (None, 512)          0           dense_18[0][0]                   
__________________________________________________________________________________________________
dense_16 (Dense)                (None, 256)          131328      dropout_9[0][0]                  
__________________________________________________________________________________________________
dense_19 (Dense)                (None, 256)          131328      dropout_11[0][0]                 
__________________________________________________________________________________________________
output1 (Dense)                 (None, 2)            514         dense_16[0][0]                   
__________________________________________________________________________________________________
output2 (Dense)                 (None, 2)            514         dense_19[0][0]                   
==================================================================================================
Total params: 2,245,508
Trainable params: 2,245,508
Non-trainable params: 0

This is where i am facing Problems

model.fit(X_train,{"output1":label1, "output2":label2},batch_size=10,verbose=1 ,callbacks=[checkpoint],validation_data=(X_test,y_test),epochs=40)

the error i get is

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 2 array(s), but instead got the following list of 1 arrays

While both label1 and label2 are of same shape (2077,2). Is there something i am missing?

$\endgroup$

1 Answer 1

2
$\begingroup$

You need to pass an array object. You don't need to map the label array with the output node name ( like TensorFlow low-level APIs ). Pass the list containing the label arrays directly.

model.fit(X_train, [ label1, label2 ] ,batch_size=10,verbose=1 ,callbacks=[checkpoint],validation_data=(X_test,y_test),epochs=40)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.