TF基础2

1.常用API

1.图,操作和张量
tf.Graph,tf.Operation,tf.Tensor

2.可视化
TensorBoard

3.变量作用域
在TF中有两个作用域(scope),一个是name_scope,另一个是variable_scope.他们有什么区别呢?
variable_scope主要是给variable_name加前缀,也可以给op_name加前缀;name_scope是给op_name加前缀。

variable_scope变量作用域机制在TF中主要有两部分组成:

v=tf.get_variable(name,shape,dtype,initializer)#通过所给的名字创建或是返回一个变量
tf.variable_scope(<scope_name>)#为变量指定命名空间

当tf.get_variable_scope().reuse==False时,variable_scope作用域只能用来创建新变量:

with tf.variable_scope("foo"):
v=tf.get_variable("v",[1])
v2=tf.get_variable("v",[1])
assert v.name=="foo/v:0"

当tf.get_variable_scope().reuse==True时,variable_scope作用域可以共享变量:

with tf.variable_scope("foo") as scope:
     v=tf.get_variable("v",[1])

with tf.variable_scope("foo",reuse=True):
     v1=tf.get_variable("v",[1])
assert v1==v

获取变量作用域:可以直接通过tf.variable_scope()来获取变量作用域:

with tf.variable_scope("foo") as scope:
     v=tf.get_variable("v",[1])
with tf.variable_scope(foo_scope):
     w=tf.get_variable("w",[1])

如果在开启一个变量作用域使用之前预先定义一个作用域,则会跳过当前变量的作用域,保持预先存在的作用域不变。
变量作用域初始化,可以默认携带一个初始化器。

原文地址:https://www.cnblogs.com/Ann21/p/10479371.html