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的可扩展开发平台. ...
随机推荐
- CentOS 7 MySql 解压版安装配置
下载 访问www.mysql.com 点击DOWNLOADS-->Community-->MySQL Community Server 选择要下载的版本,目前可选择的有:5.5.5.6.5 ...
- IP包头结构详解
版本号(Version):长度4比特.标识目前采用的IP协议的版本号.一般的值为0100(IPv4),0110(IPv6) IP包头长度(Header Length):长度4比特.这个字段的作用是为了 ...
- (light oj 1306) Solutions to an Equation 扩展欧几里得算法
题目链接:http://lightoj.com/volume_showproblem.php?problem=1306 You have to find the number of solutions ...
- 手动安装Package Control
手动下载一个package control的包:https://github.com/wbond/package_control 然后Download ZIP后,解压,将解压后的文件夹重命名为 Pac ...
- [C++项目]2048控制台游戏
#include <iostream> #include <windows.h> #include <ctime> using namespace std; ; ; ...
- flutter Provide 状态管理篇
Provide是Google官方推出的状态管理模式.官方地址为: https://github.com/google/flutter-provide 现在Flutter的状态管理方案很多,redux. ...
- gVim编辑器 操作篇
gVim是一款强大的编辑器,可以满足大部分语言的编程需要.尤其是其自带的模板定制功能对于Verilog来说非常受用.然而gVim有很多操作是不同于其他编辑器的,这让很多初学者望而却步,因此,本文将gV ...
- CentOS_7下安装Nginx服务
安装make: yum -y install gcc automake autoconf libtool make make是一个命令工具,是一个解释makefile中指令的命令工具.它可以简化编译过 ...
- ZooKeeper连接并创建节点以及实现分布式锁操作节点排序输出最小节点Demo
class LockThread implements Runnable { private DistributedLock lock; public LockThread(int threadId, ...
- 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) - 4.28
赛后补了几道 赛中我就写了两个... A - Altruistic AmphibiansGym - 101933A 看了眼榜没几个人做.就没看. 最后发现就是一个DP(但是我觉得复杂度有点迷) 题意: ...