使用矩阵 运行 的计算雅可比行列式的自动求导出错

Using autograd of compute Jacobian of a matrix ran into error

有人能告诉我为什么以下计算核矩阵雅可比的代码不起作用吗:

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)

我得到的错误是:

---------------------------------------------------------------------------
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

我已经知道我无法创建一个零矩阵(或单位矩阵)然后用嵌套的 for 循环填充值。因此我创建 np.array 然后连接它们。我使用相同的方法来计算同一内核矩阵的其他一些输出的梯度,它确实有效,所以我不确定为什么它对雅可比矩阵不起作用。

编辑:错误现在应该可以重现

存在数据类型问题。我的代码 top_k_history 的类型为 list,包含 10 个一维数组,每个长度为 10。如果将其转换为形状为 (10, 10) 的 1 个二维数组,则错误应该消失:

# <original code except the last line>

top_k_history = np.array(top_k_history) # new

jac(top_k_history) # original last line