Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. import tensorflow as tf
  2.  
  3. class myCallback(tf.keras.callbacks.Callback):
  4. def on_epoch_end(self, epoch, logs={}):
  5. if(logs.get('acc')>0.99):
  6. print("nReached 99% accuracy so cancelling training!")
  7. self.model.stop_training = True
  8.  
  9. mnist = tf.keras.datasets.mnist
  10.  
  11. (x_train, y_train),(x_test, y_test) = mnist.load_data()
  12. x_train, x_test = x_train / 255.0, x_test / 255.0
  13.  
  14. callbacks = myCallback()
  15.  
  16. model = tf.keras.models.Sequential([
  17. tf.keras.layers.Flatten(input_shape=(28, 28)),
  18. tf.keras.layers.Dense(512, activation=tf.nn.relu),
  19. tf.keras.layers.Dense(10, activation=tf.nn.softmax)
  20. ])
  21. model.compile(optimizer='adam',
  22. loss='sparse_categorical_crossentropy',
  23. metrics=['accuracy'])
  24.  
  25. model.fit(x_train, y_train, epochs=10, callbacks=[callbacks])
  26.  
  27. Epoch 1/10
  28. 59296/60000 [============================>.] - ETA: 0s - loss: 0.2005 - accuracy: 0.9400
  29.  
  30. ---------------------------------------------------------------------------
  31. TypeError Traceback (most recent call last)
  32. <ipython-input-26-f5e673b24d24> in <module>()
  33. 23 metrics=['accuracy'])
  34. 24
  35. ---> 25 model.fit(x_train, y_train, epochs=10, callbacks=[callbacks])
  36.  
  37. C:Program Files (x86)Microsoft Visual StudioSharedAnaconda3_64libsite-packagestensorflowpythonkerasenginetraining.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
  38. 871 validation_steps=validation_steps,
  39. 872 validation_freq=validation_freq,
  40. --> 873 steps_name='steps_per_epoch')
  41. 874
  42. 875 def evaluate(self,
  43.  
  44. C:Program Files (x86)Microsoft Visual StudioSharedAnaconda3_64libsite-packagestensorflowpythonkerasenginetraining_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq, mode, validation_in_fit, prepared_feed_values_from_dataset, steps_name, **kwargs)
  45. 406 if mode == ModeKeys.TRAIN:
  46. 407 # Epochs only apply to `fit`.
  47. --> 408 callbacks.on_epoch_end(epoch, epoch_logs)
  48. 409 progbar.on_epoch_end(epoch, epoch_logs)
  49. 410
  50.  
  51. C:Program Files (x86)Microsoft Visual StudioSharedAnaconda3_64libsite-packagestensorflowpythonkerascallbacks.py in on_epoch_end(self, epoch, logs)
  52. 288 logs = logs or {}
  53. 289 for callback in self.callbacks:
  54. --> 290 callback.on_epoch_end(epoch, logs)
  55. 291
  56. 292 def on_train_batch_begin(self, batch, logs=None):
  57.  
  58. <ipython-input-26-f5e673b24d24> in on_epoch_end(self, epoch, logs)
  59. 3 class myCallback(tf.keras.callbacks.Callback):
  60. 4 def on_epoch_end(self, epoch, logs={}):
  61. ----> 5 if(logs.get('acc')>0.99):
  62. 6 print("nReached 99% accuracy so cancelling training!")
  63. 7 self.model.stop_training = True
  64.  
  65. TypeError: '>' not supported between instances of 'NoneType' and 'float'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement