Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluating a section of a jacobian #582

Open
darcykenworthy opened this issue Sep 12, 2022 · 1 comment
Open

Evaluating a section of a jacobian #582

darcykenworthy opened this issue Sep 12, 2022 · 1 comment

Comments

@darcykenworthy
Copy link

Let's say that I have some function f(x), where x is a vector and returns a vector. I can evaluate the Jacobian of this function fairly simply, as demonstrated below.

from autograd import numpy as np
import autograd as ag
def f(x):
    return np.array([x.sum(),(x[:3]**2).sum(),np.log(np.exp(x).sum())])
xtest=np.array([0,.5,.3,.2])
print(f(xtest))

print(ag.jacobian(f)(xtest))


My question is if there's some way of evaluating only some columns of this jacobian. For example, let's say I only wanted the first and last columns of it. So far I haven't found any way of evaluating this more efficiently than just evaluating the whole jacobian and throwing some away. If anyone can help please let me know!

@pat749
Copy link

pat749 commented Mar 26, 2023

Yes, it is possible to evaluate only certain columns of the Jacobian matrix using Autograd. Autograd provides a jacobian_vector_product function that allows you to compute the product of the Jacobian matrix with a given vector. By setting the input vector to be a standard basis vector, you can obtain the corresponding column of the Jacobian matrix.

from autograd import numpy as np
import autograd as ag

def f(x):
    return np.array([x.sum(), (x[:3]**2).sum(), np.log(np.exp(x).sum())])

xtest = np.array([0, 0.5, 0.3, 0.2])
J = ag.jacobian(f)(xtest)  # Compute the full Jacobian matrix
e1 = np.zeros_like(xtest)
e1[0] = 1  # Set the first component to 1
e3 = np.zeros_like(xtest)
e3[-1] = 1  # Set the last component to 1

col1 = ag.jacobian_vector_product(f, xtest)(e1)  # Compute first column
col3 = ag.jacobian_vector_product(f, xtest)(e3)  # Compute third column

print(col1)
print(col3)

we compute the Jacobian matrix of f with respect to xtest using ag.jacobian(f)(xtest). We then create two vectors e1 and e3, which are the standard basis vectors corresponding to the first and last columns of the Jacobian matrix, respectively. We compute these columns by calling ag.jacobian_vector_product(f, xtest)(e1) and ag.jacobian_vector_product(f, xtest)(e3), respectively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@darcykenworthy @pat749 and others