TensorFlow-当损失达到定义值时停止训练

时间:2018-07-20 10:07:06

标签: python tensorflow keras

我使用第一个示例here作为网络示例。

当损失达到固定值时如何停止训练?

例如,我想修复最多3000个纪元,并且当损失小于0.2时,培训将停止。

我读了topic,但这不是我找到的解决方法。

我希望在损失达到一定值时停止训练,而不是像先前主题中的this function那样没有任何改善时停止训练。

代码如下:

import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD

# Generate dummy data
import numpy as np
x_train = np.random.random((1000, 20))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)
x_test = np.random.random((100, 20))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)

model = Sequential()
# Dense(64) is a fully-connected layer with 64 hidden units.
# in the first layer, you must specify the expected input data shape:
# here, 20-dimensional vectors.
model.add(Dense(64, activation='relu', input_dim=20))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])

model.fit(x_train, y_train,
          epochs=3000,
          batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)  

2 个答案:

答案 0 :(得分:0)

此处的文档:EarlyStopping

keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto', baseline=None)

答案 1 :(得分:0)

如果要切换到TensorFlow 2.0,可以使用类似这样的方法:

class haltCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
    if(logs.get('loss') <= 0.05):
        print("\n\n\nReached 0.05 loss value so cancelling training!\n\n\n")
        self.model.stop_training = True

您只需要创建一个这样的回调,然后将该回调添加到您的model.fit中,这样它就会变成这样:

model.fit(x_train, y_train,
      epochs=3000,
      batch_size=128,
      callbacks=['trainingStopCallback'])

通过这种方式,只要拟合下降到0.05以下(或定义时输入的任何值),都应停止拟合。

此外,由于您已经问了很长时间了,但是对于将其与TensorFlow 2.0结合使用仍然没有实际答案,因此我将您的代码段更新为TensorFlow 2.0,这样每个人现在都可以轻松地在新项目中查找和使用它

import tensorflow as tf

# Generate dummy data
import numpy as np


x_train = np.random.random((1000, 20))
y_train = tf.keras.utils.to_categorical(
    np.random.randint(10, size=(1000, 1)), num_classes=10)
x_test = np.random.random((100, 20))
y_test = tf.keras.utils.to_categorical(
    np.random.randint(10, size=(100, 1)), num_classes=10)

model = tf.keras.models.Sequential()
# Dense(64) is a fully-connected layer with 64 hidden units.
# in the first layer, you must specify the expected input data shape:
# here, 20-dimensional vectors.
model.add(tf.keras.layers.Dense(64, activation='relu', input_dim=20))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(64, activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(10, activation='softmax'))


class haltCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs={}):
        if(logs.get('loss') <= 0.05):
            print("\n\n\nReached 0.05 loss value so cancelling training!\n\n\n")
            self.model.stop_training = True


trainingStopCallback = haltCallback()

sgd = tf.keras.optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy', 'loss'])

model.fit(x_train, y_train,
          epochs=3000,
          batch_size=128,
          callbacks=['trainingStopCallback'])
score = model.evaluate(x_test, y_test, batch_size=128)