python - 为什么模型变量初始化方法在tensorflow中被调用了两次?
问题描述
我试图弄清楚 SGD 在 tensorflow 中的实现。
在apply_gradients
方法中
with ops.name_scope_v2(self._name):
# Create iteration if necessary.
with ops.init_scope():
self._create_all_weights(var_list)
就在self._create_all_weights(var_list)
我添加一些日志记录之前
with ops.name_scope_v2(self._name):
# Create iteration if necessary.
with ops.init_scope():
tf.print('apply_gradients::with ops.init_scope: ', var_list[0].numpy(), var_list[1].numpy())
self._create_all_weights(var_list)
然后我得到了两次初始化的权重和偏差,我不明白。为什么这个方法被调用了两次?
为了验证我的猜测,我禁用了所有日志记录,除了
def apply_gradients(self,
grads_and_vars,
name=None,
experimental_aggregate_gradients=True):
tf.print('apply_gradients is called')
就在这条线之前
grads_and_vars = optimizer_utils.filter_empty_gradients(grads_and_vars)
每次我运行我的脚本时,我只得到一个日志记录
apply_gradients is called
为什么在tensorflow中权重和偏差初始化方法被调用了两次?
单击此处查看整篇文章并在 colab 上重现我的试用版
解决方案
推荐阅读
- c - GNU readline:巨大的内存泄漏
- python - 无法从网络获取下载链接
- android - 依赖失败:io.grpc:grpc-auth:1.13.1 -> io.grpc:grpc-core@[1.13.1],但 grpc-core 版本是 1.16.1
- sql - 如何有效地从依赖表中选择最新记录
- java - 如何使用 JavaFX 11(模块化)创建独立应用程序?
- mysql - 如何在同一个查询中返回多个结果?
- jaxb - 从 2.2.7 开始,使用 JAXB-RI 实现的解组失败
- android - 包 R 不存在,布局为空
- python - 从python访问矩阵的运算符函数
- laravel-5 - 选择每页显示的项目数 Vue/Laravel 分页