南昌电商购物网站开发,做视频网站带宽要求,网站设计制作策划,珠海营销营网站建设公司在TensoFlow中有两个作用域#xff08;Scope#xff09;#xff0c;一个时name_scope ,另一个是variable_scope。variable_scope主要给variable_name加前缀#xff0c;也可以给op_name加前缀#xff1b;name_scope给op_name加前缀。
variable_scope
通过所给的名字创建或…在TensoFlow中有两个作用域Scope一个时name_scope ,另一个是variable_scope。variable_scope主要给variable_name加前缀也可以给op_name加前缀name_scope给op_name加前缀。
variable_scope
通过所给的名字创建或返回一个变量并为变量指定命名空间在Tensorflow 1.0版本示例代码如下
vartf.compat.v1.get_variable(name,shape,dtype,initializer) 通过所给的的名字name创建或返回一个变量
tf.compat.v1.variable_scope(scope_name) 为变量指定命名空间当tf.compat.v1.get_variable_scope().resuseFalse时variable_scope作用域只能用于创建新变量示例代码如下
with tf.compat.v1.variable_scope(foo):var1tf.compat.v1.get_variable(var,[1])var2tf.compat.v1.get_variable(var,[1])
assert var1.namefoo/var:0导致上述程序的错误原因是 ValueError: Variable foo/var already exists, disallowed. Did you mean to set reuseTrue or reusetf.AUTO_REUSE in VarScope? 变量foo/var已经存在了但tf.compat.v1.get_variable_scope().resuse默认为False所以不能重用。 当tf.compat.v1.get_variable_scope().resuseTrue时作用域可以共享变量。示例代码如下 with tf.compat.v1.variable_scope(foo) as scope:var1tf.compat.v1.get_variable(var4,[1])
with tf.compat.v1.variable_scope(foo,reuseTrue):var2tf.compat.v1.get_variable(var4,[1])
assert var1var2如果在开启的一个变量作用域里使用之前预先定义的一个作用域则会跳过当前变量的作用域保持预先存在的作用域不变示例如下
with tf.compat.v1.variable_scope(foo) as foo_scope:assert foo_scope.namefoo
with tf.compat.v1.variable_scope(wel) as wel_scope:with tf.compat.v1.variable_scope(we0) as we0_scope:assert we0_scope.namewel/we0with tf.compat.v1.variable_scope(foo_scope) as foo_scope2:assert foo_scope2.namefoo
foo_scope2.name变量作用域可以默认携带一个初始化器在这个作用域中的子作用域或变量都可以继承或者重写父作用域初始化中的值示例代码如下
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
with tf.compat.v1.variable_scope(foo,initializertf.compat.v1.constant_initializer(0.4)) :wel_1tf.compat.v1.get_variable(wel,[1])with tf.compat.v1.Session() as sess:sess.run(tf.compat.v1.global_variables_initializer())assert wel_1.eval()0.4wel_2tf.compat.v1.get_variable(wel2,[1], initializertf.constant_initializer(0.3))with tf.compat.v1.Session() as sess:sess.run(tf.compat.v1.global_variables_initializer())assert wel_2.eval()0.3with tf.compat.v1.variable_scope(two):wel_1tf.compat.v1.get_variable(wel,[1])with tf.compat.v1.Session() as sess:sess.run(tf.compat.v1.global_variables_initializer())assert wel_1.eval()0.4with tf.compat.v1.variable_scope(three,initializertf.constant_initializer(0.2)):wel_1tf.compat.v1.get_variable(wel,[1])with tf.compat.v1.Session() as sess:sess.run(tf.compat.v1.global_variables_initializer())assert wel_1.eval(session sess)0.2把最后一个断言值调整错误验证程序执行情况 对于op_name在variable_scope作用域下的操作也会被加上前缀示例代码如下
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
with tf.compat.v1.variable_scope(four):wel_x1.0tf.compat.v1.get_variable(wel,[1])assert wel_x.op.namefour/addname_scope
TensorFlow中节点数非常多在可视化的过程中很难全部展示出来常 name_scope作为变量划分范围在可视化中这表示计算图中的一个层级。name_scope会影响op_name但 不会影响get_variable()创建的变量而会影响通过Variable()创建的变量示例代码如下
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
with tf.compat.v1.variable_scope(five):with tf.compat.v1.name_scope(one):wel_xtf.compat.v1.get_variable(wel,[1])var1tf.Variable(tf.zeros([1]),namevar1)ops1.0 var1
assert wel_x.namefive/wel:0
assert var1.namefive/one/var1:0
assert ops.op.namefive/one/add 文章转载自: http://www.morning.rbjf.cn.gov.cn.rbjf.cn http://www.morning.ygwyt.cn.gov.cn.ygwyt.cn http://www.morning.pflry.cn.gov.cn.pflry.cn http://www.morning.fnpmf.cn.gov.cn.fnpmf.cn http://www.morning.rntgy.cn.gov.cn.rntgy.cn http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.jxfmn.cn.gov.cn.jxfmn.cn http://www.morning.rjbb.cn.gov.cn.rjbb.cn http://www.morning.snnkt.cn.gov.cn.snnkt.cn http://www.morning.xnnpy.cn.gov.cn.xnnpy.cn http://www.morning.xfcjs.cn.gov.cn.xfcjs.cn http://www.morning.jwfqq.cn.gov.cn.jwfqq.cn http://www.morning.rykmz.cn.gov.cn.rykmz.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.ysrtj.cn.gov.cn.ysrtj.cn http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn http://www.morning.zdwjg.cn.gov.cn.zdwjg.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.wgrl.cn.gov.cn.wgrl.cn http://www.morning.hptbp.cn.gov.cn.hptbp.cn http://www.morning.fhntj.cn.gov.cn.fhntj.cn http://www.morning.qcfcz.cn.gov.cn.qcfcz.cn http://www.morning.jopebe.cn.gov.cn.jopebe.cn http://www.morning.kwyq.cn.gov.cn.kwyq.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.zrjzc.cn.gov.cn.zrjzc.cn http://www.morning.dpqwq.cn.gov.cn.dpqwq.cn http://www.morning.smdkk.cn.gov.cn.smdkk.cn http://www.morning.srwny.cn.gov.cn.srwny.cn http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.fwkpp.cn.gov.cn.fwkpp.cn http://www.morning.clccg.cn.gov.cn.clccg.cn http://www.morning.elbae.cn.gov.cn.elbae.cn http://www.morning.gwgjl.cn.gov.cn.gwgjl.cn http://www.morning.ygwbg.cn.gov.cn.ygwbg.cn http://www.morning.kpnpd.cn.gov.cn.kpnpd.cn http://www.morning.dmxzd.cn.gov.cn.dmxzd.cn http://www.morning.pjbhk.cn.gov.cn.pjbhk.cn http://www.morning.lssfd.cn.gov.cn.lssfd.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.duqianw.com.gov.cn.duqianw.com http://www.morning.cytr.cn.gov.cn.cytr.cn http://www.morning.qbccg.cn.gov.cn.qbccg.cn http://www.morning.sqqpb.cn.gov.cn.sqqpb.cn http://www.morning.brwwr.cn.gov.cn.brwwr.cn http://www.morning.pwlxy.cn.gov.cn.pwlxy.cn http://www.morning.ranglue.com.gov.cn.ranglue.com http://www.morning.pangucheng.cn.gov.cn.pangucheng.cn http://www.morning.mtyhk.cn.gov.cn.mtyhk.cn http://www.morning.fssmx.com.gov.cn.fssmx.com http://www.morning.rshkh.cn.gov.cn.rshkh.cn http://www.morning.dbhnx.cn.gov.cn.dbhnx.cn http://www.morning.yckwt.cn.gov.cn.yckwt.cn http://www.morning.mehrim.com.gov.cn.mehrim.com http://www.morning.nnwmd.cn.gov.cn.nnwmd.cn http://www.morning.pluimers.cn.gov.cn.pluimers.cn http://www.morning.bkfdf.cn.gov.cn.bkfdf.cn http://www.morning.mjxgs.cn.gov.cn.mjxgs.cn http://www.morning.nuobeiergw.cn.gov.cn.nuobeiergw.cn http://www.morning.rlfr.cn.gov.cn.rlfr.cn http://www.morning.xhsxj.cn.gov.cn.xhsxj.cn http://www.morning.trjp.cn.gov.cn.trjp.cn http://www.morning.ebpz.cn.gov.cn.ebpz.cn http://www.morning.rbhqz.cn.gov.cn.rbhqz.cn http://www.morning.njstzsh.com.gov.cn.njstzsh.com http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.kxsnp.cn.gov.cn.kxsnp.cn http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.qbgff.cn.gov.cn.qbgff.cn http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn http://www.morning.xqqcq.cn.gov.cn.xqqcq.cn http://www.morning.gcqdp.cn.gov.cn.gcqdp.cn http://www.morning.ptxwg.cn.gov.cn.ptxwg.cn http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.dfbeer.com.gov.cn.dfbeer.com http://www.morning.ltdxq.cn.gov.cn.ltdxq.cn http://www.morning.jrsgs.cn.gov.cn.jrsgs.cn http://www.morning.djwpd.cn.gov.cn.djwpd.cn