使用计算矩阵的雅可比阶的自梯度时出现了误差



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

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)
~AppDataLocalTemp/ipykernel_15016/2419460232.py in <module>
1 jac = jacobian(kernel_matrixx)
----> 2 jac(top_k_history)
~Anaconda3envsunlearninglibsite-packagesautogradwrap_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
~Anaconda3envsunlearninglibsite-packagesautograddifferential_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个1D数组,每个数组的长度为10。如果您将其转换为形状为(10, 10)的2D阵列,则错误应该消失:

# <original code except the last line>
top_k_history = np.array(top_k_history) # new
jac(top_k_history) # original last line

最新更新