1
$\begingroup$

I have a dataset of 2 classes, both containing 2K images. I have split that into 1500 images for training and 500 images for validation.

This is a simple structure for testing purposes, and each image is classified depending on the colour of a particular pixel. Either Green or Red.

I have run this model many times and I find that sometimes the models gets low loss/ high accuracy within a few epochs, but other times it gets stuck at accuracy 50%.

The datasets are exactly the same each time with only difference coming from model.fit "shuffle" option.

I tested the LR Range first:

enter image description here

and I "cycle" the learning rate through an appropriate range.

model = keras.Sequential([
        keras.layers.Dense(112, activation=tf.nn.relu, input_shape=(224, 224, 3)),
        keras.layers.Dropout(0.4),
        keras.layers.Flatten(),
        keras.layers.Dense(3, activation=tf.nn.softmax)
    ])


LRS = CyclicLR(base_lr=0.000005, max_lr=0.0003, step_size=200.)

model.fit(train_images, train_labels, shuffle=True, epochs=10,
              callbacks=[checkpoint,
                         LRS],
              validation_data = (test_images, test_labels)
              )

Why does the model sometimes NOT get a good fit?

EDIT 1

Re Serali's suggestion:

myOpt = keras.optimizers.Adam(lr=0.001,decay=0.01)
model.compile(optimizer=myOpt, loss='categorical_crossentropy',  metrics=['accuracy'])

reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.2,verbose=1,patience=5, min_lr=0.00001)
$\endgroup$

2 Answers 2

0
$\begingroup$

Shuffle = True should give better results particularly when you are running for more epochs. But I am not clear why there is so huge difference in the accuracy when shuffle changes . One thing you can try is , to increase the number of epochs and see if the accuracy improves. For shuffle = False

Shuffle set to false, allows you to use the previously trained data. Setting this to true means that you either want to retrain or set the epoch to some value greater than 10. To learn, but this increases the chances of memorization (over fitting)

$\endgroup$
1
  • $\begingroup$ I think shuffle=true only happens once at start of fitting - not at each epoch. That was my impression. $\endgroup$
    – ManInMoon
    Commented Nov 1, 2019 at 15:22
0
$\begingroup$

Have you tried without the "CycleLR" part? It is obviously a good idea but as far as I know, not a standard Keras function - needs to be implemented separately. Any small error in this implementation might cause problems.

$\endgroup$
5
  • $\begingroup$ No, not recently. I guess I could try that but it would mean choosing a fixed LR. Given the graph above - what would you suggest? $\endgroup$
    – ManInMoon
    Commented Nov 1, 2019 at 15:24
  • $\begingroup$ Assuming you are using Adam optimizer, I would start with default value of 0.001 and use LearningRateScheduler to gradually decrease LR once the loss value reaches a plateau. Keras documentation for callbacks: keras.io/callbacks $\endgroup$
    – serali
    Commented Nov 1, 2019 at 16:16
  • $\begingroup$ See EDIT 1 please. Is this what you had in mind? $\endgroup$
    – ManInMoon
    Commented Nov 1, 2019 at 16:49
  • $\begingroup$ Something like that, but actually if you are training for only for 10 epochs, you don't need the reducelr part. And you can simply set the minimum value to zero. $\endgroup$
    – serali
    Commented Nov 1, 2019 at 17:32
  • $\begingroup$ I tried it with 100 epochs, and it still did not get a fit. Then I ran exactly same model/data set-up again and it found a fit after 3 epochs. My model must be getting stuck in a local minima. I am surprised that your suggestion, or my original CyclicLR don't manage to work around this... $\endgroup$
    – ManInMoon
    Commented Nov 4, 2019 at 9:49

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.