Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: '>' not supported between instances of 'NoneType' and 'float'

I have this code and it raise an error in python 3 and such a comparison can work on python 2 how can I change it?

import tensorflow as tf 
def train_set():
    class MyCallBacks(tf.keras.callbacks.Callback):
        def on_epoch_end(self,epoch,logs={}):
            if(logs.get('acc')>0.95):
                print('the training will stop !')
                self.model.stop_training=True
    callbacks=MyCallBacks()
    mnist_dataset=tf.keras.datasets.mnist 
    (x_train,y_train),(x_test,y_test)=mnist_dataset.load_data()
    x_train=x_train/255.0
    x_test=x_test/255.0
    classifier=tf.keras.Sequential([
                                    tf.keras.layers.Flatten(input_shape=(28,28)),
                                    tf.keras.layers.Dense(512,activation=tf.nn.relu),
                                    tf.keras.layers.Dense(10,activation=tf.nn.softmax)
                                    ])
    classifier.compile(
                        optimizer='sgd',
                        loss='sparse_categorical_crossentropy',
                        metrics=['accuracy']
                       )    
    history=classifier.fit(x_train,y_train,epochs=20,callbacks=[callbacks])
    return history.epoch,history.history['acc'][-1]
train_set()
like image 590
Ouismed Avatar asked Jan 18 '20 16:01

Ouismed


2 Answers

Tensorflow 2.0

DESIRED_ACCURACY = 0.979

class myCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epochs, logs={}) :
        if(logs.get('acc') is not None and logs.get('acc') >= DESIRED_ACCURACY) :
            print('\nReached 99.9% accuracy so cancelling training!')
            self.model.stop_training = True

callbacks = myCallback()
like image 51
Haseeb Avatar answered Oct 11 '22 20:10

Haseeb


it seems that your error is similar to Exception with Callback in Keras - Tensorflow 2.0 - Python try replacing logs.get('acc') with logs.get('accuracy')

like image 16
Logan Anderson Avatar answered Oct 11 '22 22:10

Logan Anderson