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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值