def memoize_autodiff(func):
 cache = {}
 @wraps(func)
 def memoizer(op_tree, be, next_error=None):
  "\n        If params in the caches, return results directly. Othewise, add to cache\n        and return the results.\n\n        Arguments:\n            op_tree (OpTreeNode): the op-tree to supply to the func.\n            be (Backend): computation backend to supply to the func.\n            next_error (Tensor or OpTreeNode, optional): next layer's error to\n                                                         supply to the func.\n        "
  key = (op_tree.key(), be, next_error)
  if (key not in cache):
   cache[key] = func(op_tree, be, next_error)
  return cache[key]
 return memoizer

def memoize_autodiff ( func ) : cache = { } @ wraps ( func ) def memoizer ( op_tree , be , next_error = None ) : key = ( op_tree . key ( ) , be , next_error ) if ( key not in cache ) : cache [ key ] = func ( op_tree , be , next_error ) return cache [ key ] return memoizer
memoize to avoid rebuilding of the gradient tree .