slim.arg_scope中python技巧
slim.arg_scope函数说明如下:
Stores the default arguments for the given set of list_ops.
For usage, please see examples at top of the file.
Args:
list_ops_or_scope: List or tuple of operations to set argument scope for or
a dictionary containing the current scope. When list_ops_or_scope is a
dict, kwargs must be empty. When list_ops_or_scope is a list or tuple,
then every op in it need to be decorated with @add_arg_scope to work.
**kwargs: keyword=value that will define the defaults for each op in
list_ops. All the ops need to accept the given set of arguments.
Yields:
the current_scope, which is a dictionary of {op: {arg: value}}
Raises:
TypeError: if list_ops is not a list or a tuple.
ValueError: if any op in list_ops has not be decorated with @add_arg_scope.
因此使用@slim.add_arg_scope修饰目标函数,从而达到用slim.arg_scope为目标函数设置默认参数的目的。而库中的conv2d等函数,已经被默认声明。因此,可以在构建网络时,快捷设置,达到节省时间的目的。同时,也可以通过下面的方式进行单独设置。
with slim.arg_scope(
[slim.conv2d,slim.max_pool2d],stride = 1,padding = 'SAME'):
net = slim.conv2d(net,32,[3,3],stride=2,scope = 'conv1')
装饰器@
# 装饰器
import time
# 装饰器,记录函数运行时间
def decorator01(fun):
def wapper():
stime = time.time()#'装饰'
fun()#第三步:执行函数功能
etime = time.time()#’装饰‘
print("fun run time is {TIME}".format(TIME=etime - stime))
return wapper # 必须要返回一个函数的内存地址
#第一步运行到这里的时候, 使用装饰器装饰某个函数,这里等价于 test01=decorator01(test01),(所谓装饰之意?)
# 即将test01实际引用变成wapper函数内存地址,所以执行test01实际是执行wapper
@decorator01
def test01():
time.sleep(2)
print("test01 is running")
#第二步:这里的时候就执行wapper()了,
test01() # 不修改代码和调用方式,实现添加记录时间功能
带参数的装饰器
# 装饰器
import time
# 如果装饰器有参数,最外层是装饰器的参数
def decorator01(*args, **kwargs):
print("装饰器参数:", *args, **kwargs)
def out(fun): #第二层才是接受的函数
def wapper(*args, **kwargs): # 使用非固定参数,无论参数是什么,都可以传递进来
stime = time.time()
fun(*args, **kwargs)
etime = time.time()
print("fun run time is {TIME}".format(TIME=etime - stime))
return wapper # 必须要返回一个函数的内存地址
return out # 要返回装饰函数的内存地址
#第一步: 装饰器本身带参数,此时 decorator01(arg)=out,即相当于 @out装饰test01,所以 test01=out(fun)=wapper
@decorator01(1)
def test01(args1):
time.sleep(2)
print("参数是 {NAME} ".format(NAME=args1))
test01("侯征") # 不修改代码和调用方式,实现添加记录时间功能
slim.arg_scope中python技巧的更多相关文章
- 机器学习中python的有关使用技巧【创建虚拟环境、jupyter的kernel修改】
1.创建虚拟环境<在原来基础上建立> *注:(这里是python2.python3环境共存,我要创建一个python3的虚拟环境) 一.先安装虚拟环境变量: pip3 install -U ...
- tf.contrib.slim arg_scope
缘由 最近一直在看深度学习的代码,又一次看到了slim.arg_scope()的嵌套使用,具体代码如下: with slim.arg_scope( [slim.conv2d, slim.separab ...
- slim.arg_scope()的使用
[https://blog.csdn.net/u013921430 转载] slim是一种轻量级的tensorflow库,可以使模型的构建,训练,测试都变得更加简单.在slim库中对很多常用的函数进行 ...
- Python技巧——list与字符串互相转换
Python技巧——list与字符串互相转换 在Python的编程中,经常会涉及到字符串与list之间的转换问题,下面就将两者之间的转换做一个梳理. 1.字符串转换成list 命令:list() ...
- 26个你不知道的Python技巧
Python是目前世界上最流行的编程语言之一.因为: 1.它容易学习 2.它用途超广 3.它有非常多的开源支持(大量的模块和库) 不好意思,优达菌又啰嗦了. 本文作者 Peter Gleeson 是一 ...
- 使用多块GPU进行训练 1.slim.arg_scope(对于同等类型使用相同操作) 2.tf.name_scope(定义名字的范围) 3.tf.get_variable_scope().reuse_variable(参数的复用) 4.tf.py_func(构造函数)
1. slim.arg_scope(函数, 传参) # 对于同类的函数操作,都传入相同的参数 from tensorflow.contrib import slim as slim import te ...
- Python技巧—list与字符串互相转换
Python技巧-list与字符串互相转换 在Python的编程中,经常会涉及到字符串与list之间的转换问题,下面就将两者之间的转换做一个梳理. 1.list转换成字符串 命令:list() 例子: ...
- 你可能不知道的 Python 技巧
英文 | Python Tips and Trick, You Haven't Already Seen 原作 | Martin Heinz (https://martinheinz.dev) 译者 ...
- Eclipse中Python开发环境搭建
Eclipse中Python开发环境搭建 目 录 1.背景介绍 2.Python安装 3.插件PyDev安装 4.测试Demo演示 一.背景介绍 Eclipse是一款基于Java的可扩展开发平台. ...
随机推荐
- docker的基本知识
Docker 是什么? Docker 是一个开源的应用容器引擎,是基于go语言的,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化. ...
- 网站通信协议升级到HTTPS&HTTP2
为何要升级到HTTPS和HTTP2? http://baijiahao.baidu.com/s?id=1602041305989767011&wfr=spider&for=pc htt ...
- Vue(五)Vue规范
代码规范很重要 1.组件名应该始终是多个单词的,根组件 App 除外. 2.组件的 data 必须是一个函数. // In a .vue file export default { data () { ...
- Visual Studio 2019 使用 Live Share
一.前言 Visual Studio 2019 在今天发布(北京时间)了,这次带来了一个比较有趣的 Live Share 功能,使用它可以进行更好的协作开发.主要功能: 更多资料可看官方介绍: Vis ...
- BAT面试题:请使用递归构建N叉树
题目要求: 现在我们拥有全国的省.市.县.镇的行政信息,比如 浙江省 -> 杭州市 -> 西湖区 --> xx街道,请将这些信息构建成一棵树,根节点为全国,叶子节点为镇. 我的误解: ...
- EXT 设置编辑框为只读
Ext.getCmp("processResult").setReadOnly(ture); //processResult是属性的id,setReadOnly(ture)设置 ...
- C#关于TreeView树在节点数较多时总是会出现闪烁的问题方法记录
首先介绍下背景吧,问题如题,这个问题应该说困扰我大半年了(不是说我没有请教大佬,不是说我没有上网查过,之前在搜索时,总是没有解决此问题~~),直到最近一次在在优化代码时,再次上网查找,在发现搜索词条” ...
- Python中的 sort 和 sorted
今天在做一道题时,因为忘了Python中sort和sorted的用法与区别导致程序一直报错,找了好久才知道是使用方法错误的问题!现在就大致的归纳一下sort和sorted的用法与区别 1. sort: ...
- MongoDB 4.0.* 远程连接及用户名密码认证登陆配置——windows
- 解决Windows10中Virtualbox安装虚拟机没有64位选项
今天想在Windows 10系统安装完Virtualbox虚拟机,然后打算装一个CENTOS系统,但是选择安装系统的时候竟然没有64位操作系统的选项,经过一阵Google,终于解决了,在这里盘点一下出 ...