先给出笔者的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