Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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 keras.estimator.model_to_estimator-无法热启动或加载以前的检查点_Python_Tensorflow - Fatal编程技术网

Python keras.estimator.model_to_estimator-无法热启动或加载以前的检查点

Python keras.estimator.model_to_estimator-无法热启动或加载以前的检查点,python,tensorflow,Python,Tensorflow,我正在训练一个张量流模型,使用keras模型的估计函数,然后用训练数据进行训练。这很好,然后我可以继续使用测试数据成功地预测 在一个单独的例行程序中,我希望能够使用最新的训练检查点加载预先训练的估计器,并进行更多的预测(即,无需重新训练)。我已经从中查看了warm\u start\u,但在加载keras模型时,这似乎不可用。我的理解是,我可以从同一个keras模型创建一个新的估计器,第一次我预测它将从我指定的目录加载检查点 下面的代码片段是我的尝试(最终estimator_model2将加载到一

我正在训练一个张量流模型,使用keras模型的估计函数,然后用训练数据进行训练。这很好,然后我可以继续使用测试数据成功地预测

在一个单独的例行程序中,我希望能够使用最新的训练检查点加载预先训练的估计器,并进行更多的预测(即,无需重新训练)。我已经从中查看了
warm\u start\u,但在加载keras模型时,这似乎不可用。我的理解是,我可以从同一个keras模型创建一个新的估计器,第一次我预测它将从我指定的目录加载检查点

下面的代码片段是我的尝试(最终estimator_model2将加载到一个单独的例程中,这只是为了演示)

从诊断中,我可以看到它在执行最后一行时尝试加载检查点。但是,我得到一个错误,表明在训练期间保存的检查点不包含新估计器所需的所有信息。这就是错误:

E       NotFoundError (see above for traceback): Key conv2d_2/bias not found in checkpoint
E            [[Node: save/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_INT64, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/RestoreV2/tensor_names, save/RestoreV2/shape_and_slices)]]
如果有帮助,我可以展示keras模型,但我认为这不是问题所在


有谁能给我一个解决方案或建议一个更好的方法,用以前训练过的值加载估计器,只是为了进行预测?

我对上述问题的解决方案是使用混合方法,我用keras符号指定模型,然后将其放入tensorflow模型函数中,然后将其加载到和估计器中。通过采用这种方法,我可以像其他任何tensorflow模型一样保存到检查点,并从检查点重新加载。我认为这是使用直观的keras符号的最佳组合,同时能够利用tensorflow估计器和数据工具。以下是我描述各种tensorflow调用设置的方法概述:

  • 创建一个估计器:

    |--estimator: tf.estimator.Estimator
    |--config: tf.estimator.RunConfig                         #checkpointPath and saving spec for training
            |--model_fn: tf.estimator.EstimatorSpec
                |--myKerasModel                               #specify model. Doesn't have to be keras.
                        |--keras.models.Model
                |--loss: myLossFunction                       #train_and_eval only
                |--optimizer: myOptimizerFunction             #train_and_eval only
                |--training_hooks:tf.train.SummarySaverHook   #train_and_eval only - for saved diagnostics
                |--evaluation_hooks:tf.train.SummarySaverHook #train_and_eval only - for saved diagnostics
                |--predictions: model(data, training=False)   #predict only
    
  • 培训和评估:

    |--tf.estimator.train_and_evaluate
            |--estimator: tf.estimator.Estimator                  #the estimator we created
            |--train_spec: tf.estimator.TrainSpec
                                |--input_fn: tf.data.Dataset      #specify the input function 
                                    |--features, labels           #data to use
                                    |--batch, shuffle, num_epochs #controls for data
            |--eval_spec: tf.estimator.EvalSpec
                                |--input_fn: tf.data.Dataset      #specify the input function
                                    |--features, labels           #data to use
                                    |--batch                      #controls for data
                                |--throttle and start_delay       #specify when to start evaluation
    
  • 注意,我们可以使用
    tf.estimator.estimator.train
    tf.estimator.estimator.evaluate
    ,但是 不允许在培训期间进行评估,因此我们使用
    tf.estimator.train\u和\u evaluate

  • 预测:

    |--estimator: tf.estimator.Estimator.predict       #the estimator we created
            |--input_fn: tf.data.Dataset               #specify the input function
                |--features                            #data to use
    

  • 我还提出了一个warm_start的功能请求,这将是解决上述问题的一种方法
    |--estimator: tf.estimator.Estimator.predict       #the estimator we created
            |--input_fn: tf.data.Dataset               #specify the input function
                |--features                            #data to use