关于tensorflow中的scope的命名及使用方法

TENSORFLOW变量作用域(VARIABLE SCOPE) 主要介绍了TENSORFLOW变量作用域(VARIABLE SCOPE),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 立即下载

相信大家在学习和使用tensorflow的过程中肯定会遇到过关于scope的一些问题,scope是一种给变量命名的方法,它会让变量命名变得简单,同时也常用于reusing variable 代码中,下面将会给大家讲解一下tensorflow中两种定义scope的方法,来帮助大家更好的理解和使用scope。

前言:scope的命名方式分为两种,tf.name_scope( )和tf.variable_scope( ),生成变量的途径也有两种:一种是 tf.get_variable( )需要提前定义一个initiallizer; 另一种是 tf.Variable( ).

一、在tf.name_scope() 的框架下使用这两种方式

首先我们先展示一段代码:

import tensorflow as tf

with tf.name_scope("a_name_scope"):
    initializer = tf.constant_initializer(value=1)
    var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32, initializer=initializer)
    var2 = tf.Variable(name='var2', initial_value=[2], dtype=tf.float32)
    var21 = tf.Variable(name='var2', initial_value=[2.1], dtype=tf.float32)
    var22 = tf.Variable(name='var2', initial_value=[2.2], dtype=tf.float32)


with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    print(var1.name)        # var1:0
    print(sess.run(var1))   # [ 1.]
    print(var2.name)        # a_name_scope/var2:0
    print(sess.run(var2))   # [ 2.]
    print(var21.name)       # a_name_scope/var2_1:0
    print(sess.run(var21))  # [ 2.0999999]
    print(var22.name)       # a_name_scope/var2_2:0
    print(sess.run(var22))  # [ 2.20000005]

在session下我们分别打印出各个变量的名字和值,打印出来的结果显示在每一句代码后面的#注释部分。仔细观察上面的结果我们不难看出,在tf.name_scope( )这种框架下,如果使用tf.get_variable( )这种途径创建的变量,其名字是不受scope的名字所影响的。接下来用tf.Variable( )这种方式创建的变量,输出时会将scope的名字一起输出(即在scope这个变量下创建的变量),而且他们都有自己的变量名字并且相同,程序没有报错但也没数输出三个相同名字的,那是因为在创建变量时,会检测是否该变量的名字已经存在,如果存在则改变其名字,因此才会输出2_1和2_2。

二、在tf.variable_scope() 的框架下使用这两种方式

with tf.variable_scope("a_variable_scope") as scope:
    initializer = tf.constant_initializer(value=3)
    var3 = tf.get_variable(name='var3', shape=[1], dtype=tf.float32, initializer=initializer)
    var4 = tf.Variable(name='var4', initial_value=[4], dtype=tf.float32)
    var5 = tf.Variable(name='var4', initial_value=[5], dtype=tf.float32)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(var3.name)            # a_variable_scope/var3:0
    print(sess.run(var3))       # [ 3.]
    print(var4.name)            # a_variable_scope/var4:0
    print(sess.run(var4))       # [ 4.]

可以看出,在tf.variable_scope( )这种框架下,两种产生变量的途径创建的变量在输出时都会把scope变量的名字一起输出。
在有的时候程序中需要我们重复使用之前定义过的变量,那我们应该怎么办呢,那么我们就可以使用tensorflow中的scope.reuse_variables()。

with tf.variable_scope("a_variable_scope") as scope:
    initializer = tf.constant_initializer(value=3)
    var3 = tf.get_variable(name='var3', shape=[1], dtype=tf.float32, initializer=initializer)
    scope.reuse_variables()
    var3_reuse = tf.get_variable(name='var3',)
    var4 = tf.Variable(name='var4', initial_value=[4], dtype=tf.float32)
    var4_reuse = tf.Variable(name='var4', initial_value=[4], dtype=tf.float32)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(var3.name)            # a_variable_scope/var3:0
    print(sess.run(var3))       # [ 3.]
    print(var3_reuse.name)      # a_variable_scope/var3:0
    print(sess.run(var3_reuse)) # [ 3.]
    print(var4.name)            # a_variable_scope/var4:0
    print(sess.run(var4))       # [ 4.]
    print(var4_reuse.name)      # a_variable_scope/var4_1:0
    print(sess.run(var4_reuse)) # [ 4.]

从上面可以看到,如果想要达到重复利用变量的效果, 我们就要使用 tf.variable_scope(), 并搭配 tf.get_variable() 这种方式产生和提取变量. 不像 tf.Variable() 每次都会产生新的变量, tf.get_variable() 如果遇到了同样名字的变量时, 它会单纯的提取这个同样名字的变量(避免产生新变量). 而在重复使用的时候, 一定要在代码中强调 scope.reuse_variables(), 否则系统将会报错, 以为你只是单纯的不小心重复使用到了一个变量。
简单来看:

  1. 使用tf.Variable()的时候,tf.name_scope()和tf.variable_scope() 都会给 Variable 和 op 的 name属性加上前缀。
  2. 使用tf.get_variable()的时候,tf.name_scope()就不会给 tf.get_variable()创建出来的Variable加前缀。

个人觉得name_scope的作用在于封装一大堆操作让生成的数据流图具有层次化,方便navigate。variable_scope则是一个更加值得注意的东西。它控制着变量的分级命名。变量命名很重要如果变量名冲突是要报错的,还有就是它控制变量共享,你可以指定控制在某一个scope下所有的变量共享。name_scope是作用在操作上的,操作如果命名冲突没关系,tf会自动帮你加一个后缀,无关紧要。如果你希望好调试,那就为操作命个名。

通过上面的学习,相信各位同学能够很好的理解scope的命名方法了吧,那么就赶紧去实战吧!!!

Multisim 11.0的安装破解汉化步骤 15:打开注册机,选择管理员运行,默认第一项,选择生成文件路径,这里不要随意选择,要找到你安装Multisim安装路径下Shard下的License Manager路径下,否则会报错,破解失败。14:后面会出现一个更新提示,选择否,然后选择重启电脑后,先不要选择重启电脑,在这里出现了问题(可能敲击回车,默认去重启电脑了),所以我说不要重启电脑,重启破解时右下角出现这,点击否。17:重启电脑,如果前面重启过,那可以跳过重启,应用程序下找到下图所示,启动。16:点击确认覆盖,路径下生成了该文件。 阅读详情

相关推荐

奇异值分解(SVD)C++实现

奇异值分解SVD(紧奇异值分解,截断奇异值分解)C++实现

weixin_45804601的博客 4172

tensorflow scope的意义

tensorflow中创建variable的2种方式 tf.Variable():只要使用该函数,一律创建新的variable,如果出现重名,变量名后面会自动加上后缀 var:0 var_1:0 var_2:0 tf.get_variable():如果变量存在,则共享以前创建的变量,如果不存在,则新创建一个变量 1,变量存在,但无法共享 2,变量不存在,但无法创建 tensorflow中的两种作用域 命名域(name scope):通过tf.name_scope()来实现; 变量

潘钟鸣的博客 578

基于Jira与Confluence的企业知识库AI助手构建实战

检索增强生成(RAG)技术通过结合信息检索与大型语言模型,有效解决了AI在专业领域知识不足的问题。其核心原理是将外部知识库(如企业文档)进行向量化存储,在用户提问时进行语义检索,并将相关上下文注入提示词中,从而生成精准、有据可依的回答。这一技术架构在工程实践中价值显著,它无需重新训练模型即可让AI掌握特定领域的专业知识,大幅提升了回答的准确性与实用性。典型的应用场景包括智能客服、企业内部知识问答以及代码辅助等。本文以开源项目`dev-kostiuk/jira-confluence-ai-context`为例

weixin_42520448的博客 367

tf中的scope探索,以及tf.variable_scope和 tf.VariableScope的关系和区别

“withtf.variable_scope("scope_name") as vs:” 上面这句是我们最常用的语句之一,用来定义一个域,以方便对变量进行操作,tf.variable_scope("scope_name") 会得到一个variable_scope的类。 但是,如果我们用函数tf.get_variable_scope() 来获取当前域。会得到 yyy = tf.variable_scope('scope_name') print(yyy) sss = tf.get_variable_.

kangshuangzhu的博客 964

S32K144驱动TPS929120避坑实录:从原理图设计到PWM调光漏电流的完整解决流程

本文详细解析了S32K144驱动TPS929120在车规LED驱动中的常见问题与解决方案,重点解决了PWM调光漏电流的行业难题。从原理图设计、FlexWire通信到软件调试,提供了一套完整的工程化避坑指南,特别针对车规MCU应用中的温度漂移、地址冲突等痛点给出了实用建议。

weixin_30872157的博客 561

TMS320F280049C 学习笔记21 交叉开关 Crossbar (X-BAR)

简单介绍了X-BAR。

whyorwhnt的专栏 2万+

Tensorflow中scope命名方法

两篇文章掌握Tensorflow中scope用法: 【1】Tensorflow中scope命名方法(本文) 【2】Tensorflow中tf.name_scope() 和 tf.variable_scope() 的区别 微信公众号 1. tf.name_scope() 在 Tensorflow 当中有两种途径生成变量 variable,一种是 tf.get_variable(),另一种...

Microstrong 2506

使用TinyProxy搭建代理服务器

背景 办公电脑所在网络无法连接外网,机房服务器可以连接外网,通过服务器代理上网; 一、CentOS下TinyProxy安装–搭建代理服务器 TinyProxy是个轻量级简单的HTTP/HTTPS代理程序,GitHub和官网,安装方式这里主要介绍两种 CentOS下yum安装,参考:TinyProxy安装 Docker安装 先安装docker环境yum install docker -y 二、Win10下使用 ...

椰汁菠萝 1万+

tensorflow scope()函数大型指导

目录 get_model_varibales()和get_variables() tf.valuable_scope() tf.name_scope()名称覆盖机制 slim升级至tf.contrib.framework.arg_scope()/tf.contrib.layers get_model_varibales()和get_variables() ...

猫头军师兵书 672

手把手教你新建一个winform项目(史上最全)

本文介绍如何利用visual studio新建一个winform程序

qq_34059233的博客 4万+

人工智能训练师三级补贴3120元申领全攻略|条件+流程+五省市对比

补贴是真的,但不是"白给"的。你需要满足社保、户籍、时间等条件,严格按照流程操作。记住四件事先确认自己是否满足条件,再报名——别考完了才发现领不到紧缺工种补贴更高——确认你所在城市是否在紧缺目录中证书下发后12个月内申请——设个日历提醒材料准备齐全再提交——避免来回折腾如果你正在考虑考人工智能训练师三级证书,想要从报名到备考到拿证到申领补贴的完整一站式指南,欢迎查看我的付费专栏:📖《人工智能训练师三级·从零到拿证完全指南》80篇系统化内容,覆盖报名、备考、冲刺、拿证全流程。

白话机器学习 1522

Tensorflow学习记录8 scope

""" 命名方式 What's name_scope? What's variable_scope? 基本上都是把变量的名字动一下手脚 """ import tensorflow as tf # # ############## name_scope ####################################################################...

RonnyChan的博客 252

智能车图像处理12-进阶篇4--环岛辅助判断条件

主函数: 第6行: 含义是当前需要不在坡道和车库状态 第11-12行 : 寻找一下环岛的双断点(在辅助函数中) 第16-66行: 当左右线的方差满足一边很大,一边很小时,truepianfangflag /truepianfangflagforleft 置1,在环岛的判断中使用。 辅助函数: 双断点函数(左与右相似,以做为例): 第13-28行: 第一处断点 第29-40行: 第一处断点后 连续边线 第41-52行: 第一处连续后,第二处断点 第53-64行: 第二处断点后,第二处

taiyuezyh的博客 1万+

tensorflow:scope 命名方法

文章目录遇到问题两种命名空间tf.name_scope()tf.variable_scope()项目中的解决方法参考 遇到问题 在使用cleverhans这个对抗工程时https://github.com/tensorflow/cleverhans,想把他给的网络结构改成自己的网络结构,然后进行攻击和防御时,一直遇到关于tensorflow命名变量的问题,特此来记录下 两种命名空间 tf.name...

苏门蒲元的博客 279

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

十八.TensorFlow之scope命名方式

TensorFlow之scope命名方式scope 能让你命名变量的时候轻松很多. 同时也会在 reusing variable 代码中常常见到. 所以今天我们会来讨论下 tensorflow 当中的两种定义 scope 的方式. 最后并附加一个 RNN 运用 reuse variable 的例子.在 Tensorflow 当中有两种途径生成变量 variable, 一种是 tf.get_vari...

码哥驾到 3354

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

tensorflow中的scope命名方法:name_scope, variavle_scope

转自:知乎回答:tensorflow里面name_scope, variable_scope等如何理解? 1 在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型)、或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量。另外一方面是当一个深度学习模型变得非常复杂的时候,往往存在大量的变量和操作,如何避免这些变量名和操作名的唯一不重复...

taolusi的博客 1313

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Tensorflow中的命名空间scope

1、name_scope   在tensorflow中有两种声明变量的方式,tf.get_variable()和tf.Variable().   name_scope对于tf.get_variable()无效,只在tf.Variable()中起作用        使用tf.Variable()这种方式创建变量时,首先会检查是否有此时要创建的变量name,如果存在则自动分配一个不同...

a493862864的博客 218

TensorFlow——命名空间tf.variable_scope和tf.name_scope的区别

tf.variable_scope和tf.name_scope都提供了命名空间管理的功能。这两个函数在大部分情况下是等价的,唯一的区别是在使用tf.get_variable函数时。 简单来说,tf.get_variable函数受tf.variable_scope命名空间影响,不受tf.name_scope命名空间影响。如下面代码所示: import tensorflow as tf with tf.variable_scope("a1"): # 在命名空间a1下获取变量“a1”,于是得到的变

CMTSA的博客 169

tensorflow变量命名相关以及tf.variable_scope和tf.name_scope

1.tensorflow变量名字 tensorflow提供了通过变量名称来创建或者获取一个变量的机制。可以通过变量的名字来使用变量,同时也能通过判断变量名称知道变量是否重复。当reuse为False或者None时(这也是默认值),同一个tf.variable_scope下面的变量名不能相同;当reuse为True时,tf.variable_scope只能获取已经创建过的变量。 reuse=F...

weixin_42752435的博客 1487

python库--tensorflow--scope命名方式

方法 参数 说明 .name_scope() with...: name 在其下使用Variable, 变量名(V_n)前会被加上'name/...'且相同V_n会自动添加后缀加以区分, 使用get_variable, V_n不会变化, 且出现相同V_n会报错 default_name=None 如果`name`参数是`None`,则使用默认...

weixin_30710457的博客 114

(Tensorflow之十九)tf.variable_scope的命名空间管理

一、tf.variable与tf.get_variable生成变量的方法val = tf.variable(tf.constant(1.0,shape=[1],name="val")) val = tf.get_variable("val",1.0,shape=[1],initializer = tf.constant_initializer(1.0))这两种方法生成变量的方法几乎是等效的,区别是:

abiggg的博客 635

tensorflow scope命名方法(variable_scope()与name_scope()解析)

学习资料: 不同 scope 对比代码 reuse variable RNN 代码 sharing variable tensorflow 官网介绍 scope 能让你命名变量的时候轻松很多. 同时也会在 reusing variable 代码中常常见到. 所以今天我们会来讨论下 tensorflow 当中的两种定义 scope 的方式. 最后并附加一个 RNN 运用 reuse vari

AI_盲的博客 4073

tensorflow tf.name_scope() 命名空间(用于规定对象和操作属于哪个区域)

tf.name_scope()规定了对象和操作属于哪个区域 本质上name_scope只对对象的name属性进行圈定,并不会对其作用域产生任何影响 tf.name_scope(‘cgx_scope’)语句重复执行几次,就会生成几个独立的命名空间,尽管表面上看起来都是“cgx_scope”,实际上tensorflow在每一次执行相同语句都会在后面加上“_序数”,加以区别 如何在不同的...

Dontla的博客 390

tensorflow中共享变量 tf.get_variable 和命名空间 tf.variable_scope

tensorflow中有很多需要变量共享的场合,比如在多个GPU上训练网络时网络参数和训练数据就需要共享。 tf通过 tf.get_variable() 可以建立或者获取一个共享的变量。 tf.get_variable函数的作用从tf的注释里就可以看出来-- ‘Gets an existing variable with this name or create a new one’。 与...

牧野的博客 3702

tensorflow variable scope 变量命名空间和变量共享

import tensorflow as tf def f(): var = tf.Variable(initial_value=tf.random_normal(shape=[2])) return var a1=f() a2=f() with tf.Session() as sess: sess.run(tf.global_variables_initia...

190

tensorflow 变量及命名空间 tf.Variable() vs tf.get_variable() & tf.name_scope() vs tf.variable_scope()

tf.Variable() vs tf.get_variable() tf.name_scope() vs tf.variable_scope() 1. 基础功能 1.1 tf.Variable() 是一个类,tensorflow.python.ops.variables.Variable,其构造函数原型为 def __init__( self, initial_value: Any = None, trainable: bool = True, collections:

撒旦先生的博客 2667
上一篇: 创建自己的GitHub仓库,学习使用markdown语言
下一篇: Windows和Linux与远程服务器之间进行文件交互
Zz_Krystal
博客等级 码龄10年 36粉丝 15原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值