class_docstr: Apply a function on all gradient if the name matches regex.
  Keep the other gradients unchanged.
  
  It can be used for gradient clipping, etc.
class_docstr_tok: ["Apply","a","function","on","all","gradient","if","the","name","matches","regex",".","Keep","the","other","gradients","unchanged",".","It","can","be","used","for","gradient","clipping",",","etc","."]
docstr: Args:
      func: a user-supplied function which takes one or two arguments.
          The argument(s) can be either a `grad` tensor, or `grad` and `var`.
          The function should return the new gradient to be used.
          If it return None, the gradient is discarded (hence no update to the variable will happen).
      regex (str): used to match variables. Defaults to match all variables.
docstr_tok: ["Args",":","func",":","a","user-supplied","function","which","takes","one","or","two","arguments",".","The","argument","(","s",")","can","be","either","a","`","grad","`","tensor",",","or","`","grad","`","and","`","var","`",".","The","function","should","return","the","new","gradient","to","be","used",".","If","it","return","None",",","the","gradient","is","discarded","(","hence","no","update","to","the","variable","will","happen",")",".","regex","(","str",")",":","used","to","match","variables",".","Defaults","to","match","all","variables","."]
code: def __init__(self, func, regex=".*"):
      """
      Args:
          func: a user-supplied function which takes one or two arguments.
              The argument(s) can be either a `grad` tensor, or `grad` and `var`.
              The function should return the new gradient to be used.
              If it return None, the gradient is discarded (hence no update to the variable will happen).
          regex (str): used to match variables. Defaults to match all variables.
      """
      args = inspect.getfullargspec(func).args
      arg_num = len(args) - inspect.ismethod(func)
      assert arg_num in [
          1,
          2,
      ], "The function must take 1 or 2 arguments!  ({})".format(args)
      if arg_num == 1:
          self.func = lambda grad, var: func(grad)
      else:
          self.func = func
  
      if not regex.endswith("$"):
          regex = regex + "$"
      self.regex = regex
      super(MapGradient, self).__init__()
  
imports: import inspect
  import re
  from abc import ABCMeta, abstractmethod
  import six
  import tensorflow
  from ..compat import tfv1
  from ..utils import logger
  from .summary import add_moving_summary
  from .symbolic_functions import print_stat, rms
label_desc: Use image node_id 1 to create a new MapGradient object from inherited base classes: GradientProcessor with example: obj = MapGradient(func, regex)