TypeError: Exception encountered when calling layer "dropout" (type Dropout)

1.2k Views Asked by At

I have the following code while building a CNN model with VGG16 and Keras. I have added three convolution layers and three pool layers. While compiling the model a value error arises from the pooling layer. I have added the code and error. please help

def vgg16_model(num_classes=None):
    
    model = VGG16(weights='imagenet', include_top=True, input_shape=(224,224,3))
    
    x=Dense(1024, activation='relu')(model.layers[-4].output) # Add my own dense layer after the last conv block
    x=Dropout(0,7)(x)
    x=Dense(512,activation='relu')(x)
    x=Dropout(0,5)(x)
    x=Dense(2,activation='softmax')(x)
    model=Model(model.input,x)
    
    return model

vgg_conv=vgg16_model(10)
for layer in vgg_conv.layers[:-10]: # Freeze all layers except the last 10
    layer.trainable = False

opt = Adam(learning_rate=0.0001, decay=1e-5)
vgg_conv.compile(loss='sparse_categorical_crossentropy', metrics=['accuracy'],optimizer=opt)

# Fit the model
history = vgg_conv.fit_generator(train_data_gen, epochs=nb_epochs, steps_per_epoch=nb_train_steps, validation_data=(val_data,val_labels), class_weight={0:1.0, 1:0.4})

I am getting the following error :

File "Python\Python310\lib\site-packages\keras\engine\training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "Python\Python310\lib\site-packages\keras\engine\training.py", line 1010, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "Python\Python310\lib\site-packages\keras\engine\training.py", line 1000, in run_step  **
        outputs = model.train_step(data)
    File "Python\Python310\lib\site-packages\keras\engine\training.py", line 859, in train_step
        y_pred = self(x, training=True)
    File "Python\Python310\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "Python\Python310\lib\site-packages\keras\layers\core\dropout.py", line 99, in _get_noise_shape
        for i, value in enumerate(self.noise_shape):

    TypeError: Exception encountered when calling layer "dropout" (type Dropout).
0

There are 0 best solutions below