tensorflow - 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.
警告的原因是什么?应如何更改代码以避免此类警告?
解决方案
警告的原因是什么?应如何更改代码以避免此类警告?
此警告仅适用于 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/024675edc62dddab17bc439b69eb9bd71f1a1a9a // /layers/core.py#L1014
推荐阅读
- python - 根据 Pandas 或 Numpy 中的列值复制行
- r - 有没有办法将 R 链接到 Excel 的多个实例?
- victory-charts - 如何在 Victory (Native) Chart touch 上获得插值 X 值?
- php - Xdebug 尝试连接到错误的地址(Xdebug、PHP、Docker、VSCode、WSL2)
- multithreading - 来自其他线程的休眠执行查询卡住
- php - DataTables 隐藏数据源
- javascript - 如何防止发送 http 请求,直到 perious 完成?
- r - 如何对 PredictionClust/Prediction/R6 对象进行子集化
- tensorboard - 已删除日志文件夹,但 TensorBoard 仍显示所有已删除的运行
- r - ROC曲线的R中的预测函数