简体   繁体   English

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

[英]TensorFlow - Stop training when losses reach a defined value

I used the first example here as an example of network.这里用第一个例子作为网络的例子。

How to stop the training when the loss reach a fixed value ?当损失达到固定值时如何停止训练?

So, for example, I would like to fix a maximum of 3000 epochs and the training will stop when the loss will be under 0.2.因此,例如,我想固定最多 3000 个 epoch,当损失低于 0.2 时,训练将停止。

I read this topic but it is not the solution I found.我阅读了这个主题,但这不是我找到的解决方案。

I would want to stop the training when the loss reach a value, not when there is no improvement like with this function proposed in the precedent topic.我想在损失达到某个值时停止训练,而不是在没有像前面主题中提出的这个函数那样有任何改进时停止训练。

Here is the code:这是代码:

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)  

You can use some method like this if you would switch to TensorFlow 2.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

You just need to create a callback like that and then add that callback to your model.fit so it becomes something like this:您只需要创建一个这样的回调,然后将该回调添加到您的 model.fit 中,它就会变成这样:

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

This way, the fitting should stop whenever it reaches down below 0.05 (or whatever value you put on while defining it).这样,只要低于 0.05(或您在定义时设置的任何值),拟合就应停止。

Also, since it's been a long time you asked this question but it still has no actual answer for using it with TensorFlow 2.0, I updated your code snippet to TensorFlow 2.0 so everyone can now easily find and use it with their new projects.此外,由于您问这个问题已经很长时间了,但仍然没有将它与 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)

此处的文档: EarlyStopping

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Tensorflow中训练网络时忽略特定的输入损耗 - Disregarding specific input losses when training network in Tensorflow 当达到特定的损失和准确度值时,如何停止 tflearn 训练时期或迭代? - How to stop tflearn training epoch or iteration when it reach a specific loss and accuracy value? Tensorflow 训练 - 打印一个 output 的多个损失 - Tensorflow training - print multiple losses for one output Tensorflow 停止并恢复训练 - Tensorflow stop and resume training 错误培训Cifar-10模型Tensorflow-精度为0,将不会优化并且不会报告损失 - Error training Cifar-10 Model Tensorflow - Accuracy is 0 and will not optimize and losses not reported 有没有办法在使用 tensorflow 的 epoch 中停止训练? - Is there a way to stop training in the middle of an epoch with tensorflow? 如何停止 tensorflow 中的培训工作? - How can I stop a training job in tensorflow? 当我停止在 jupyter 中训练时,学习率会重置吗? Tensorflow.Keras - Does the learning rate reset when I stop training in jupyter? Tensorflow.Keras 训练Tensorflow LinearClassifier时发生TypeError - TypeError when training Tensorflow LinearClassifier 类型错误:无法转换值<tensorflow.python.keras.losses.categoricalcrossentropy object ...>到 TensorFlow DType</tensorflow.python.keras.losses.categoricalcrossentropy> - TypeError: Cannot convert value <tensorflow.python.keras.losses.CategoricalCrossentropy object ...> to a TensorFlow DType
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM