机器学习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()搭配无意义。
本文详细解析了TensorFlow中variable_scope()和name_scope()的功能与用法,包括变量共享、命名空间管理及二者之间的区别与特性。通过实例说明如何在深度学习项目中有效利用这些工具。
1万+

被折叠的 条评论
为什么被折叠?



