0

I understand from the autograd tutorial that array assignment is not supported when the array is contained in the objective to be differentiated. However, I currently have the following objective function in my code which I would like to differentiate with respect to theta:

def obj(theta):
    """
    Computes the objective function to be differentiated.

    Args:
        theta: np.array of shape (n, d)

    Return:
        res: np.array of shape (n,)
    """
    theta = np.atleast_2d(theta)
    n = theta.shape[0]

    res = np.zeros(n)  # Scores
    for i in xrange(n):
        res[i] = ... # Do some computations with theta[i, :]

    return res

Typically I can avoid the for loop by vectorizing the computation over theta; however, in this case the computation already involves various linear algebra operations (inverses, etc.) given a specific row of theta (as hyperparameters), and I found it quite difficult to vectorize the operation over all rows of theta. In this case, I don't know of a better way than filling the res array row by row with a for-loop.

I have tried a naive way to avoid array assignments by creating a list and at each iteration append the results to that list, then finally convert the list to the array upon returning res, but I get all-zero gradients in the end...

I wonder what is the general recommend solution in this setup?

4

1 に答える 1