-
Notifications
You must be signed in to change notification settings - Fork 844
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
46 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# -*- coding: utf-8 -*- | ||
#!/usr/bin/env python | ||
|
||
import re | ||
from tensorpack.utils import logger | ||
from tensorpack.tfutils.gradproc import GradientProcessor | ||
|
||
|
||
class FilterGradientVariables(GradientProcessor): | ||
""" | ||
Skip the update of certain variables and print a warning. | ||
""" | ||
|
||
def __init__(self, var_regex='.*', verbose=True): | ||
""" | ||
Args: | ||
var_regex (string): regular expression to match variable to update. | ||
verbose (bool): whether to print warning about None gradients. | ||
""" | ||
super(FilterGradientVariables, self).__init__() | ||
self._regex = var_regex | ||
self._verbose = verbose | ||
|
||
def _process(self, grads): | ||
g = [] | ||
to_print = [] | ||
for grad, var in grads: | ||
if re.match(self._regex, var.op.name): | ||
g.append((grad, var)) | ||
else: | ||
to_print.append(var.op.name) | ||
if self._verbose and len(to_print): | ||
message = ', '.join(to_print) | ||
logger.warn("No gradient w.r.t these trainable variables: {}".format(message)) | ||
return g |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters