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的可扩展开发平台. ...
随机推荐
- Python:time模块/random模块/os模块/sys模块
time 模块 #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取当前时间戳 python中时间日期格式化符号: %y 两位数的 ...
- 第二章 Python基本图形绘制
2.1 深入理解Python语言 Python语言是通用语言 Python语言是脚本语言 Python语言是开源语言 Python语言是跨平台语言 Python语言是多模型语言 Python的特点与优 ...
- Java获取Linux和Window系统CPU、内存和磁盘总使用率的情况
这是一个工具类,获取的内容: CPU使用率:得到的是当前CPU的使用情况,这是算出的是两次500毫秒时间差的CPU使用率 内存使用率:[1 - 剩余的物理内存/(总的物理内存+虚拟内存) ] * 1 ...
- button样式篇一(ant Design React)
这篇来介绍button中elementUi.iview.ant中样式结构 ant Design react ant-react中button分两个文件less: mixins.less:根据butto ...
- css文字与排版
目录 文字与排版样式 `font文字样式 排版样式(text) 文字半透明 文字阴影 背景和颜色 基本 背景简写 背景透明 背景缩放 列表样式 表格样式 表格边框样式 折叠边框 设置宽度和高度 表格对 ...
- jdbc连接字符串
MySQL:String Driver="com.mysql.jdbc.Driver"; //驱动程序String URL="jdbc:mysql://localhost ...
- java学习——递归
/** * 添加商品类型的功能 * 注意创建时间和修改时间在具体的方法中直接赋值 * @param gT 商品类型管理表映射的GT类的实例化对象 */ @Override public void ad ...
- CodeForces 1151E Number of Components
题目链接:http://codeforces.com/problemset/problem/1151/E 题目大意: n个人排成一个序列,标号为 1~n,第 i 个人的学习成绩为 ai,现在要选出学习 ...
- DAY22、面向对象
一.复习:1.面向过程与面向对象 过程:程序流程化,可拓展性差 对象:程序流程多样化,可拓展性强 面向对象引入属性 | 方法的概念,通过所属者.语法调用2.拥有名称空间的对象:有__dict__属性, ...
- npm 安装包失败 --- 清除npm缓存
今天同事给了一个webpack的项目,我拿过来,npm install 突然出现报错了,并且报了一个奇怪的错误, 如下所示, Unexpected end of JSON input while p ...