【问题标题】:What is the equivalent in Tensorflow 2.0 of tf.contrib.framework.nest.flatten_dict_items()?tf.contrib.framework.nest.flatten_dict_items() 在 Tensorflow 2.0 中的等价物是什么?
【发布时间】:2019-06-05 16:16:07
【问题描述】:

我正在使用 tf_upgrade_v2 将 TF1 代码升级到 TF2,我发现了这条消息:

tf.contrib.framework.nest.flatten_dict_items(dict)
AttributeError: module 'tensorflow' has no attribute 'contrib'

我应该如何更新代码?我没有找到解决方案。

【问题讨论】:

  • 试试tf.python.util.nest.flatten_dict_items

标签: python tensorflow tensorflow2.0


【解决方案1】:

这个有点奇怪(很难找到),因为它的导出方式与核心功能不同。

cs95 在他的评论中是正确的,因为它存在于tensorflow.python.util.nest,但不能简单地这样做:

import tensorflow as tf

tf.python.util.nest.flatten_dict_items(my_dict)

相反,我们需要导入 nest 模块本身,例如:

from tensorflow.python.util import nest

nest.flatten_dict_items(my_dict)

【讨论】:

  • 它有效,但我有一个问题。为什么我们不能像cs95说的那样做?我不知道为什么它不起作用。
  • 我不是关于 python 导入和导出如何工作的最佳权威。我碰巧知道(由经验证明)当我们只导入 base tf 模块时,只有标记为tf.export() 的方法才会被导入。 “为什么”(或更具体地说,发生这种情况的机制)恐怕不是我得到答案的原因。
猜你喜欢
  • 2017-01-14
  • 1970-01-01
  • 2014-06-29
  • 2019-03-24
  • 1970-01-01
  • 2013-07-23
  • 2017-06-05
  • 2019-11-11
  • 2022-06-22
相关资源
最近更新 更多