机器学习5:variable_scope()和name_scope()

机器学习5:variable_scope()和name_scope()

(1)variable_scope()简介:

         ①功能:

            与tf.get_variable()配合使用实现变量共享的功能;

        ②格式:

            1)with tf.variable_scope(name_or_scope,reuse=Name):;

            2)通过tf.get_variable_scope().reuse==True/False判断参变量是否共享;

            3)通过tf.get_variable_scope().reuse_variables()=True;设置reuse值。

        ③共享变量的实现:

             在variable_scope()中使用tf.get_variable()创建变量,并通过with tf.variable_scope(name_or_scope,reuse=True):共享变量;

             当reuse=true时,只能获取命名空间中已经存在的变量,如果不存在,则函数报错;

             当reuse=false时,创建新的变量,若同名变量已经存在,则函数报错。

 

(2)name_scope()简介:

         ①功能:

           管理变量的命名空间,多用于可视化计算图时;

        ②格式:

            with tf.name_scope(name):;

 

(3)variable_scope()和name_scope()的区别和特性:

         tf.variable()创建的变量受name_scope()控制,而get_variable()不受name_scope()控制;

         同一个name_scope()下的variable变量同名时自动设置别名,而get_variable()会报错;

            不同name_scope()下的variable变量同名时,由于完整名不一样,不代表同一变量。

        variable()的变量名是可选参数,而get_variable()的变量名是必填参数;

        name_scope()作用于variable()时和variable_scope()一样;

            name_scope()对get_variable()无效,只归属于variable_scope()来管理共享问题。

        variable_scope()和get_variable()(除全局scope外)必须搭配使用;

            variable()可单独使用也可以搭配name_scope()使用,给变量模块化分类命名;

            variable()与variable_scope()搭配无意义。

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值