使张量流模型中的一个输入恒定

时间:2020-03-10 10:02:53

标签: python tensorflow keras

我有以下问题: 我使用Deepchem创建了一个模型,该模型是包裹的keras模型,对其进行了训练并重新加载。我可以预测使用该模型不会出现问题。 现在,我想制作一个该模型的副本,该模型的输入要少一些,因为在我的使用场景中,一个输入始终是恒定的,并且始终将其传递会导致我无法编辑的函数出错。

data = np.array(data.data, dtype=float32)
    with tf.Graph().as_default() as temp_graph:
        tf.import_graph_def(self.model.session.graph.as_graph_def(),
                            input_map={self.model._input_placeholders[1].name: 
                                       tf.constant(np.array([0], dtype=float32)),})
    #self.model.session.graph = temp_graph
    #for deep explainer: replace all switched dropouts with dropouts 
    #get input tensor for this graph 
    tensors = tf.contrib.graph_editor.get_tensors(temp_graph)
    for t in tensors:
        if "input_1" in t.name:
            input_tensor = t
            break
    #reshape output --> only singletask!
    output = tf.reshape(tensors[-1], [-1, 1])
    model = (input_tensor, output)
    sess = tf.Session(graph=temp_graph)
    feed_dict = dict(zip([input_tensor], [data]))
    print(sess.run(output, feed_dict))

在这段代码中,我能够加载模型图并将常量传递到其输入中。现在显然我不能在同一会话中运行此新模型,因为该会话包含旧模型。使用feed dict来运行模型的方式无法更改,因为它是真实情况下的另一个程序包。我收到以下错误消息:

Error while reading resource variable dense_2/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist.

完整跟踪为:

Traceback (most recent call last):
  File "/EXT/Tobha/.conda/envs/test_BA_Tobias_std_deepchem-2-3-0_py36_20200114/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1356, in _do_call
    return fn(*args)
  File "/EXT/Tobha/.conda/envs/test_BA_Tobias_std_deepchem-2-3-0_py36_20200114/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1341, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "/EXT/Tobha/.conda/envs/test_BA_Tobias_std_deepchem-2-3-0_py36_20200114/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1429, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable dense_2/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/dense_2/bias)
     [[{{node import/model/dense_2/BiasAdd/ReadVariableOp}}]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/EXT/Tobha/eclipse-workspace/Bachelorarbeit/toolbox_dc_2_3_0/python_source/Models.py", line 490, in <module>
    main()
  File "/EXT/Tobha/eclipse-workspace/Bachelorarbeit/toolbox_dc_2_3_0/python_source/Models.py", line 478, in main
    evaluate()
  File "/EXT/Tobha/eclipse-workspace/Bachelorarbeit/toolbox_dc_2_3_0/python_source/Models.py", line 445, in evaluate
    reader.explain()
  File "/EXT/Tobha/eclipse-workspace/Bachelorarbeit/toolbox_dc_2_3_0/python_source/DataHandling.py", line 1534, in explain
    self.explain()
  File "/EXT/Tobha/eclipse-workspace/Bachelorarbeit/toolbox_dc_2_3_0/python_source/DataHandling.py", line 1519, in explain
    self._explain_Gradient_SHAP(self.df)
  File "/EXT/Tobha/eclipse-workspace/Bachelorarbeit/toolbox_dc_2_3_0/python_source/DataHandling.py", line 2047, in _explain_Gradient_SHAP
    print(sess.run(output, feed_dict))
  File "/EXT/Tobha/.conda/envs/test_BA_Tobias_std_deepchem-2-3-0_py36_20200114/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 950, in run
    run_metadata_ptr)
  File "/EXT/Tobha/.conda/envs/test_BA_Tobias_std_deepchem-2-3-0_py36_20200114/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1173, in _run
    feed_dict_tensor, options, run_metadata)
  File "/EXT/Tobha/.conda/envs/test_BA_Tobias_std_deepchem-2-3-0_py36_20200114/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1350, in _do_run
    run_metadata)
  File "/EXT/Tobha/.conda/envs/test_BA_Tobias_std_deepchem-2-3-0_py36_20200114/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1370, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable dense_2/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/dense_2/bias)
     [[node import/model/dense_2/BiasAdd/ReadVariableOp (defined at /eclipse-workspace/Bachelorarbeit/toolbox_dc_2_3_0/python_source/DataHandling.py:2033) ]]

Original stack trace for 'import/model/dense_2/BiasAdd/ReadVariableOp':
  File "/eclipse-workspace/Bachelorarbeit/toolbox_dc_2_3_0/python_source/Models.py", line 490, in <module>
    main()
  File "/eclipse-workspace/Bachelorarbeit/toolbox_dc_2_3_0/python_source/Models.py", line 478, in main
    evaluate()
  File "/eclipse-workspace/Bachelorarbeit/toolbox_dc_2_3_0/python_source/Models.py", line 445, in evaluate
    reader.explain()
  File "/eclipse-workspace/Bachelorarbeit/toolbox_dc_2_3_0/python_source/DataHandling.py", line 1534, in explain
    self.explain()
  File "/eclipse-workspace/Bachelorarbeit/toolbox_dc_2_3_0/python_source/DataHandling.py", line 1519, in explain
    self._explain_Gradient_SHAP(self.df)
  File "/eclipse-workspace/Bachelorarbeit/toolbox_dc_2_3_0/python_source/DataHandling.py", line 2033, in _explain_Gradient_SHAP
    tf.constant(np.array([0], dtype=float32)),})
  File "/.conda/envs/test_BA_Tobias_std_deepchem-2-3-0_py36_20200114/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "/.conda/envs/test_BA_Tobias_std_deepchem-2-3-0_py36_20200114/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 443, in import_graph_def
    _ProcessNewOps(graph)
  File "/.conda/envs/test_BA_Tobias_std_deepchem-2-3-0_py36_20200114/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 236, in _ProcessNewOps
    for new_op in graph._add_new_tf_operations(compute_devices=False):  # pylint: disable=protected-access
  File "/.conda/envs/test_BA_Tobias_std_deepchem-2-3-0_py36_20200114/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3751, in _add_new_tf_operations
    for c_op in c_api_util.new_tf_operations(self)
  File "/.conda/envs/test_BA_Tobias_std_deepchem-2-3-0_py36_20200114/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3751, in <listcomp>
    for c_op in c_api_util.new_tf_operations(self)
  File "/.conda/envs/test_BA_Tobias_std_deepchem-2-3-0_py36_20200114/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3641, in _create_op_from_tf_operation
    ret = Operation(c_op, self)
  File "/.conda/envs/test_BA_Tobias_std_deepchem-2-3-0_py36_20200114/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2005, in __init__
    self._traceback = tf_stack.extract_stack()

我正在使用tensorflow 1.14和Python 3.6(也无法更改)

所以我的问题可以通过两种不同的方式解决:要么我可以使用旧会话中的信息来运行第二张图,要么可以告诉旧会话使用一个常量输入。

谢谢您的帮助!

致以最诚挚的问候 托比亚斯

编辑: 最终,我通过包装要使用的类并覆盖了一些方法来解决此问题。我认为可能还有另一种想法,那就是用一个keras常量代替一个Keras输入。

1 个答案:

答案 0 :(得分:2)

此错误有些棘手。这里有一些想到的建议:

  • DeepChem HEAD现在在TensorFlow 2.X上运行。如果您的问题在“急切”模式下更容易解决,则可能是一种选择。当然,HEAD不稳定,并且可能还会出现其他问题。
  • DeepChem模型位于仅由Keras层制成的引擎盖下方。如果可以从模型的构成层中创建Keras模型,则可以避免使用DeepChem包装器,并直接在Keras中解决问题。

这可能还有助于在尝试使用的DeepChem模型和下游函数中添加更多信息。