'Model'object has no attribute '_get_distribution_strategy'的一种解决方案

先给出笔者的tensorflow和keras版本
在这里插入图片描述在这里插入图片描述
如题,笔者在用别人的代码的时候出现了’Model’object has no attribute '_get_distribution_strategy’的错误,百度半天也没有找到解决方案,只知道貌似是版本不同导致的。由于笔者是在anaconda下配置tensorflow和keras的,下文仅提供在此前提下的一种解决方案(不一定可行,尝试前请先备份!笔者的友人在windows上修改后还是失败,报了另一种错误):
先进入anaconda的安装目录,进入lib/site-packages/tensorflow_core/python/keras,编辑callbacks.py(先备份!),把1529行开始的

    # TensorBoard callback involves writing a summary file in a
    # possibly distributed settings.
    self._log_write_dir = distributed_file_utils.write_dirpath(
        self.log_dir, self.model._get_distribution_strategy())  # pylint: disable=protected-access

修改为

    # In case this callback is used via native Keras, _get_distribution_strategy does not exist.
    if hasattr(self.model, '_get_distribution_strategy'):
      # TensorBoard callback involves writing a summary file in a
      # possibly distributed settings.
      self._log_write_dir = distributed_file_utils.write_dirpath(
          self.log_dir, self.model._get_distribution_strategy())  # pylint: disable=protected-access
    else:
      self._log_write_dir = self.log_dir

再将1730行左右开始的

 # Safely remove the unneeded temp files.
    distributed_file_utils.remove_temp_dirpath(
        self.log_dir, self.model._get_distribution_strategy())  # pylint: disable=protected-access

修改为

 # In case this callback is used via native Keras, _get_distribution_strategy does not exist.
    if hasattr(self.model, '_get_distribution_strategy'):
      # Safely remove the unneeded temp files.
      distributed_file_utils.remove_temp_dirpath(
          self.log_dir, self.model._get_distribution_strategy())  # pylint: disable=protected-access

保存,再次运行就不再报这个错误了报另一种错,确信


本文讲得不清楚的可以参考https://github.com/tensorflow/tensorflow/pull/34870
或者直接看https://github.com/tensorflow/tensorflow/pull/34870/commits/0d31c0bee8a1e06c7b4fa977ce2bc6ce347aa96f

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值