当前位置:网站首页>(TensorFlow) - detailed explanation of tf.variable_scope and tf.name_scope
(TensorFlow) - detailed explanation of tf.variable_scope and tf.name_scope
2022-08-04 06:06:00 【Big Yellow Cat No. 1】
Learning tensorflow in the past few days, I have seen about tf.variable_scope and tf.name_scop, but I have not had a deep understanding of their functions.Reprint a blog:
The difference between tf.name_scope() and tf.variable_scope() in Tensorflow Record the effect,Forget one side later.
Briefly say it here:
tf.variable_scope allows variables to have the same name, including the variables obtained by tf.get_variable and the variables of tf.Variable
tf.name_scope allows variables to have the same name, only limited to tf.Variable variables
Example:
import tensorflow as tf;import numpy as np;import matplotlib.pyplot as plt;with tf.variable_scope('V1'):a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))a2 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')with tf.variable_scope('V2'):a3 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))a4 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')with tf.Session() as sess:sess.run(tf.initialize_all_variables())print a1.nameprint a2.nameprint a3.nameprint a4.name
Output:
V1/a1:0
V1/a2:0
V2/a1:0
V2/a2:0
Example 2:
import tensorflow as tf;import numpy as np;import matplotlib.pyplot as plt;with tf.name_scope('V1'):a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))a2 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')with tf.name_scope('V2'):a3 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))a4 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')with tf.Session() as sess:sess.run(tf.initialize_all_variables())print a1.nameprint a2.nameprint a3.nameprint a4.name
Error: Variable a1 already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:
Replace with the following code to execute
import tensorflow as tf;import numpy as np;import matplotlib.pyplot as plt;with tf.name_scope('V1'):# a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))a2 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')with tf.name_scope('V2'):# a3 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))a4 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')with tf.Session() as sess:sess.run(tf.initialize_all_variables())# print a1.nameprint a2.name# print a3.nameprint a4.name
Output:
V1/a2:0
V2/a2:0
边栏推荐
猜你喜欢
postgresql 游标(cursor)的使用
NFT市场以及如何打造一个NFT市场
【深度学习21天学习挑战赛】2、复杂样本分类识别——卷积神经网络(CNN)服装图像分类
(十二)树--哈夫曼树
安装dlib踩坑记录,报错:WARNING: pip is configured with locations that require TLS/SSL
Upload靶场搭建&&第一二关
ReentrantLock(公平锁、非公平锁)可重入锁原理
【深度学习21天学习挑战赛】1、我的手写被模型成功识别——CNN实现mnist手写数字识别模型学习笔记
flink on yarn指定第三方jar包
Matplotlib中的fill_between;np.argsort()函数
随机推荐
k3s-轻量级Kubernetes
MySql的concat和group_concat的区别
The difference between oracle temporary table and pg temporary table
PostgreSQL模式(Schema)
TensorFlow2 study notes: 7. Optimizer
postgresql 事务隔离级别与锁
flink onTimer定时器实现定时需求
SQL练习 2022/7/1
(十四)平衡二叉树
【深度学习21天学习挑战赛】1、我的手写被模型成功识别——CNN实现mnist手写数字识别模型学习笔记
Lombok的一些使用心得
WARNING: sql version 9.2, server version 11.0.Some psql features might not work.
【深度学习21天学习挑战赛】0、搭建学习环境
安装dlib踩坑记录,报错:WARNING: pip is configured with locations that require TLS/SSL
flink-sql大量使用案例
SQL练习 2022/7/2
CTFshow—Web入门—信息(9-20)
剑指 Offer 2022/7/2
Polynomial Regression (PolynomialFeatures)
with recursive用法