python - Keras 和 TensorBoard - AttributeError: 'Sequential' 对象没有属性 '_get_distribution_strategy'
问题描述
我正在使用 keras 并尝试使用 tensorboard 绘制日志。您可以在下面找到我遇到的错误以及我正在使用的软件包版本列表。我无法理解它给了我“顺序”对象没有属性“_get_distribution_strategy”的错误。
包:Keras 2.3.1 Keras-Applications 1.0.8 Keras-Preprocessing 1.1.0 tensorboard 2.1.0 tensorflow 2.1.0 tensorflow-estimator 2.1.0
模型:
model = Sequential()
model.add(Embedding(MAX_NB_WORDS, EMBEDDING_DIM, input_shape=(X.shape[1],)))
model.add(GlobalAveragePooling1D())
#model.add(Dense(10, activation='sigmoid'))
model.add(Dense(len(CATEGORIES), activation='softmax'))
model.summary()
#opt = 'adam' # Here we can choose a certain optimizer for our model
opt = 'rmsprop'
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) # Here we choose the loss function, input our optimizer choice, and set our metrics.
# Create a TensorBoard instance with the path to the logs directory
tensorboard = TensorBoard(log_dir='logs/{}'.format(time()),
histogram_freq = 1,
embeddings_freq = 1,
embeddings_data = X)
history = model.fit(X, Y, epochs=epochs, batch_size=batch_size, validation_split=0.1, callbacks=[tensorboard])
错误:
C:\Users\Bruno\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\callbacks\tensorboard_v2.py:102: UserWarning: The TensorBoard callback does not support embeddings display when using TensorFlow 2.0. Embeddings-related arguments are ignored.
warnings.warn('The TensorBoard callback does not support '
C:\Users\Bruno\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\indexed_slices.py:433: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
"Converting sparse IndexedSlices to a dense Tensor of unknown shape. "
Train on 1123 samples, validate on 125 samples
Traceback (most recent call last):
File ".\NN_Training.py", line 128, in <module>
history = model.fit(X, Y, epochs=epochs, batch_size=batch_size, validation_split=0.1, callbacks=[tensorboard]) # Feed in the train
set for X and y and run the model!!!
File "C:\Users\Bruno\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training.py", line 1239, in fit
validation_freq=validation_freq)
File "C:\Users\Bruno\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training_arrays.py", line 119, in fit_loop
callbacks.set_model(callback_model)
File "C:\Users\Bruno\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\callbacks\callbacks.py", line 68, in set_model
callback.set_model(model)
File "C:\Users\Bruno\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\callbacks\tensorboard_v2.py", line 116, in set_model
super(TensorBoard, self).set_model(model)
File "C:\Users\Bruno\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\callbacks.py", line 1532, in
set_model
self.log_dir, self.model._get_distribution_strategy()) # pylint: disable=protected-access
AttributeError: 'Sequential' object has no attribute '_get_distribution_strategy'```
解决方案
keras
您在and之间混合导入tf.keras
,它们不是同一个库,不支持这样做。
您应该从其中一个库中进行所有导入,无论是keras
还是tf.keras
.
推荐阅读
- jenkins - Jenkins 无法读取 config.xml:hudson.security.GlobalMatrixAuthorizationStrategy hudson.security.LDAPSecurityRealm
- angular - How to write scanned qr-code data to Firebase Realtime Database in Ionic?
- sql - 使用带有 SQL 变量的 WHERE IN 子句搜索 SQL 中的值列表,包括范围?
- c# - C# ClientSize wrong calculated
- javascript - 如果条件从未满足(即使它们满足)
- meson-build - Prefix all sources with src/ in Meson
- c# - 从 ArrayList 获取变量是否包含不同类型的列表的好方法?
- c# - 如何在 Chrome 或 Firefox 中为具有正确模式的 ac# 应用程序创建类似控制台的脚本引擎
- python - How to get string following some specific letters?
- computer-vision - How to implement real-time face recognition and blurring to a specific person?