Jake P
Jake P

Reputation: 480

Dynamic number of epochs with a tensorflow keras model

I want to have a neural net that trains until a certain level of accuracy has been reached. Is there a built in function to use instead of running each epoch individually until the accuracy has been reached?

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.compile(optimizer=tf.train.AdamOptimizer(), loss='sparse_categorical_crossentropy', metrics=['accuracy'])

epochs = 0
train_acc = 0
while 1-train_acc > .01:
    model.fit(train_images, train_labels,  initial_epoch=epochs, epochs=epochs+1,verbose=0)
    epochs += 1
    train_loss, train_acc = model.evaluate(train_images,train_labels)

Upvotes: 1

Views: 1458

Answers (1)

rvinas
rvinas

Reputation: 11895

No, there isn't any built in function to do this. However, you can easily define a custom callback that stops training once the training accuracy reaches a certain threshold:

import keras


class AccuracyStopping(keras.callbacks.Callback):
    def __init__(self, acc_threshold):
        super(AccuracyStopping, self).__init__()
        self._acc_threshold = acc_threshold

    def on_epoch_end(self, batch, logs={}):
        train_acc = logs.get('acc')
        self.model.stop_training = 1 - train_acc <= self._acc_threshold

Here's a simple example showing how to use it:

import numpy as np
from keras.layers import Dense
from keras.models import Sequential

x = np.random.normal(size=(100,))
y = x > 0

model = Sequential()
model.add(Dense(1, input_dim=1, activation='sigmoid'))
model.compile('sgd', 'binary_crossentropy', metrics=['accuracy'])

acc_callback = AccuracyStopping(0.05)
model.fit(x, y, batch_size=8, epochs=1000, callbacks=[acc_callback])

Upvotes: 3

Related Questions