Understand on the tf.variable_scope and tf.name_scope

本文探讨了TensorFlow中tf.variable_scope与tf.name_scope的区别。作者通过实际经验分享了如何使用这两种方法来组织代码并提高TensorBoard可视化效果。文中详细解释了变量名的作用范围以及在不同场景下如何避免命名冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Recent two weeks, I have spent much time on learning Tensorflow to implement my idea. Here, I would like to give some diferences between tf.variable_scope and tf.name_scope. Totally, these two method can summarize some basics operations into one node and show a clear graph visualized by TensorBoard. Next, I would like to highlight their differences.

First, we usually use the manner like w= tf.Variable(dtype=tf.float32, shape=[784, 10])) to define the variable w. Actually, the tensorflow would give a specific name for this variable in their graph, sometimes maybe like variable_1, variable_2, and so on like these. Occasionally, we would also like to define the w like this: w= tf.Variable(dtype=tf.float32, shape=[784, 10]), name=’w’). I think all these name methods are just for the compact visualization in the Tensorboard. In the same name_scope, variables with the same name would not allowed to be created andalso TF gives the errors.

Second, if the tf.variable_scope is used, the name of the variable would automatically be added a prefix with the variable scope name. In this regard, variables with the same names specified in the declaration would be fine, because a different variable scope name is added as their prefix.

Sometimes, we don’t not need to specify the variable name by the declaration, otherwise for reuse, which is another question and would be discussed when required in my future works.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值