【问题标题】:Making one input in a tensorflow model constant使张量流模型中的一个输入保持不变
【发布时间】:2020-06-22 23:29:06
【问题描述】:

我有以下问题: 我使用 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(这也无法更改)

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

提前感谢您的帮助!

最好的问候 托比亚斯

编辑: 我最终通过包装我试图使用的类并覆盖一些方法来解决这个问题。我认为另一个想法可能是用 keras 常量替换一个 Keras 输入。

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    这个错误有点棘手。以下是我想到的一些建议:

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

    添加更多关于您尝试使用的 DeepChem 模型以及您在其中看到错误的下游函数的信息也可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 2018-10-31
      • 2018-12-12
      • 1970-01-01
      相关资源
      最近更新 更多