Reputation: 41
I am training a Fashion MNIST data using CNN. Dut to overfitting I tried to add Dropout layer. But it's not working
Before I added Dropout the model was working fine.
def fashion_model()
batch_size = 64
epochs = 20
num_classes = 10
fashion_drop_model = Sequential()
fashion_drop_model.add(Conv2D(32, kernel_size=(3, 3),activation='linear',padding='same',input_shape=(28,28,1)))
fashion_drop_model.add(LeakyReLU(alpha=0.1))
fashion_drop_model.add(MaxPooling2D((2, 2),padding='same'))
fashion_drop_model.add(Dropout(0.25))
fashion_drop_model.add(Conv2D(64, (3, 3), activation='linear',padding='same'))
fashion_drop_model.add(LeakyReLU(alpha=0.1))
fashion_drop_model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
fashion_drop_model.add(Dropout(0.25))
fashion_drop_model.add(Conv2D(128, (3, 3), activation='linear',padding='same'))
fashion_drop_model.add(LeakyReLU(alpha=0.1))
fashion_drop_model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
fashion_drop_model.add(Dropout(0.4))
fashion_drop_model.add(Flatten())
fashion_drop_model.add(Dense(128, activation='linear'))
fashion_drop_model.add(LeakyReLU(alpha=0.1))
fashion_drop_model.add(Dropout(0.3))
fashion_drop_model.add(Dense(num_classes, activation='softmax'))
return fashion_drop_model.summary()
fashion_model()
The error that I'm getting is: UnboundLocalError: local variable 'a' referenced before assignment
PS: After a brief walkthrough of the code line-by-line, I figured the error was creeping in line8 (fashion_drop_model.add(Dropout(0.25))
)
Upvotes: 3
Views: 7021
Reputation: 104464
You are missing a colon in your Python function definition:
def fashion_model(): #<--
After you to this, the code should run. Running this in Google Colaboratory, you'll see that a summary of your model is produced:
It is highly discouraged to use Dropout layers after Convolutional layers. The whole point of Convolutional layers is to exploit pixels within a spatial neighbourhood to extract the right features to feed into Dense layers. Dropout would destroy this relationship and thus prevent your model from successfully learning these features.
See this discussion on Reddit for more details: https://www.reddit.com/r/MachineLearning/comments/42nnpe/why_do_i_never_see_dropout_applied_in/
Upvotes: 7