Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TensorFlow如何安全地手动终止培训(键盘中断)_Python_Tensorflow_Neural Network - Fatal编程技术网

Python TensorFlow如何安全地手动终止培训(键盘中断)

Python TensorFlow如何安全地手动终止培训(键盘中断),python,tensorflow,neural-network,Python,Tensorflow,Neural Network,我希望在我的代码中添加功能,这样,如果我希望在任何时候终止代码,它将安全地终止培训并保存变量。虽然我已经尝试寻找更好的解决方案,但我认为捕获键盘中断异常将是我最好的选择 然而,它会安全吗?更具体地说,以下代码是否有效: with tf.Session() as sess try: for i in range(FLAGS.max_steps): sess.run(train_op, feed_dict=some_feed_dictionar

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

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

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)

或是不安全,是否会导致损坏的保存文件,考虑到在任何TysFoad操作的中间都可以发生键盘中断吗?有没有合适的方法可以做到这一点?

我个人一直在训练中捕捉
键盘中断来使用类似的方法,唯一的区别是每次
sess后我都会“保存”。运行
步骤(或其中的每几步),从来没有遇到过问题

我不知道“是否不安全”的答案,但我知道我的方法可以避免问这个问题

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

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\u steps
变量确定两次保存之间的步骤数