Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.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/2/tensorflow/5.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 TFLearn:依次加载2个不同保存的模型时出错_Python_Neural Network_Tflearn - Fatal编程技术网

Python TFLearn:依次加载2个不同保存的模型时出错

Python TFLearn:依次加载2个不同保存的模型时出错,python,neural-network,tflearn,Python,Neural Network,Tflearn,我有两个不同的神经网络模型,使用TFLearn进行训练和保存。当我运行每个脚本时,保存的模型被正确加载。我需要一个系统,其中,第二个模型应该在第一个模型的输出之后调用。 但是,当我在加载第一个模型后尝试加载第二个模型时,会出现以下错误: NotFoundError(回溯见上文):在检查点中找不到键val_loss_2 [[Node:save_6/RestoreV2_42=RestoreV2[dtypes=[DT_FLOAT],[u device=“/job:localhost/replica:0

我有两个不同的神经网络模型,使用TFLearn进行训练和保存。当我运行每个脚本时,保存的模型被正确加载。我需要一个系统,其中,第二个模型应该在第一个模型的输出之后调用。 但是,当我在加载第一个模型后尝试加载第二个模型时,会出现以下错误:

NotFoundError(回溯见上文):在检查点中找不到键val_loss_2 [[Node:save_6/RestoreV2_42=RestoreV2[dtypes=[DT_FLOAT],[u device=“/job:localhost/replica:0/task:0/cpu:0”]([u arg_save_6/Const_0_0,save_6/RestoreV2_42/tensor_name,save_6/RestoreV2_42/shape_和_切片)]]

如果我注释掉第一个模型的加载,或者如果我单独运行这两个脚本,那么第二个模型将正确加载。知道为什么会发生这种错误吗

代码结构类似于

from second_model_file import check_second_model

def run_first_model(input):
    features = convert_to_features(input)
    model = tflearn.DNN(get_model())
    model.load("model1_path/model1")   # relative path
    pred = model.predict(features)
    ...
    if pred == certain_value:
       check_second_model()
second\u model\u file.py
类似:

def check_second_model():
    input_var = get_input_var()
    model2 = tflearn.DNN(regression_model())
    model2.load("model2_path/model2")   # relative path  
    pred = model2.predict(input_var)
    #other stuff  ......     

模型保存在不同的文件夹中,因此每个模型都有自己的
检查点
文件

好吧,我找到了解决方案。这是隐藏在讨论这个问题。 在构建第二个网络和模型之前,我使用了
tf.reset\u default\u graph()。希望这对其他人也有帮助

新代码:

import tensorflow as tf

def check_second_model():
    input_var = get_input_var()
    tf.reset_default_graph()
    model2 = tflearn.DNN(regression_model())
    model2.load("model2_path/model2")   # relative path  
    pred = model2.predict(input_var)

虽然我直观地理解了这个解决方案的工作原理,但如果有人能更好地解释为什么它设计成这样,我会很高兴。

好吧,我找到了解决方案。这是隐藏在讨论这个问题。 在构建第二个网络和模型之前,我使用了
tf.reset\u default\u graph()。希望这对其他人也有帮助

新代码:

import tensorflow as tf

def check_second_model():
    input_var = get_input_var()
    tf.reset_default_graph()
    model2 = tflearn.DNN(regression_model())
    model2.load("model2_path/model2")   # relative path  
    pred = model2.predict(input_var)

虽然我直观地理解了这个解决方案的工作原理,但如果有人能更好地解释为什么它是这样设计的,我会很高兴。

什么是
回归模型()
?什么是
回归模型()