gpt4 book ai didi

python - name_or_scope = None 的variable_scope有什么用

转载 作者:太空宇宙 更新时间:2023-11-03 14:24:02 25 4
gpt4 key购买 nike

当我们使用variable_scope时,我们通常将第一个参数(name_or_scope)设置为variable_scope的名称。当我们将reuse变量设置为“True”时,我们可以在variable_scope内共享该变量。但是,我发现在tensorflow API(例如Bahdanau注意力或tf.layers.Dense)内部,有一些变量作用域,其第一个参数(name_or_scope)设置为None 和第二个参数 (default_name) 设置为我们认为的作用域名称。我检查了tf.variable_scope的代码,发现:

If name_or_scope is not None, it is used as is. If scope is None, then default_name is used. In that case, if the same name has been previously used in the same scope, it will be made unique by appending _N to it.

在这种情况下,我无法在 for 循环中使用它,因为每次调用 variable_scope 时,它​​都会创建具有不同名称的新 variable_scope

我找不到任何地方解释这个函数(我的意思是name_or_scope设置为None)。

有人解释一下吗?

最佳答案

你是对的,当使用 name_or_scope=None 调用 tf.variable_scope 时,会使用 default_name 参数,并且它是唯一的。因此在循环中调用它会创建不同的作用域。

不确定Bahdanau注意力,但可以通过_scope参数使用自定义范围创建tf.layers.Dense:

layer = Dense(units, ..., _scope=name)

事实上,您可以调用tf.layers.dense具有指定的名称,并且该名称将定义tf.variable_scope(name)。这种方式允许您在循环中创建密集层。

关于python - name_or_scope = None 的variable_scope有什么用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47746758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com