我希望将功能添加到我的代码中,这样,如果我希望在任何时候终止代码,它将安全地终止训练并保存变量。尽管我已经尝试过寻找更好的解决方案,但我认为捕获KeyboardInterrupt异常将是我最好的选择。
然而,它会安全吗?更具体地说,以下代码是否可以工作:
with tf.Session() as sess
try:
for i in range(FLAGS.max_steps):
sess.run(train_op, feed_dict=some_feed_dictionary)
# Some other summary writing and evaluative operations
except KeyboardInterrupt:
print("Manual interrupt occurred.")
print('Done training for {} steps'.format(global_steps))
save_path = saver.save(sess, 'Standard CNN', global_step=global_steps, write_meta_graph=False)或者它是不安全的,并可能导致损坏的保存文件,因为键盘中断可以自由地发生在任何tensorflow操作的中间?有足够的方法做到这一点吗?
发布于 2017-07-11 19:51:27
我个人使用的东西与此非常相似,在训练过程中一直捕获KeyboardInterrupt,唯一的区别是我在每个sess.run步骤(或这些步骤中的每几个步骤)后“保存”,从未遇到过问题。
我不知道“它是否不安全”的答案,但我知道我的方法有效地避免了问这个问题……
在您的代码中,如下所示:
with tf.Session() as sess
try:
for i in range(FLAGS.max_steps):
sess.run(train_op, feed_dict=some_feed_dictionary)
# Some other summary writing and evaluative operations
if i % save_steps == 0:
save_path = saver.save(sess, 'Standard CNN', global_step=global_steps, write_meta_graph=False)
except KeyboardInterrupt:
print("Manual interrupt occurred.")
print('Done training for {} steps'.format(global_steps))为了清楚起见,save_steps变量决定了两次保存之间的步数。
https://stackoverflow.com/questions/45033413
复制相似问题