当前位置:网站首页>(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.nameOutput:
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.nameError: 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.nameOutput:
V1/a2:0
V2/a2:0
边栏推荐
猜你喜欢

flink-sql所有语法详解

TensorFlow2 study notes: 4. The first neural network model, iris classification

【深度学习21天学习挑战赛】1、我的手写被模型成功识别——CNN实现mnist手写数字识别模型学习笔记
![[Deep Learning 21 Days Learning Challenge] Memo: What does our neural network model look like? - detailed explanation of model.summary()](/img/99/819ccbfed599ffd52307235309cdc9.png)
[Deep Learning 21 Days Learning Challenge] Memo: What does our neural network model look like? - detailed explanation of model.summary()

TensorFlow2学习笔记:6、过拟合和欠拟合,及其缓解方案

【go语言入门笔记】12、指针

fill_between in Matplotlib; np.argsort() function

【深度学习21天学习挑战赛】2、复杂样本分类识别——卷积神经网络(CNN)服装图像分类

逻辑回归---简介、API简介、案例:癌症分类预测、分类评估法以及ROC曲线和AUC指标

Kubernetes基础入门(完整版)
随机推荐
(十三)二叉排序树
线性回归02---波士顿房价预测
Kubernetes基本入门-集群资源(二)
flink-sql自定义函数
(十二)树--哈夫曼树
CAS与自旋锁、ABA问题
TensorFlow2 study notes: 6. Overfitting and underfitting, and their mitigation solutions
postgresql中创建新用户等各种命令
win云服务器搭建个人博客失败记录(wordpress,wamp)
智能合约安全——delegatecall (2)
Androd Day02
WARNING: sql version 9.2, server version 11.0.Some psql features might not work.
read and study
MySQL事务详解(事务隔离级别、实现、MVCC、幻读问题)
flink on yarn指定第三方jar包
两个APP进行AIDL通信
TensorFlow2学习笔记:5、常用激活函数
(十一)树--堆排序
0, deep learning 21 days learning challenge 】 【 set up learning environment
flink-sql大量使用案例