Earlier when you trained for extra epochs you had an issue where your loss might change. It might have taken a bit of time for you to wait for the training to do that, and you might have thought 'wouldn't it be nice if I could stop the training when I reach a desired value?' -- i.e. 95% accuracy might be enough for you, and if you reach that after 3 epochs, why sit around waiting for it to finish a lot more epochs....So how would you fix that? Like any other program...you have callbacks! Let's see them in action...
Copy import tensorflow as tf
print (tf. __version__ )
class myCallback ( tf . keras . callbacks . Callback ):
def on_epoch_end ( self , epoch , logs = {} ):
if (logs . get ( 'loss' ) < 0.4 ) :
print ( "\nReached 60 % a ccuracy so cancelling training!" )
self . model . stop_training = True
callbacks = myCallback ()
mnist = tf . keras . datasets . fashion_mnist
(train_images , training_labels) , (test_images , test_labels) = mnist . load_data ()
train_images = train_images / 255.0
test_images = test_images / 255.0
model = tf . keras . models . Sequential ([
tf.keras.layers. Flatten (),
tf.keras.layers. Dense ( 512 , activation = tf.nn.relu),
tf.keras.layers. Dense ( 10 , activation = tf.nn.softmax)
])
model . compile (optimizer = 'adam' , loss = 'sparse_categorical_crossentropy' )
model . fit (train_images, training_labels, epochs = 5 , callbacks = [callbacks])
Copy 1.13.1
Epoch 1/5
60000/60000 [==============================] - 19s 316us/sample - loss: 0.4767
Epoch 2/5
59872/60000 [============================>.] - ETA: 0s - loss: 0.3590
Reached 60% accuracy so cancelling training!
60000/60000 [==============================] - 17s 279us/sample - loss: 0.3588
<tensorflow.python.keras.callbacks.History at 0x7faf5dc59110>
Copy import keras
class Generator(keras.utils.Sequence):
def __init__(self, data_file):
self.data_file = data_file
self.length = -1
def __iter__(self):
while True:
with open(self.data_file, 'r') as f:
reader = csv.reader(f)
for row in reader:
yield row[0], row[1]
def __len__(self):
if self.length == -1:
n_rows = 0
with open(self.data_file, 'r') as f:
reader = csv.reader(f)
for row in reader:
n_rows += 1
self.length = n_rows
return self.length