我一直在阅读他们写过的tensorflow教程
with tf.name_scope('read_inputs') as scope:
# something
Run Code Online (Sandbox Code Playgroud)
我怀疑为什么我们使用name_scope?
a = tf.constant(5)
Run Code Online (Sandbox Code Playgroud)
1)和2)都是一样的.那么什么时候使用name_scope创造差异?
eta*_*ion 19
它们不是同一件事.
import tensorflow as tf
c1 = tf.constant(42)
with tf.name_scope('s1'):
c2 = tf.constant(42)
print(c1.name)
print(c2.name)
Run Code Online (Sandbox Code Playgroud)
版画
Const:0
s1/Const:0
Run Code Online (Sandbox Code Playgroud)
顾名思义,范围函数为您在其中创建的操作的名称创建范围.这会影响你如何引用张量,重用,图表在TensorBoard中的显示方式等等.
我没有看到重用常量的用例,但是这里是有关范围和变量共享的一些相关信息。
范围
name_scope 将范围添加为所有操作的前缀
variable_scope 将范围作为所有变量和操作的前缀添加
实例化变量
tf.Variable()构造函数为变量名加上当前name_scope和variable_scope
tf.get_variable()构造函数忽略name_scope并且仅在当前名称前添加名称variable_scope
例如:
with tf.variable_scope("variable_scope"):
with tf.name_scope("name_scope"):
var1 = tf.get_variable("var1", [1])
with tf.variable_scope("variable_scope"):
with tf.name_scope("name_scope"):
var2 = tf.Variable([1], name="var2")
Run Code Online (Sandbox Code Playgroud)
产生
var1 = <tf.Variable 'variable_scope/var1:0' shape=(1,) dtype=float32_ref>
var2 = <tf.Variable 'variable_scope/name_scope/var2:0' shape=(1,) dtype=string_ref>
Run Code Online (Sandbox Code Playgroud)
重用变量
始终用于tf.variable_scope定义共享变量的范围
执行重用变量的最简单方法是使用reuse_variables()如下所示的
with tf.variable_scope("scope"):
var1 = tf.get_variable("variable1",[1])
tf.get_variable_scope().reuse_variables()
var2=tf.get_variable("variable1",[1])
assert var1 == var2
Run Code Online (Sandbox Code Playgroud)
tf.Variable()总是创建一个新的变量,当变量与已经使用的名称,它只是追加建造_1,_2它等-这可能会导致冲突:(| 归档时间: |
|
| 查看次数: |
10057 次 |
| 最近记录: |