6b4a4f5c435d53d072055942872dd742248c6b1a,tangent/utils.py,,,#,357

Before Change


  return {k: add_grad(left[k], right[k]) for k in left}


grad_adders = {
    (list, list): add_grad_list,
    (dict, dict): add_grad_dict,
    (numpy.ndarray, numpy.ndarray): add_grad_numpy,
    (numpy.ndarray, float): add_grad_numpy,
    (float, numpy.ndarray): add_grad_numpy,
    (numpy.ndarray, numpy.float64): add_grad_numpy,
    (numpy.float64, numpy.ndarray): add_grad_numpy,
    (numpy.ndarray, list): add_grad_numpy,
    (list, numpy.ndarray): add_grad_numpy,
    (bool, bool): lambda left, right: left or right,
}


def register_add_grad(left_type, right_type, add_grad_function):
  Register a new gradient adder supporting the given types.

After Change


// The values are tuples (initializer, allow_lazy_initializer). If
// supports_lazy_initializer is true, Tangent may substitude actual instances
// of the object for the ZeroGradient wrapper, which is a lazy creator.
grad_initializers = {
    // TODO: We may be able to use ZeroGradient for ndarrays, too.
    numpy.ndarray: (numpy.zeros_like, False),
    numpy.float32: (lambda obj: 0.0, False),
    numpy.float64: (lambda obj: 0.0, False),
    list: (lambda obj: [init_grad(el) for el in obj], False),
    tuple: (lambda obj: [init_grad(el) for el in obj], False),
    dict: (lambda obj: {k: init_grad(v) for k, v in six.iteritems(obj)}, False),
    Stack: (lambda obj: Stack(), False),
    float: (lambda obj: 0.0, False),
    int: (lambda obj: 0, False),
    bool: (lambda obj: False, False),
}

if hasattr(types, "ClassType"):
  grad_initializers[types.ClassType] = (init_common_object, False)
else:
  grad_initializers[type] = (init_common_object, False)


class ZeroGradient(object):
  Lightweight substitute for zero gradients.

  This object may be used instead of an actual type when manipulating
Italian Trulli
In pattern: SUPERPATTERN

Frequency: 4

Non-data size: 10

Instances


Project Name: google/tangent
Commit Name: 6b4a4f5c435d53d072055942872dd742248c6b1a
Time: 2017-11-08
Author: mdan@google.com
File Name: tangent/utils.py
Class Name:
Method Name:


Project Name: cornellius-gp/gpytorch
Commit Name: 47a96974ab2d75fc20c76a6d6da20976ecad32bb
Time: 2017-10-02
Author: gpleiss@gmail.com
File Name: gpytorch/random_variables/random_variable.py
Class Name: RandomVariable
Method Name: cuda


Project Name: google/tangent
Commit Name: 088da65939f97dff12f67db313c3601da925b1bf
Time: 2017-11-29
Author: mdan@google.com
File Name: tangent/utils.py
Class Name:
Method Name:


Project Name: cornellius-gp/gpytorch
Commit Name: 47a96974ab2d75fc20c76a6d6da20976ecad32bb
Time: 2017-10-02
Author: gpleiss@gmail.com
File Name: gpytorch/random_variables/random_variable.py
Class Name: RandomVariable
Method Name: cpu