Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
seminar-breakout
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shashank Suhas
seminar-breakout
Commits
4f4794df
Commit
4f4794df
authored
Aug 17, 2017
by
Yuxin Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use inspect.signature instead of getcallargs. also improve argscope with this
parent
9850edf5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
8 deletions
+22
-8
tensorpack/models/common.py
tensorpack/models/common.py
+15
-6
tensorpack/utils/argtools.py
tensorpack/utils/argtools.py
+7
-2
No files found.
tensorpack/models/common.py
View file @
4f4794df
...
...
@@ -3,6 +3,7 @@
# Author: Yuxin Wu <ppwwyyxx@gmail.com>
import
tensorflow
as
tf
import
inspect
from
functools
import
wraps
import
six
import
re
...
...
@@ -14,7 +15,7 @@ from ..utils import logger
# make sure each layer is only logged once
_LAYER_LOGGED
=
set
()
_LAYER_REGIST
ERED
=
{}
_LAYER_REGIST
RY
=
{}
__all__
=
[
'layer_register'
]
...
...
@@ -53,11 +54,11 @@ class VariableHolder(object):
def
_register
(
name
,
func
):
if
name
in
_LAYER_REGIST
ERED
:
if
name
in
_LAYER_REGIST
RY
:
raise
ValueError
(
"Layer named {} is already registered!"
.
format
(
name
))
if
name
in
[
'tf'
]:
raise
ValueError
(
logger
.
error
(
"A layer cannot be named {}"
.
format
(
name
)))
_LAYER_REGIST
ERED
[
name
]
=
func
_LAYER_REGIST
RY
[
name
]
=
func
def
get_registered_layer
(
name
):
...
...
@@ -67,7 +68,7 @@ def get_registered_layer(name):
Returns:
the wrapped layer function, or None if not registered.
"""
return
_LAYER_REGIST
ERED
.
get
(
name
,
None
)
return
_LAYER_REGIST
RY
.
get
(
name
,
None
)
def
disable_layer_logging
():
...
...
@@ -124,10 +125,18 @@ def layer_register(
isinstance
(
inputs
[
0
],
(
tf
.
Tensor
,
tf
.
Variable
)))):
raise
ValueError
(
"Invalid inputs to layer: "
+
str
(
inputs
))
# TODO use inspect.getcallargs to enhance?
# update from current argument scope
# use kwargs from current argument scope
actual_args
=
copy
.
copy
(
get_arg_scope
()[
func
.
__name__
])
# explicit kwargs overwrite argscope
actual_args
.
update
(
kwargs
)
# explicit positional args also override argscope
if
six
.
PY2
:
posargmap
=
inspect
.
getcallargs
(
func
,
*
args
)
else
:
posargmap
=
inspect
.
signature
(
func
)
.
bind_partial
(
*
args
)
.
arguments
for
k
in
six
.
iterkeys
(
posargmap
):
if
k
in
actual_args
:
del
actual_args
[
k
]
if
name
is
not
None
:
# use scope
with
tf
.
variable_scope
(
name
)
as
scope
:
...
...
tensorpack/utils/argtools.py
View file @
4f4794df
...
...
@@ -20,12 +20,17 @@ def map_arg(**maps):
Apply a mapping on certains argument before calling the original function.
Args:
maps (dict): {
key
: map_func}
maps (dict): {
argument_name
: map_func}
"""
def
deco
(
func
):
@
functools
.
wraps
(
func
)
def
wrapper
(
*
args
,
**
kwargs
):
argmap
=
inspect
.
getcallargs
(
func
,
*
args
,
**
kwargs
)
if
six
.
PY2
:
argmap
=
inspect
.
getcallargs
(
func
,
*
args
,
**
kwargs
)
else
:
# getcallargs was deprecated since 3.5
sig
=
inspect
.
signature
(
func
)
argmap
=
sig
.
bind_partial
(
*
args
,
**
kwargs
)
.
arguments
for
k
,
map_func
in
six
.
iteritems
(
maps
):
if
k
in
argmap
:
argmap
[
k
]
=
map_func
(
argmap
[
k
])
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment