首页 > 解决方案 > 为什么模型变量初始化方法在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 上重现我的试用版

标签: pythontensorflow

解决方案


推荐阅读