def grad(cost, wrt, consider_constant=None,
         disconnected_inputs='raise', add_names=True,
         known_grads=None, return_disconnected='zero',
         null_gradients='raise'):
    t0 = time.time()
    global tensor
    if tensor is None:
        from theano import tensor

    if cost is None:
        if known_grads is None:
            raise AssertionError("cost and known_grads can't both be None.")

    if cost is not None and isinstance(cost.type, NullType):
        raise ValueError("Can't differentiate a NaN cost."
                         "cost is NaN because " +
                         cost.type.why_null)

    if cost is not None and cost.ndim != 0:
        raise TypeError("cost must be a scalar.")

    if isinstance(wrt, set):
        raise TypeError("wrt must not be a set. sets have no defined "
                        "iteration order, so we can't return gradients in a"
                        "  matching order.")

    using_list = isinstance(wrt, list)
    using_tuple = isinstance(wrt, tuple)
    if not using_list and not using_tuple:
        wrt = [wrt]

    for elem in wrt:
        if not isinstance(elem, Variable):
            raise TypeError("Expected Variable, got " + str(elem) +
                            " of type " + str(type(elem)))

    outputs = []
    if cost is not None:
        outputs.append(cost)
    if known_grads is not None:
        outputs.extend(list(known_grads.keys()))

    var_to_app_to_idx = _populate_var_to_app_to_idx(
        outputs, wrt, consider_constant)

    # build a dict mapping var to the gradient of cost with respect to var
    grad_dict = OrderedDict()

    if known_grads is None:
        known_grads = OrderedDict()

    # The gradient of the cost is 1 unless specified otherwise by known_grads.
    if cost is not None:
        if cost in known_grads:
            g_cost = known_grads[cost]
        else:
            g_cost = _float_ones_like(cost)
        # g_cost may be Disconnected or NullType. A creative use of the
        # function, sure, but nonetheless one we can and should support.
        # So before we try to cast it make sure it even has a dtype
        if (hasattr(g_cost.type, 'dtype') and
                cost.type.dtype not in tensor.discrete_dtypes):
                # Here we enforce the constraint that floating point variables
                # have the same dtype as their gradient.
                g_cost = g_cost.astype(cost.type.dtype)
        # DO NOT enforce g_cost to be 0 if cost is an integer.
        # This is to be enforced by the Op.grad method for the
        # Op that outputs cost.
        if hasattr(g_cost.type, 'dtype'):
            assert g_cost.type.dtype not in tensor.discrete_dtypes

        grad_dict[cost] = g_cost

    for var in known_grads:
        g_var = known_grads[var]

        if not hasattr(g_var, 'type'):
            raise TypeError('output grads must be theano variables.'
                            'Ambiguous whether %s should be made into tensor'
                            ' or sparse theano variable' % str(type(g_var)))

        if (not isinstance(g_var.type, (NullType, DisconnectedType)) and
                'float' not in str(g_var.type.dtype)):
            raise TypeError("Gradients must always be NullType, "
                            "DisconnectedType, or continuous, but grad was "
                            "given a known_grad of type " + str(g_var.type))

        # DO NOT check that these gradients are equal to 0 if var is int
        # The gradient is allowed to be non-zero on var in that case
        # Ops outputing var should not backpropagate its gradient further
        # but that is enforced elsewhere (grep for only_connected_to_int)

        grad_dict[var] = g_var

    def handle_disconnected(var):
            message = ("grad method was asked to compute the gradient "
                       "with respect to a variable that is not part of "
                       "the computational graph of the cost, or is used "
                       "only by a non-differentiable operator: %s" % var)
            if disconnected_inputs == 'ignore':
                pass
            elif disconnected_inputs == 'warn':
                warnings.warn(message, stacklevel=2)
            elif disconnected_inputs == 'raise':
                # Add the var trace
                tr = getattr(var.tag, 'trace', [])
                if len(tr) > 0:
                    message += "\nBacktrace when the node is created:\n"

                    # Print separate message for each element in the list of batcktraces
                    sio = StringIO()
                    for subtr in tr:
                        traceback.print_list(subtr, sio)
                    message += str(sio.getvalue())

                raise DisconnectedInputError(message)
            else:
                raise ValueError("Invalid value for keyword "
                                 "'disconnected_inputs', valid values are "
                                 "'ignore', 'warn' and 'raise'.")

    # variables that do not influence the cost have zero gradient.
    # if wrt is such a variable, populate the grad_dict with this info
    # so that wrt not being in var_to_app_to_idx won't cause an error below
    # according to the flag, possibly raise an error if wrt is disconnected
    for elem in wrt:
        if elem not in var_to_app_to_idx and elem is not cost \
                and elem not in grad_dict:
            handle_disconnected(elem)
            grad_dict[elem] = disconnected_type()

    cost_name = None
    if add_names and cost is not None:
        cost_name = cost.name

    # Make sure we didn't initialize the grad_dict with any ints
    # The gradient may NEVER be an int, even if the variable is an int.
    # Read the Op contract and talk to Ian Goodfellow before changing this!
    for var in grad_dict:
        g = grad_dict[var]
        if hasattr(g.type, 'dtype'):
            assert g.type.dtype in tensor.float_dtypes

    rval = _populate_grad_dict(var_to_app_to_idx,
                               grad_dict, wrt, cost_name)

    for i in xrange(len(rval)):
        if isinstance(rval[i].type, NullType):
            if null_gradients == 'raise':
                raise NullTypeGradError("tensor.grad encountered a NaN. " +
                                        rval[i].type.why_null)
            else:
                assert null_gradients == 'return'
        if isinstance(rval[i].type, DisconnectedType):
            handle_disconnected(rval[i])
            if return_disconnected == 'zero':
                rval[i] = _float_zeros_like(wrt[i])
            elif return_disconnected == 'None':
                rval[i] = None
            else:
                assert return_disconnected == 'Disconnected'

    if using_tuple:
        rval = tuple(rval)
    elif not using_list:
        rval, = rval
    t1 = time.time()
    global grad_time
    grad_time += t1 - t0
    return rval
Get the zeroing image location
dataset/ETHPy150Open rizar/attention-lvcsr/libs/Theano/theano/gradient.py/grad--crossover with--dataset/ETHPy150Open ganeti/ganeti/lib/config/__init__.py/ConfigWriter.GetZeroingImage