Query about hyperparameter tuning using keras tuner

train_ds = keras.utils.image_dataset_from_directory(

directory = '/content/train',

labels='inferred',

label_mode = 'binary',

batch_size=32,

image_size=(256,256)

)

validation_ds = keras.utils.image_dataset_from_directory(

directory = '/content/test',

labels='inferred',

label_mode = 'binary',

batch_size=32,

image_size=(256,256)

)

def build_model(hp):

model=Sequential()

counter=0

for i in range(hp.Int(‘Conv_layers’,min_value=1,max_value=4)):

if counter==0:

  model.add(

      Conv2D(

          hp.Int('units'+str(i),min_value=16,max_value=64,step=8),

          kernel_size=(3,3),

          padding=hp.Choice('padding'+str(i),values=['valid','same']),

          activation=hp.Choice('activation'+str(i),values=['tanh','relu','sigmoid']),

          input_shape=(256,256,3)

          )

      )

else:

  model.add(

      Conv2D(

          hp.Int('units'+str(i),min_value=8,max_value=128,step=8),

          kernel_size= (3,3),

          padding=hp.Choice('padding'+str(i),values=['valid','same']),

          activation=hp.Choice('activation'+str(i),values=['tanh','relu','sigmoid']),

          )

      )

  

model.add(BatchNormalization())

model.add(MaxPooling2D(

    pool_size=(2,2),

    strides=hp.Int('units'+str(i),min_value=2,max_value=4,step=1),

    padding=hp.Choice('padding'+str(i),values=['valid','same'])

    )

)

  

counter += 1

for i in range(hp.Int(‘Num_layers’,min_value=1,max_value=10)):

model.add(

    Dense(

        hp.Int('units'+str(i),min_value=8,max_value=128,step=8),

        hp.Choice('activation'+str(i),values=['tanh','relu','sigmoid'])

        )

    )

  

model.add(Dropout(hp.Choice('dropout'+ str(i),values=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9])))

model.add(Dense(1,activation=‘sigmoid’))

optimizer=hp.Choice(‘optimzer’,values = [‘adam’,‘nadam’,‘sgd’,‘rmsprop’,‘adadelta’])

model.compile(optimizer=optimizer,

            loss='binary_crossentropy',

            metrics=['accuracy']

            ) 

return model

tuner=kt.RandomSearch(build_model,

                  objective='val_accuracy',

                  max_trials=5,

                  directory='CatVsDog',

                  project_name='version 1')

Above 3 blocks of code are getting executed but during the execution of below block I’m getting error (logits and labels must have the same shape, received ((None, 1, 1, 1) vs (None, 1))) can any one spot the error or give some suggestions

tuner.search(train_ds,epochs=5,validation_data=validation_ds)

Hi @Roshan_10597, The logits are the raw outputs of a model before they are transformed into probabilities. You’re getting this error as the last layer output shape and the label shape do not match. To resolve this error you have convert your label shape to the model output shape or viceversa. For more details please refer to this gist. Thank You.