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()
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()
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With