【tensorflow】module ‘tensorflow.python.keras.utils.generic_utils‘ has no attribute populate_dict_with

遇到这个问题

参考:https://stackoverflow.com/posts/61347413/edit
module ‘tensorflow.python.keras.utils.generic_utils’ has no attribute ‘populate_dict_with_module_objects’

  • 解决办法1:pip install tensorflow==2.1.0
  • 解决办法2:copy “populate_dict_with_module_objects” function from this link (line 1162 to 1167) and add it to “generic_utils.py”
    【注】由于这是github上源码,指定的函数可能会发生改变,因为作者可能有更新,所以行数是参考,重点是找到对应的函数名;

链接里面的代码复制到generic_utils.py文件里。
这是因为原本的generic_utils.py没有这个函数,复制下面代码添加到本地的generic_utils.py模块里; 或者将连接里的整个文件下载到本地替换本地的文件!

  • 在这里插入图片描述
def populate_dict_with_module_objects(target_dict, modules, obj_filter):
  for module in modules:
    for name in dir(module):
      obj = getattr(module, name)
      if obj_filter(obj):
        target_dict[name] = obj

在你本地上是能找到这个generic_utils.py的在这里插入图片描述

对应你使用的tensorflow下的keras对应的位置;

方法2总结:由报错提示说明generic_utils.py文件内没有populate_dict_with_module_objects模块;所以这个解决办法是更新generic_utils.py这个文件(文件在本地)
更新方法1:就是在github源代码里面找到这个函数复制这个函数添加到本地的generic_utils.py模块里
更新方法2:直接全选复制这个链接里面的代码,替换掉本地的generic_utils.py里面的代码并保存

  • 解决办法3:TensorFlow卸载重装

注解:我使用解决方法一成功解决!

posted @ 2022-05-02 19:41  jucw  阅读(845)  评论(0编辑  收藏  举报