首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TensorFlow如何安全地手动终止培训(KeyboardInterrupt)

TensorFlow如何安全地手动终止培训(KeyboardInterrupt)
EN

Stack Overflow用户
提问于 2017-07-11 19:34:07
回答 1查看 2.3K关注 0票数 5

我希望将功能添加到我的代码中,这样,如果我希望在任何时候终止代码,它将安全地终止训练并保存变量。尽管我已经尝试过寻找更好的解决方案,但我认为捕获KeyboardInterrupt异常将是我最好的选择。

然而,它会安全吗?更具体地说,以下代码是否可以工作:

代码语言:javascript
复制
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操作的中间?有足够的方法做到这一点吗?

EN

回答 1

Stack Overflow用户

发布于 2017-07-11 19:51:27

我个人使用的东西与此非常相似,在训练过程中一直捕获KeyboardInterrupt,唯一的区别是我在每个sess.run步骤(或这些步骤中的每几个步骤)后“保存”,从未遇到过问题。

我不知道“它是否不安全”的答案,但我知道我的方法有效地避免了问这个问题……

在您的代码中,如下所示:

代码语言:javascript
复制
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变量决定了两次保存之间的步数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45033413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档