我有一个使用tflearn库训练的模型,我使用深度神经网络(DNN)来做到这一点。我们可以在这里看到更多(http://tflearn.org/models/dnn/

下面是我的代码:

# Build neural network
net = tflearn.input_data(shape=[None, len(train_x[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(train_y[0]), activation='softmax')
net = tflearn.regression(net)

# Define model and setup tensorboard
model = tflearn.DNN(net, tensorboard_dir='tflearn_logs', best_val_accuracy=0.91)
# Start training (apply gradient descent algorithm)
model.fit(train_x, train_y, n_epoch=350, batch_size=8, show_metric=True)
model.save('model.tflearn')


当我运行该代码时,直到纪元结束,我都会得到一些类似的值:

Training Step: 5083  | total loss: 0.31890 | time: 0.302s
| Adam | epoch: 085 | loss: 0.31890 - acc: 0.8948 -- iter: 344/474
Training Step: 20999  | total loss: 0.08880 | time: 0.366s
....
Training Step: 11279  | total loss: 0.10708 | time: 0.419s
| Adam | epoch: 188 | loss: 0.10708 - acc: 0.9556 -- iter: 472/474
Training Step: 11280  | total loss: 0.12302 | time: 0.425s
| Adam | epoch: 188 | loss: 0.12302 - acc: 0.9351 -- iter: 474/474
....
| Adam | epoch: 350 | loss: 0.08880 - acc: 0.9503 -- iter: 472/474
Training Step: 21000  | total loss: 0.08863 | time: 0.373s
| Adam | epoch: 350 | loss: 0.08863 - acc: 0.9553 -- iter: 474/474


任何人都知道每次损失和准确性达到特定值时如何停止训练?假设损失0.05,准确性为0.95。
提前致谢

最佳答案

通过作为fit方法的参数提供的回调实例使用Early Stopping,如下所示:

http://mckinziebrandon.me/TensorflowNotebooks/2016/11/20/early-stopping.html

这样的事情应该在准确性达到0.95时停止训练

class EarlyStoppingCallback(tflearn.callbacks.Callback):
    def __init__(self, val_acc_thresh):
        """ Note: We are free to define our init function however we please. """
        self.val_acc_thresh = val_acc_thresh

    def on_epoch_end(self, training_state):
        """ """
        # Apparently this can happen.
        if training_state.val_acc is None: return
        if training_state.val_acc > self.val_acc_thresh:
            raise StopIteration

# Initializae our callback.
early_stopping_cb = EarlyStoppingCallback(val_acc_thresh=0.95)
# Give it to our trainer and let it fit the data.
trainer.fit(feed_dicts={X: trainX, Y: trainY},
            val_feed_dicts={X: testX, Y: testY},
            n_epoch=2,
            show_metric=True, # Calculate accuracy and display at every step.
            snapshot_epoch=False,
            callbacks=early_stopping_cb)

关于python - 当达到特定的损失和准确性值时,如何停止tflearn训练时期或迭代?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54108317/

10-13 02:26