1

As part of a TF 2.0 tutorial, I was trying out the callback function in the TensorFlow which enables the model to stop training when a specific accuracy or loss value is reached. The example provided in this Colab works fine. I tried to run a similar example locally using pycharm(with tf gpu conda env) but the callback function is not executed at all and runs till the last epoch. There is no error whatsoever and the codes looks same.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from matplotlib import pyplot as plt
from tensorflow.keras.callbacks import Callback


class MyCallback(Callback):
    def on_epochs_end(self, epoch, logs={}):
        if(logs.get('accuracy') > 0.9):
            print("\n Training stopping now. accuracy reached 90 !")
            self.model.stop_training = True


callback = MyCallback()

# Input data
(training_data, training_labels), (testing_data, testing_labels) = fashion_mnist.load_data()
training_data = training_data / 255.0
testing_data = testing_data / 255.0
plt.imshow(training_data[0], cmap='gray')

# Network
model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(units=128, activation='relu'),
    Dense(units=10, activation='softmax')])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(training_data, training_labels, epochs=25, callbacks=[callback])

I was referring to different examples for some solutions and I came across statements like
- activation='relu'
- activation=tf.nn.relu
- activation=tf.keras.activation.relu

Which is the right one to use? Is the error caused due to incorrect imports?

If anyone could give some hints, it would be helpful.

1 Answer 1

1

The error is due to a typo in your callback class. In the definition of on_epoch_end function you had a typo as on_epochs_end. Other than that everything is correct.

class MyCallback(Callback):
 #def on_epochs_end(self, epoch, logs={}): # should be epoch (not epochs)
  def on_epoch_end(self, epoch, logs={}):
    if(logs.get('accuracy') > 0.9):
      print("\n Training stopping now. accuracy reached 90 !")
      self.model.stop_training = True

Full code is here for your reference.

Sign up to request clarification or add additional context in comments.

3 Comments

It was a silly mistake. A part of the function name was auto-filled by pycharm, so I didn't bother to check it there. Thank you!
Can you provide some links that will help clear up about the different versions of the activation function usage that I have mentioned in the post ?
Mostly I look at tutorials and guidelines described in tensorflow and keras websites. I also followed several resources searched through google. Here are couple of them but do little more search. keras.io/layers/core, towardsdatascience.com/…, missinglink.ai/guides/neural-network-concepts/…. Hope this helps

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.