#NEWLINE# def subgraph_grad( wrt , end , start = None , cost = None , details = False ) : #NEWLINE# #INDENT# "\n    With respect to `wrt`, computes gradients of cost and/or from\n    existing `start` gradients, up to the `end` variables of a\n    symbolic digraph.  In other words, computes gradients for a\n    subgraph of the symbolic theano function. Ignores all disconnected\n    inputs.\n\n    This can be useful when one needs to perform the gradient descent\n    iteratively (e.g. one layer at a time in an MLP), or when a\n    particular operation is not differentiable in theano\n    (e.g. stochastic sampling from a multinomial). In the latter case,\n    the gradient of the non-differentiable process could be\n    approximated by user-defined formula, which could be calculated\n    using the gradients of a cost with respect to samples (0s and\n    1s). These gradients are obtained by performing a subgraph_grad\n    from the `cost` or previously known gradients (`start`) up to the\n    outputs of the stochastic process (`end`).  A dictionary mapping\n    gradients obtained from the user-defined differentiation of the\n    process, to variables, could then be fed into another\n    subgraph_grad as `start` with any other `cost` (e.g. weight\n    decay).\n\n    In an MLP, we could use subgraph_grad to iteratively backpropagate:\n\n    .. code-block:: python\n\n        x, t = theano.tensor.fvector('x'), theano.tensor.fvector('t')\n        w1 = theano.shared(np.random.randn(3,4))\n        w2 = theano.shared(np.random.randn(4,2))\n        a1 = theano.tensor.tanh(theano.tensor.dot(x,w1))\n        a2 = theano.tensor.tanh(theano.tensor.dot(a1,w2))\n        cost2 = theano.tensor.sqr(a2 - t).sum()\n        cost2 += theano.tensor.sqr(w2.sum())\n        cost1 = theano.tensor.sqr(w1.sum())\n\n        params = [[w2],[w1]]\n        costs = [cost2,cost1]\n        grad_ends = [[a1], [x]]\n\n        next_grad = None\n        param_grads = []\n        for i in xrange(2):\n            param_grad, next_grad = theano.subgraph_grad(\n                wrt=params[i], end=grad_ends[i],\n                start=next_grad, cost=costs[i]\n            )\n            next_grad = dict(zip(grad_ends[i], next_grad))\n            param_grads.extend(param_grad)\n\n    :type wrt: list of variables\n    :param wrt:\n      Gradients are computed with respect to `wrt`.\n\n    :type end: list of variables\n    :param end:\n      Theano variables at which to end gradient descent (they are\n      considered constant in theano.grad).  For convenience, the\n      gradients with respect to these variables are also returned.\n\n    :type start: dictionary of variables\n    :param start:\n      If not None, a dictionary mapping variables to their\n      gradients. This is useful when the gradient on some variables\n      are known. These are used to compute the gradients backwards up\n      to the variables in `end` (they are used as known_grad in\n      theano.grad).\n\n    :type cost: scalar (0-dimensional) variable\n    :param cost:\n      Additional costs for which to compute the gradients.  For\n      example, these could be weight decay, an l1 constraint, MSE,\n      NLL, etc. May optionally be None if start is provided.  Warning\n      : If the gradients of `cost` with respect to any of the `start`\n      variables is already part of the `start` dictionary, then it may\n      be counted twice with respect to `wrt` and `end`.\n\n      .. warning::\n\n        If the gradients of `cost` with respect to any of the `start`\n        variables is already part of the `start` dictionary, then it\n        may be counted twice with respect to `wrt` and `end`.\n\n\n    :type details: bool\n    :param details:\n      When True, additionally returns the list of gradients from\n      `start` and of `cost`, respectively, with respect to `wrt` (not\n      `end`).\n\n    :rtype: Tuple of 2 or 4 Lists of Variables\n\n    :return: Returns lists of gradients with respect to `wrt` and `end`,\n            respectively.\n\n    .. versionadded:: 0.7\n    " #NEWLINE# assert ( ( cost is not None ) or ( start is not None ) ) #NEWLINE# assert isinstance ( end , list ) #NEWLINE# assert isinstance ( wrt , list ) #NEWLINE# if ( start is not None ) : #NEWLINE# #INDENT# assert isinstance ( start , dict ) #NEWLINE# #UNINDENT# params = list ( set ( ( wrt + cost ) ) ) #NEWLINE# start_grads = None #NEWLINE# cost_grads = None #NEWLINE# if ( start is not None ) : #NEWLINE# #INDENT# start_grads = list ( theano . grad ( cost = None , wrt = params , known_grads = start , consider_constant = end , disconnected_inputs = 'ignore' ) ) #NEWLINE# #UNINDENT# if ( cost is not None ) : #NEWLINE# #INDENT# cost_grads = list ( theano . grad ( cost = cost , wrt = params , consider_constant = end , disconnected_inputs = 'ignore' ) ) #NEWLINE# #UNINDENT# grads = None #NEWLINE# if ( start is None ) : #NEWLINE# #INDENT# grads = cost_grads #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# grads = start_grads #NEWLINE# if ( cost_grads is not None ) : #NEWLINE# #INDENT# for i in range ( len ( grads ) ) : #NEWLINE# #INDENT# grads [ i ] += cost_grads [ i ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# pgrads = OrderedDict ( izip ( params , grads ) ) #NEWLINE# wrt_grads = list ( ( pgrads [ k ] for k in wrt ) ) #NEWLINE# end_grads = list ( ( pgrads [ k ] for k in end ) ) #NEWLINE# if details : #NEWLINE# #INDENT# return ( wrt_grads , end_grads , start_grads , cost_grads ) #NEWLINE# #UNINDENT# return ( wrt_grads , end_grads )