TensorFlow2.1.0报错解决:AttributeError: 'Sequential' object has no attribute '_get_distribution_strategy

本机环境:

  • cudatoolkit = 10.1.243
  • cudnn = 7.6.5
  • tensorflow-gpu = 2.1.0
  • keras-gpu = 2.3.1

今天在以TensorFlow2.1.0为后端的Keras中使用TensorBoard时报错,发现原因是keras和tf.keras混用导致的。报错与解决方案如下:

  1. 导致报错语句:
summary = TensorBoard(log_dir="cnn_lstm_logs/",histogram_freq=1)

报错:

---> 54     summary = TensorBoard(log_dir="cnn_lstm_logs/",histogram_freq=1)
     55     history=model.fit(trainX, trainy, 
     56                       epochs=epochs, batch_size=batch_size, verbose=verbose, validation_data=(testX, testy),

NameError: name 'TensorBoard' is not defined
  1. 更改程序:
summary = keras.callbacks.TensorBoard(log_dir="cnn_lstm_logs/",histogram_freq=1)

报错:

AttributeError: 'Sequential' object has no attribute '_get_distribution_strategy
  1. 进一步修改程序
summary = tf.keras.callbacks.TensorBoard(log_dir="cnn_lstm_logs/",histogram_freq=1)

仍然报错:

AttributeError: 'Sequential' object has no attribute '_get_distribution_strategy
  1. 根据Stack Overflow 描述,做如下修改:
#model = Sequential() # 顺序模型
model = tf.keras.Sequential()

报错:

C:\anaconda3\envs\keras\lib\site-packages\tensorflow_core\python\keras\engine\sequential.py in add(self, layer)
    159       raise TypeError('The added layer must be '
    160                       'an instance of class Layer. '
--> 161                       'Found: ' + str(layer))
    162 
    163     tf_utils.assert_no_legacy_layers([layer])

TypeError: The added layer must be an instance of class Layer. Found: <keras.layers.wrappers.TimeDistributed object at 0x0000016997098348>

  1. 继续修改
    将开头导入的包from keras.xx,全部改为from tensorflow.keras.xx,如:
#import keras
import tensorflow
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, TimeDistributed, Conv1D, MaxPooling1D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import LSTM
from tensorflow.keras.utils import to_categorical

仍然报错:

C:\anaconda3\envs\keras\lib\site-packages\tensorflow_core\python\eager\profiler.py in stop()
     97     if _profiler is None:
     98       raise ProfilerNotRunningError(
---> 99           'Cannot stop profiling. No profiler is running.')
    100     if context.default_execution_mode == context.EAGER_MODE:
    101       context.context().executor.wait()

ProfilerNotRunningError: Cannot stop profiling. No profiler is running.
  1. 参考这位博主的解决方案,做如下修改,成功运行:
    logdir = os.path.join('cnn_lstm_logs')
    summary = tensorflow.keras.callbacks.TensorBoard(log_dir=logdir,histogram_freq=1)
    history=model.fit(trainX, trainy, 
                      epochs=epochs, batch_size=batch_size, verbose=verbose, validation_data=(testX, testy),
                      callbacks=[summary])
  1. 运行结果:

在这里插入图片描述

参考:
https://stackoverflow.com/questions/59765784/attributeerror-sequential-object-has-no-attribute-get-distribution-strategy
https://stackoverflow.com/questions/59894720/keras-and-tensorboard-attributeerror-sequential-object-has-no-attribute-g

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

datamonday

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值