tensorflow中的Variable Scope

TensorFlow提供Variable Scope机制来控制变量的作用域,一定程度上类似于C++中的namespace,使得相同名称的变量可以同时存在。

变量作用域相关的函数:

tensorflow中的Variable Scope

变量生成相关的函数

tensorflow中的Variable Scope

使用示例

如下所示,conv_block中创建了weights和biases两个变量。

tensorflow中的Variable Scope

当使用多个conv_block时,可以使用variable_scope区分不同的作用域。

tensorflow中的Variable Scope

通过variable_scope创建conv1/weights, conv1/biases, conv2/weights, conv2/biases变量。

变量的复用机制

当需要复用变量时,调用函数reuse_variables()。

tensorflow中的Variable Scope

当tf.get_variable_scope().reuse == False,调用该函数会创建新的变量

tensorflow中的Variable Scope

当tf.get_variable_scope().reuse == True,调用该函数会重用已经创建的变量

tensorflow中的Variable Scope

其它

tf.variable_scope适用于tf.get_variable或者tf.Variable生成的变量;

tf.name_scope具有类似的功能,但只限于tf.Variable生成的变量。


分享到:


相關文章: