Tensorflow Keras官方教程中的折旧警告

Tensorflow Keras官方教程中的折旧警告,tensorflow,keras,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: m

示例代码

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'))
在官方结果中,输出中会出现警告,如该页面本身所示

calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
警告的原因是什么?如何更改代码以避免此类警告?

欢迎加入俱乐部:)我的意思是tensorflow因不向后兼容而臭名昭著,更糟糕的是,现在很长一段时间以来都有类似的警告

请看这里:

所以你最好的办法就是忽略这些警告,如果你想主动,那么就提供一个修复方案。否则,确保他们不会分散你的主要任务。警告现在在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\u uniform
。大概是这样的:

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