当前位置:网站首页>(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
边栏推荐
猜你喜欢

MySql--存储引擎以及索引

剑指 Offer 2022/7/3
Kubernetes基本入门-集群资源(二)

TensorFlow2 study notes: 6. Overfitting and underfitting, and their mitigation solutions

Kubernetes集群安装

TensorFlow2学习笔记:8、tf.keras实现线性回归,Income数据集:受教育年限与收入数据集

Simple and clear, the three paradigms of database design

NFT市场可二开开源系统

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

【CV-Learning】线性分类器(SVM基础)
随机推荐
SQL练习 2022/6/30
SQL练习 2022/7/2
剑指 Offer 2022/7/1
Thoroughly understand box plot analysis
yolov3数据读入(二)
安装dlib踩坑记录,报错:WARNING: pip is configured with locations that require TLS/SSL
win云服务器搭建个人博客失败记录(wordpress,wamp)
Kubernetes集群安装
flink-sql所有表格式format
(十五)B-Tree树(B-树)与B+树
(十一)树--堆排序
简单说Q-Q图;stats.probplot(QQ图)
flink on yarn任务迁移
彻底搞懂箱形图分析
MySQL最左前缀原则【我看懂了hh】
NFT市场以及如何打造一个NFT市场
postgres 递归查询
flink-sql自定义函数
(五)栈及其应用
The pipeline mechanism in sklearn