tensorflow中的“tf.name_scope()”有什么用?

CPU深度学习训练速度过慢+keras深度学习训练常见函数解释 2020/5/2 日记录 cpu训练速度慢的可能原因 请参考 深度学习训练速度的提高的一些浅见(1)gpu,cpu,硬盘等硬件 1.【线程】 pytorch以及tensorflow的多线程输入设定过大,一般推荐较大数据流4线程,较小2线程。具体问题具体分析,要看数据输入是否是训练速度优化的瓶颈。 numpy或者opencv等的多线程操作或者tensorflow以及pytorch在cpu运行上的op... 阅读详情

转自:https://www.jianshu.com/p/635d95b34e14

1. tf.name_scope()命名空间的实际作用

(1)在某个tf.name_scope()指定的区域中定义的所有对象及各种操作,他们的“name”属性上会增加该命名区的区域名,用以区别对象属于哪个区域;
(2)将不同的对象及操作放在由tf.name_scope()指定的区域中,便于在tensorboard中展示清晰的逻辑关系图,这点在复杂关系图中特别重要。

见下例:

import tensorflow as tf;  
tf.reset_default_graph()

# 无tf.name_scope()
a = tf.constant(1,name='my_a') #定义常量
b = tf.Variable(2,name='my_b') #定义变量
c = tf.add(a,b,name='my_add') #二者相加(操作)
print("a.name = "+a.name)
print("b.name = "+b.name)
print("c.name = "+c.name)

# 有tf.name_scope()
# with tf.name_scope('cgx_name_scope'): #定义一块名为cgx_name_scope的区域,并在其中工作
#     a = tf.constant(1,name='my_a')
#     b = tf.Variable(2,name='my_b')
#     c = tf.add(a,b,name='my_add')
# print("a.name = "+a.name)
# print("b.name = "+b.name)
# print("c.name = "+c.name)

# 保存graph用于tensorboard绘图
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    writer = tf.summary.FileWriter("./test",sess.graph)
    print(sess.run(c))
writer.close()
# 输出结果
# 无tf.name_scope()
a.name = my_a:0
b.name = my_b:0
c.name = my_add:0

# 有tf.name_scope()
a.name = cgx_name_scope/my_a:0
b.name = cgx_name_scope/my_b:0
c.name = cgx_name_scope/my_add:0

从输出结果可以看出,在tf.name_scope()下的所有对象和操作,其name属性前都加了cgx_name_scope,用以表示这些内容全在其范围下。
下图展示了两种情况的tensorboard差异,差别一目了然。

 

namescope.jpg

2. name_scope()只决定“对象”属于哪个范围,并不会对“对象”的“作用域”产生任何影响。

tf.name_scope()只是规定了对象和操作属于哪个区域,但这并不意味着他们的作用域也只限于该区域(with的这种写法很容易让人产生这种误会),不要将其和“全局变量、局部变量”的概念搞混淆,两者完全不是一回事。在name_scope中定义的对象,从被定义的位置开始,直到后来某个地方对该对象重新定义,中间任何地方都可以使用该对象。本质上name_scope只对对象的name属性进行圈定,并不会对其作用域产生任何影响。这就好比甲、乙、丙、丁属于陈家,这里“陈家”就是一个name_scope划定的区域,虽然他们只属于陈家,但他们依然可以去全世界的任何地方,并不会只将他们限制在陈家范围。
见下例:
(1)程序首先指定了命名区域cgx_1,并在其中定义了变量a,紧接着case1直接在cgx_1中输出a.name = cgx_1/my_a:0,这很好理解,跟想象的一样;
(2)case2在cgx_1之外的公共区域也输出了相同的a.name,这就说明a的作用范围并没有被限制在cgx_1中;
(3)接着程序又新指定了命名区域cgx_2,并在其中执行case3,输出a.name,结果还是和case1和case2完全相同,实际上还是最前面定义的那个a,这更进一步说明name_scope不会对对象的作用域产生影响;
(4)★★接着在cgx_2中重新定义了变量“a”,紧接着就执行case4,输出a.name = cgx_2/my_a:0,可见此时的结果与前面三个case就不同了,说明这里新定义的a覆盖了前面的a,即使他们在两个完全独立的name_scope中;
(5)case5输出的结果与case4结果相同,这已经无须解释了。

#name_scope 的影响范围
with tf.name_scope('cgx_1'): #指定cgx_1区域
    a = tf.Variable(tf.constant(4),name='my_a') #定义变量a
    print("case1: a.name="+a.name) #cgx_1区域内输出
print("case2: a.name="+a.name) #cgx_1区域外输出

# 保存graph用于tensorboard绘图
with tf.name_scope('cgx_2'): #新指定cgx_2区域
    print("case3: a.name="+a.name) #在cgx_2内输出cgx_1中定义a.name
    a = tf.Variable(tf.constant(4),name='my_a') #★★重新定义a这个变量
    print("case4: a.name="+a.name)
print("case5: a.name="+a.name)
# 输出结果:
case1: a.name = cgx_1/my_a:0
case2: a.name = cgx_1/my_a:0
case3: a.name = cgx_1/my_a:0
case4: a.name = cgx_2/my_a:0
case5: a.name = cgx_2/my_a:0

3. tf.name_scope('cgx_scope')语句重复执行几次,就会生成几个独立的命名空间,尽管表面上看起来都是“cgx_scope”,实际上tensorflow在每一次执行相同语句都会在后面加上“_序数”,加以区别。

见下例:
(1)指定了“cgx_scope”命名区域,并在其中定义变量a;
(2)又指定了相同名称的“cgx_scope”命名区域,并在其中定义变量b;
(3)输出a.name = cgx_scope/my_a:0和b.name = cgx_scope_1/my_b:0,可见b.name已经自动加了“_1”,这是tensorflow的特点,自动检测是否重复,有重复就自动增加数字作为标记。

with tf.name_scope('cgx_scope'):
    a = tf.Variable(1,name='my_a')

with tf.name_scope('cgx_scope'):
    b = tf.Variable(2,name='my_b')

c = tf.add(a,b,name='my_add')

print("a.name = "+a.name)
print("b.name = "+b.name)

# 保存graph用于tensorboard绘图程序(同上)
# 输出结果
a.name = cgx_scope/my_a:0
b.name = cgx_scope_1/my_b:0 #自动增加了“_1”

下图进一步说明a和b不在同一个name_scope中

 

相同name_scope.jpg

4. 如何在不同的地方将对象指定到相同的name_scope中?

方法:
with tf.name_scope('cgx_scope') as cgxscope:
……
with tf.name_scope(cgxscope) :
……
见下例:
将前一例程序做简单修改,输出结果a和b都在cgx_scope的命名范围内,后面的tensorboard也说明了这点。

with tf.name_scope('cgx_scope') as cgxscope:
    a = tf.Variable(1,name='my_a')

with tf.name_scope(cgxscope):
    b = tf.Variable(2,name='my_b')

c = tf.add(a,b,name='my_add')

print("a.name = "+a.name)
print("b.name = "+b.name)
# 输出结果
a.name = cgx_scope/my_a:0
b.name = cgx_scope/my_b:0

下图说明此例中a和b位于相同的name_scope中。

 

相同name_scope.jpg

5. 多重name_scope命名空间

多个with tf.name_scope()重叠使用,如下例。

with tf.name_scope('cgx_scope_1'): #第一重命名空间
    with tf.name_scope('cgx_scope_2'): #第二重命名空间
        a = tf.Variable(1,name='my_a')

with tf.name_scope('cgx_scope_3'):
    b = tf.Variable(2,name='my_b')

c = tf.add(a,b,name='my_add')

print("a.name = "+a.name)
print("b.name = "+b.name)
# 输出结果
a.name = cgx_scope_1/cgx_scope_2/my_a:0
b.name = cgx_scope_3/my_b:0

多重名命名空间.jpg

6. 常量tf.constant()即使在某个name_scope下被定义,在tensorboard中也不会被绘制到该name_scope下,主要考虑到图像的结构问题。

如下例:
常量a和变量b都定义在cgx_scope下,操作c定义在公共区域。从输出结果看,一切正常。但后面的tensorboard图跟想象的有点区别,按规律my_a和my_b都应该在cgx_scope内,但图中显然没有。猜测,tensorboard中常量通常在水平左侧,形式比较固定,而变量通常在下侧,形式也比较随意,因此把常量和变量画在同一name_scope下对图的逻辑有较大影响。

该例中如果把c = tf.add(a,b,name='my_add')同样写入cgx_scope下,那么在tensorboard中整个过程都会被放入cgx_scope下。

with tf.name_scope('cgx_scope'):
    a = tf.constant(1,name='my_a')
    b = tf.Variable(2,name='my_b')

c = tf.add(a,b,name='my_add')

print("a.name = "+a.name)
print("b.name = "+b.name)
print("c.name = "+c.name)
# 输出结果
a.name = cgx_scope/my_a:0
b.name = cgx_scope/my_b:0
c.name = my_add:0

常量一般不显示在name_scope内.jpg

7. tf.name_scope()对由tf.get_variable()产生的变量没有影响

如下例:
在cgx_scope命名空间下,a是由tf.get_variable()生成的变量,b是由tf.Variable()生成的变量,按规律a.name和b.name的前缀都是cgx_scope,但从输出结果可看出,a.name的前缀没出现,说明a不在该命名空间。后面的tensorboard图也说明了这点。出现这种情况是由tf.get_variable()的特性决定的,详见其他文章。

with tf.name_scope('cgx_scope'):
    a = tf.get_variable('my_a',initializer=4)
    b = tf.Variable(2,name='my_b')

c = tf.add(a,b,name='my_add')

print("a.name = "+a.name)
print("b.name = "+b.name)
# 输出结果
a.name = my_a:0
b.name = cgx_scope/my_b:0

get_valuable()不受name_scope影响.jpg

tensorflow入门:tf.name_scope,tf.variable_scope、变量共享 1. tf.name_scope('scope_name')或tf.name_scope(named_scope)主要与tf.Variable搭配使用;当传入字符串时,用以给变量名添加前缀,类似于目录,如case1所示;当传入已存在的name_scope对象时,则其范围内变量的前缀只与当前传入的对象有关,与更上层的name_scope无关,如case2所示。import tensorflow as... 阅读详情

相关推荐

彻底弄懂tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope异同

我之前有转载过一篇讲tf.variable_scope和tf.name_scope的文章,今天一看,觉得还是没介绍太清楚,没讲到点子上,毕竟英文什么的看起来还是挺麻烦的。故而,今天来细细梳理下tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope四者之间的区别与联系。

qq_22522663的博客 8155

tf.name_scope

tf.name_scope site/en/api_docs/api_docs/python/tf/name_scope.md tf.name_scope 函数是为了解决命名冲突问题。 1. Class name_scope 1.1 Aliases Class tf.keras.backend.name_scope Class tf.name_scope aliases:n. 别名 (alias 的复数) scope [skəʊp]:n. 范围,余地,视野,眼界,导弹射程 vt. 审视 A conte

程永强 2534

tensorflow里面共享变量、name_scope, variable_scope等如何理解

tensorflow里面共享变量、name_scope, variable_scope等如何理解 name_scope, variable_scope目的:1 减少训练参数的个数。 2 区别同名变量 为什么要共享变量?我举个简单的例子:例如,当我们研究生成对抗网络GAN的时候,判别器的任务是,如果接收到的是生成器生成的图像,判别器就尝试优化自己的网络结构来使自己输出0,如果接收到的是来自真实数据的图

u012564409的专栏 4433

tf.name_scope用法

tf.name_scope:用于定义Python op的上下文管理器。 tf.name_scope( name ) 此上下文管理器将推送名称范围,这将使在其中添加的所有操作的名称带有前缀。 例如,定义一个新的Python opmy_op: def my_op(a, b, c, name=None): with tf.name_scope("MyOp") as scope: a = tf.convert_to_tensor(a, name="a") b = tf.co.

qq_36201400的博客 7786

tensorflow学习之路(2-2):tf.variable_scope(),tf.name_scope(),tf.get_variable()的认识(补充)

tensorflow tf.get_variable() tf.variable_scope() tf.name_scope()

IB_H20的博客 3718

OpenVPN 新增 windows 客户端

虚拟私人网络(Virtual Private Network,简称VPN)是一种网络技术,它允许用户通过公共网络(如互联网)建立一个安全的连接通道。本文介绍如何添加 windows 客户端。

利昂的技术存档 3770

tf.name_scope()详解【命名空间其实就是给几个变量包一层名字,方便变量管理】

命名空间其实就是给几个变量包一层名字,方便变量管理。函数是:tf.name_scope 另外,就像操作系统文件夹命名一样,不同的顶层文件夹下,可以有同名文件夹。这里,不同的命名空间下,可以有名字相同的变量。 tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理。 Signature: tf.name_scope(*args, **kwds) Docstring: Returns a context manager for use when defining a P

Drug discovery 1万+

避坑指南:企业微信IP白名单设置失败的5种常见原因及解决方案

本文深入解析企业微信API调用中IP白名单设置失败的5种常见原因,包括动态公网IP变更、代理服务器导致的IP失真等,并提供针对性的解决方案。特别针对可信IP配置问题,详细介绍了诊断方法和Java代码示例,帮助企业开发者快速解决访问限制问题,确保API调用安全稳定。

weixin_29280657的博客 619

02 tensorflow中的“tf.name_scope()”

链接:https://www.jianshu.com/p/635d95b34e14 (1)在某个tf.name_scope()指定的区域中定义的所有对象及各种操作,他们的“name”属性上会增加该命名区的区域名,用以区别对象属于哪个区域; (2)将不同的对象及操作放在由tf.name_scope()指定的区域中,便于在tensorboard中展示清晰的逻辑关系图,这点在复杂关系图中特别重要。 ...

qq_40723205的博客 204

阿里云大模型ACP认证模拟考试(二)

摘要:在阿里云大模型高级工程师 ACP 认证暂无真题题库的现状下,模拟题具有重要意义。其依照真实考试设计,涵盖题型、题量与分值分布,助力考生熟悉考试框架,合理分配答题时间精力,提升效率。同时,模拟题紧扣官方课程,能帮助考生精准定位知识盲点,如大模型架构原理和通义千问应用实践等重点内容,以便针对性完善知识体系。此外,通过练习模拟题,考生可掌握不同题型的解题思维与技巧,快速洞察复杂题目本质,提高答题准确率。

davidkorhenn的博客 2082

别再只会用555了!手把手教你用运放和RC电路搭一个50Hz正弦波信号源(附Multisim仿真文件)

本文详细介绍了如何使用运放LM358和RC电路搭建一个精准的50Hz正弦波信号源,涵盖文氏电桥核心原理、单电源供电解决方案、Multisim仿真及实物调试技巧。通过实战指南和优化建议,帮助电子爱好者快速掌握低频信号生成技术,适用于各种测试和校准场景。

weixin_34116482的博客 366

tf.variable_scope和tf.name_scope的用法

tf.variable_scope可以让变量有相同的命名,包括tf.get_variable得到的变量,还有tf.Variable的变量 tf.name_scope可以让变量有相同的命名,只是限于tf.Variable的变量 例如: import tensorflow as tf; import numpy as np; import matplotlib.pyplot as plt

UESTC_C2_403的博客 5万+

0009 用VisualStudio设计Max工具

这次分享个独家专利^)^:在Visual Studio中设计3dsMax脚本工具界面。 某次做原型设计时,需求改来改去,实在懒得搞GUI;虽然也尝试在Axure RP里画原型,但还是Visual Studio更顺手,也更容易测试一些交互,就直接在VS里做了。后来突发奇想,想实验一把能否直接用到Max里去,结果没想到成功了,效果还挺好。发现了新大陆,记录一下。   开始: 在VS里新建W...

whwst的专栏 770

Tensorflow中tf.name_scope() 和 tf.variable_scope() 的区别

目录 一、name_scope和variable_scope的用途 (1)variable_scope用途:共享变量 (2)可视化用途:画Tensorboard流程图时封装 二、TensorFlow中name_scope和variable_scope区别 (1)TF中创建变量的方式有两种:tf.get_variable()和tf.Variable() (2)Tensorflow中有两种...

Microstrong 1640

Python全栈入门到实战【JavaScript篇 15】async/await与Fetch API,打通前后端数据通道

Fetch API是浏览器内置的现代HTTP请求接口,用于向服务器发送请求并获取响应。它是(XHR)的替代品。学习Fetch的目标非常明确——用JS从后端API获取数据,并渲染到前端页面。这标志着前后端数据通道的打通,是实现全栈开发的核心技能。try {// 等待Promiseconst parallel = await Promise.all([ // 并行等待]);

那些看似不起波澜的日复一日,会在某天让你看到坚持的意义 1984

openmediavault(OMV)(30)插件介绍(3)filebrowser文件管理器

filebrowser可以对omv的文件进行图形化管理,而不需要每次都通过命令进行修改。filebrowser安装。

Monster✺◟(∗❛ัᴗ❛ั∗)◞✺的博客 2321

【TensorFlow】tf.name_scope、tf.variable_scope使用

一、tf.name_scope(‘scope_name’) tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理。 import tensorflow as tf ''' Signature: tf.name_scope(*args, **kwds) Docstring: Returns a context manager for use when d...

秋天 996

车载以太网网络测试-24【SOME/IP概述】

SOME/IP**(Scalable Service-Oriented Middleware over IP)是一种基于IP网络的面向服务的通信协议,专为汽车电子系统设计,支持服务发现、远程过程调用(RPC)和事件通知等功能。核心标准是由 AUTOSAR(汽车开放系统架构)定义,现已成为车载通信的重要标准之一(如AUTOSAR 4.0+)。SOME/IP的全称是,它是一种基于IP网络的面向服务的通信协议,广泛应用于汽车电子(AUTOSAR)和物联网领域。Scalable(可扩展的)含义。

车载网络测试工程师的博客 2193

『tensorflow笔记』两种作用域类型tf.name_scope,tf.name_scope

命名域 (name scope),通过tf.name_scope 或 tf.op_scope创建; 变量域 (variable scope),通过tf.name_scope或 tf.variable_op_scope创建; 这两种作用域,对于使用tf.Variable()方式创建的变量,具有相同的效果,都会在变量名称前面,加上域名称。 对于通过tf.get_variable()方式创建的变量,只...

AI新视界 2453

深度学习工具包 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")

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

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

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

Winter_FDd的博客 1万+

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

(TensorFlow)——tf.variable_scope和tf.name_scope详解

这几天学习tensorflow,看到关于tf.variable_scope和tf.name_scop,一直没有深入了解其中的作用。转载一篇博客: Tensorflow中tf.name_scope() 和 tf.variable_scope() 的区别记录一下作用,一面以后忘记。 在这里简单点说下: tf.variable_scope可以让变量有相同的命名,包括tf.get_variable...

weixin_41637329的博客 408

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

tf.name_scope与tf.variable_scope区别

命名机制与变量共享 Variable 变量 (一般表达参数)、Tensor(操 作输出)、操作 Operation、Placeholder 输 入都有名字 当模型复杂的时候,需要有效的命名机制: 方便、清晰 TF 中的命名机制 有效的命名机制: 起名方便 (不用从 w1 起到 w1024…) 甚至不显式写名,也能自动给个好找的名字 3. 同时避免重名带来的名字冲突 TF ...

博客 606

TensorFlow笔记:tf.get_variable,tf.Variable(),tf.variable_scope,tf.name_scope

tf.get_variable,tf.Variable() tf.get_variable(“vname”)在创建变量时,如果这个变量vname已经存在,直接使用这个变量,如果不存在,则重新创建; tf.Variable()在创建变量时,一律创建新的变量,如果这个变量已存在,则后缀会增加0、1、2等数字编号予以区别。 import tensorflow as tf with tf.name_sc...

qq_38410428的博客 500

tf.name_scope()和tf.variable_scope()的用法

tf.name_scope()实现变量的分级管理,该作用域中的tf.Variable变量的name前面会自动加上scope_name的前缀,而tf.get_variable变量不会受到影响,既不会加前缀 import tensorflow as tf with tf.name_scope("scp1"): v1=tf.get_variable('v1', shape=[1], initiali...

eefresher的博客 1104

Tensorflow中的tf.name_scope()应用

1. tf.name_scope()命名空间的实际作用 (1)在某个tf.name_scope()指定的区域中定义的所有对象及各种操作,他们的“name”属性上会增加该命名区的区域名,用以区别对象属于哪个区域; (2)将不同的对象及操作放在由tf.name_scope()指定的区域中,便于在tensorboard中展示清晰的逻辑关系图,这点在复杂关系图中特别重要。 见下例: import t...

lty_sky的专栏 437
上一篇: tensorflow中os.environ["TF_CPP_MIN_LOG_LEVEL"]的值的含义
下一篇: tf.placeholder函数说明
money_yuan
博客等级 码龄10年 104粉丝 80原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值