gpt4 book ai didi

python - 当达到特定的损失和精度值时,如何停止 tflearn 训练时期或迭代?

转载 作者:太空宇宙 更新时间:2023-11-04 04:23:00 26 4
gpt4 key购买 nike

我有一个模型可以使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com