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:

  • 参考tensorflow的 pull #34870 : "Use _get_distribution_strategy only when it is available. " https://github.com/tensorflow/tensorflow/pull/34870

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文件,如下图。

相关代码如下。

# 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中引用。

import keras

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

# xxxxx
  • ref :https://stackoverflow.com/questions/59765784/attributeerror-sequential-object-has-no-attribute-get-distribution-strategy

你可能感兴趣的:(keras调用tensorborad时报错 AttributeError : Model object has no attribute _get_distribution_strategy)