首先是完善的不报错的代码及输出:
# -*- coding: UTF-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from tensorflow.contrib.framework import arg_scope,add_arg_scope
def print_hh(name = 'ss'):
print(name)
@add_arg_scope
def print_ee(name,add_args):
print(name)
print(add_args)
add_arg_scope(print_hh)
with arg_scope([print_hh],name='xx'):
print_hh()
print_hh('yy')
with arg_scope([print_ee],add_args='pp'):
with arg_scope([print_ee], add_args='mm'):
print_ee('test1')
print_ee('test2')
输出:
ss
yy
test1
mm
test2
pp
从中可以看到arg_scope的作用范围,也可以看出其与函数参数默认值之间的优先权,但是问题就在于@add_arg_scope与add_arg_scope()函数的作用:
@add_arg_scope
def print_ee(name,add_args,add_p = 'learn'):
print(name)
print(add_args)
print(add_p)
with arg_scope([print_ee],add_args='pp'):
with arg_scope([print_ee], add_args='mm'):
print_ee('test1')
print_ee('test2')
with arg_scope([print_ee],add_args='pp',add_p='cc'):
print_ee('test3')
输出:
test1
mm
learn
test2
pp
learn
test3
pp
cc
def print_hh(name = 'ss'):
print(name)
add_arg_scope(print_hh)
with arg_scope([print_hh],name='xx'):
print_hh()
print_hh('yy')
输出:
ss
yy
可以看出add_arg_scope()与@add_arg_scope在优先权上是存在区别的,在add_arg_scope()中,在arg_scope()定义的参数优先权是不如函数的参数设置的默认值的.而在@add_arg_scope中,arg_scope()定义的参数优先权是大于函数参数的默认值的.
@add_arg_scope
def print_ee(para,name='cc'):
print(para)
print(name)
with arg_scope([print_ee],para='dd'):
print_ee()
print('oo')
输出:
dd
cc
oo
def print_hh(para,name = 'ss'):
print(para)
print(name)
add_arg_scope(print_hh)
with arg_scope([print_hh],para='xx'):
print_hh()
print_hh('yy')
输出:
Traceback (most recent call last):
File "/home/saber/桌面/testForPRN/test_arg_scope.py", line 46, in <module>
print_hh()
TypeError: print_hh() takes at least 1 argument (0 given)
可以看到add_arg_scope()与@add_arg_scope,前者并没有真正把参数传过去,而后者则是可以的,这让我怀疑add_arg_scope()函数是否有发挥作用,但是当我删除该函数时,在with arg_scope()函数内使用print_hh()函数却是会报错,这说明它的确发挥这自己的作用.
并且
@add_arg_scope
def print_ee(add_p = 'learn'):
print(add_p)
with arg_scope([print_ee],add_p='pp'):
print_ee()
输出:
pp
@add_arg_scope
def print_ee(add_p):
print(add_p)
with arg_scope([print_ee],add_p='pp'):
print_ee('d')
输出:
Traceback (most recent call last):
File "/home/saber/桌面/testForPRN/test_arg_scope.py", line 55, in <module>
print_ee('s')
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 182, in func_with_args
return func(*args, **current_args)
TypeError: print_ee() got multiple values for keyword argument 'add_p'
可以看到@add_arg_scope传入的值引起了冲突,而add_arg_scope()就不存在这个问题
def print_hh(add_p):
print(add_p)
add_arg_scope(print_hh)
with arg_scope([print_hh],add_p='pp'):
print_hh('d')
输出:
d
最后,只在代码中看到了这个,表示我的头还是很大
def add_arg_scope(func):
"""Decorates a function with args so it can be used within an arg_scope.
Args:
func: function to decorate.
Returns:
A tuple with the decorated function func_with_args().
"""
def arg_scope(list_ops_or_scope, **kwargs):
"""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.
...
Example of how to use tf.contrib.framework.add_arg_scope to enable your
function to be called within an arg_scope later:
@tf.contrib.framework.add_arg_scope
def conv2d(*args, **kwargs)