Home > Net >  Using autograd of compute Jacobian of a matrix ran into error
Using autograd of compute Jacobian of a matrix ran into error

Time:03-02

Can someone enlighten me why the following code to compute the jacobian of kernel matrix doesn't work:

import autograd.numpy as np
# import numpy as np
from autograd import grad 
from autograd import jacobian 
from numpy import linalg as LA

def kernel(x1,x2,l):
    return np.exp(-((x1-x2)**2).sum()/(2*(l**2)))

def kernel_matrixx(top_k_history):
    k_t_X_list = []

    for i in range(k-1):
    #         print(kernel(top_k_history[i],observation,l))
        k_t_X_list.append(np.expand_dims(np.expand_dims((kernel(top_k_history[0],top_k_history[i 1],l)), axis=0), axis=0))
    #     print(k_t_X_list[0].item())
    #     k_t_X = np.expand_dims(np.asarray(k_t_X_list), axis=0)

    k_t_X = np.expand_dims(np.expand_dims((kernel(top_k_history[0],top_k_history[0],l)), axis=0), axis=0)

    for i in range(k-1):
    #         temp =  np.expand_dims(np.expand_dims(np.asarray(kernel(observation,top_k_history[i 1],l)), axis=0), axis=0)
        k_t_X = np.concatenate([k_t_X, k_t_X_list[i]], axis=1)

    k_t_X_first = k_t_X

    k_t_X_list_list = []
    for j in range(k-1):

        k_t_X_list = []

        for i in range(k-1):
        #         print(kernel(top_k_history[i],observation,l))
            k_t_X_list.append(np.expand_dims(np.expand_dims((kernel(top_k_history[j 1],top_k_history[i 1],l)), axis=0), axis=0))
        #     print(k_t_X_list[0].item())
        #     k_t_X = np.expand_dims(np.asarray(k_t_X_list), axis=0)

        k_t_X = np.expand_dims(np.expand_dims((kernel(top_k_history[j 1],top_k_history[0],l)), axis=0), axis=0)

        for i in range(k-1):
        #         temp =  np.expand_dims(np.expand_dims(np.asarray(kernel(observation,top_k_history[i 1],l)), axis=0), axis=0)
            k_t_X = np.concatenate([k_t_X, k_t_X_list[i]], axis=1)

        k_t_X_list_list.append(k_t_X)

    for i in range(k-1): 
        k_t_X_first = np.concatenate([k_t_X_first, k_t_X_list_list[i]], axis=0)
        
    return k_t_X_first

k=10
l=19
top_k_history = []
for i in range(10):
    top_k_history.append(np.random.rand(10))
jac = jacobian(kernel_matrixx)
jac(top_k_history)

the error I got is:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_15016/2419460232.py in <module>
      1 jac = jacobian(kernel_matrixx)
----> 2 jac(top_k_history)

~\Anaconda3\envs\unlearning\lib\site-packages\autograd\wrap_util.py in nary_f(*args, **kwargs)
     18             else:
     19                 x = tuple(args[i] for i in argnum)
---> 20             return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
     21         return nary_f
     22     return nary_operator

~\Anaconda3\envs\unlearning\lib\site-packages\autograd\differential_operators.py in jacobian(fun, x)
     57     vjp, ans = _make_vjp(fun, x)
     58     ans_vspace = vspace(ans)
---> 59     jacobian_shape = ans_vspace.shape   vspace(x).shape
     60     grads = map(vjp, ans_vspace.standard_basis())
     61     return np.reshape(np.stack(grads), jacobian_shape)

TypeError: can only concatenate tuple (not "list") to tuple

I am already aware that I cannot create a zero matrix (or identity matrix) then fill in the value with nested for loop. Therefore I create np.array and then concat them. I use the same approach for compute the grad of some other output of the same kernel matrix and it did work so I'm not sure why it didn't work for Jacobian.

Edit: the error now should be reproducible

CodePudding user response:

There is a datatype problem. I your code top_k_history is of type list and contains 10 1D-arrays, each of length 10. If you convert this into 1 2D-array of shape (10, 10), then the error should vanish:

# <original code except the last line>

top_k_history = np.array(top_k_history) # new

jac(top_k_history) # original last line
  • Related