首页 > 解决方案 > Tensorflow Keras 官方教程中的折旧警告

问题描述

示例代码

model = tf.keras.Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(layers.Dense(64, activation='relu'))
# Add another:
model.add(layers.Dense(64, activation='relu'))
# Add a softmax layer with 10 output units:
model.add(layers.Dense(10, activation='softmax'))

Tensorflow 官方网站中会导致输出中出现警告,如该页面本身所示。

calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.

警告的原因是什么?应如何更改代码以避免此类警告?

标签: tensorflowkeras

解决方案


警告的原因是什么?应如何更改代码以避免此类警告?

此警告仅适用于 Tensorflow 2.0 之前的版本。出现警告是因为从 Tensorflow 2.0 开始,dtype在调用初始化程序实例时应该传递参数,而不是传递给构造函数。

在 pre-tensorflow2.0 中重现警告的代码片段:

from tensorflow.keras.layers import Dense
dense = Dense(64, activation='relu')

输出:

calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor

由于没有创建初始化程序,因此代码片段会导致此警告的原因尚不清楚。发生的情况是,在启动层时,Tensorflow 内部会创建初始化程序Dense,因为默认的内核初始化程序是glorot_uniform. 像这样的东西:

from tensorflow.keras import initializers
initializer = initializers.get('glorot_uniform') 

https://github.com/tensorflow/tensorflow/blob/024 ​​675edc62dddab17bc439b69eb9bd71f1a1a9a/tensorflow/python/keras/layers/core.py#L997 https://github.com/tensorflow/tensorflow/blob/024​​675edc62dddab17bc439b69eb9bd71f1a1a9a // /layers/core.py#L1014


推荐阅读