警告:tensorflow:`write_grads` 将在 TensorFlow 2.0 中忽略`TensorBoard` 回调
Posted
技术标签:
【中文标题】警告:tensorflow:`write_grads` 将在 TensorFlow 2.0 中忽略`TensorBoard` 回调【英文标题】:WARNING:tensorflow:`write_grads` will be ignored in TensorFlow 2.0 for the `TensorBoard` Callback 【发布时间】:2020-12-02 21:09:21 【问题描述】:我正在使用以下代码行来可视化使用 tensorboard 的 ANN 模型的梯度
tensorboard_callback = tf.compat.v1.keras.callbacks.TensorBoard(log_dir='./Graph', histogram_freq=1, write_graph = True, write_grads =True, write_images = False)
tensorboard_callback .set_model(model)
%tensorboard --logdir ./Graph
我收到一条警告消息说“警告:tensorflow:write_grads
将在 TensorFlow 2.0 中被忽略 TensorBoard
回调。”
我得到了张量板输出,但没有渐变。
可能的原因是什么?
(注:我用的是2.3.0的tensorflow版本)
谢谢。
【问题讨论】:
【参考方案1】:Write_Grads
未在 TF2.x
中实现。这是仍然开放的备受期待的功能请求之一。请检查此 GitHub issue 作为功能请求。所以,我们只需要导入TF1.x
模块并使用write_grads
,如下代码所示。
# Load the TensorBoard notebook extension
%load_ext tensorboard
import tensorflow as tf
import datetime
# Clear any logs from previous runs
!rm -rf ./logs/
# Disable V2 behavior
tf.compat.v1.disable_v2_behavior()
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
def create_model():
return tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model = create_model()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.compat.v1.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1, write_grads =True)
model.fit(x=x_train, y=y_train, epochs=1, validation_data=(x_test, y_test), callbacks=[tensorboard_callback])
%tensorboard --logdir logs/fit
输出:
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 0s 0us/step
Train on 60000 samples, validate on 10000 samples
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training_v1.py:2048: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
32/60000 [..............................] - ETA: 0s - loss: 2.3311 - acc: 0.0312WARNING:tensorflow:Callbacks method `on_train_batch_end` is slow compared to the batch time (batch time: 0.0055s vs `on_train_batch_end` time: 0.0235s). Check your callbacks.
60000/60000 [==============================] - 17s 288us/sample - loss: 0.2187 - acc: 0.9349 - val_loss: 0.1012 - val_acc: 0.9690
<tensorflow.python.keras.callbacks.History at 0x7f7ebd1d3d30>
【讨论】:
使用tf.compat.v1.keras.callbacks.TensorBoard(write_grads=True)
后仍然显示相同的警告信息
@shaikmoeed,像这样导入张量流:python try: import tensorflow.compat.v1 as tf tf.disable_v2_behavior() print('v1 behaviour!') except ImportError: import tensorflow as tf print('Entered in the exception')
以上是关于警告:tensorflow:`write_grads` 将在 TensorFlow 2.0 中忽略`TensorBoard` 回调的主要内容,如果未能解决你的问题,请参考以下文章
TensorFlow忽略警告信息:FutureWarning
使用 @tffunction 的 Tensorflow2 警告