keras调用tensorborad时报错 AttributeError 'Model' object has no attribute '_get_distribution_strategy'

keras调用tensorborad时报错:
AttributeError: 'Model' object has no attribute '_get_distribution_strategy'

软件版本:

  • python: 3.7.4
  • keras: 2.3.1
  • tensorboard: 2.1.0
  • tensorflow: 2.1.0

解决办法A:

The changes introduced in 06d8f77 are not compatible with standalone Keras (they are compatible with tf.Keras). a keras.Model does not have a _get_distribution_strategy method, which is now assumed for the Tensorboard callback.

  • 直接修改tensorflow/python/keras/callbacks.py文件,如下图。

相关代码如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 1529行左右 : # distributed_file_utils.write_dirpath()

# 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

# 1732行左右也要改一下: # distributed_file_utils.remove_temp_dirpath()

# 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

解决办法B:

修改引用,改为从tensorflow.keras中引用。

1
2
3
4
5
6
7
import keras

from tensorflow.keras.callbacks import TensorBoard
from tensorflow.keras.layers import *
from tensorflow.keras.models import Sequential

# xxxxx