Autograd 中断 np.empty_like

Autograd breaks np.empty_like

我正在尝试获取一个函数的梯度,在该函数中我单独分配 numpy 数组元素(在 FEA 中将局部力分配给全局力向量),但这似乎会破坏 Autograd——如果我使用 np.zeros 对于全局数组我得到 ValueError: setting an array element with a sequence,而如果我使用 np.empty 我得到 NotImplementedError: VJP of empty_like wrt argnums (0,) not defined.

示例:

import autograd.numpy as np
from autograd import jacobian, grad

def test(input):
    a = np.empty_like(input)
    a[:] = input[:]

grad(test)(np.array([0.]))

报错:

C:\Miniconda3\python.exe C:/Users/JoshuaF/Desktop/gripper/softDrone/bug_test.py
Traceback (most recent call last):
  File "C:\Miniconda3\lib\site-packages\autograd\core.py", line 31, in __init__
    vjpmaker = primitive_vjps[fun]
KeyError: <function primitive.<locals>.f_wrapped at 0x000001AB1D0AA8C8>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/JoshuaF/Desktop/gripper/softDrone/bug_test.py", line 8, in <module>
    grad(test)(np.array([0.]))
  File "C:\Miniconda3\lib\site-packages\autograd\wrap_util.py", line 20, in nary_f
    return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
  File "C:\Miniconda3\lib\site-packages\autograd\differential_operators.py", line 25, in grad
    vjp, ans = _make_vjp(fun, x)
  File "C:\Miniconda3\lib\site-packages\autograd\core.py", line 10, in make_vjp
    end_value, end_node =  trace(start_node, fun, x)
  File "C:\Miniconda3\lib\site-packages\autograd\tracer.py", line 10, in trace
    end_box = fun(start_box)
  File "C:\Miniconda3\lib\site-packages\autograd\wrap_util.py", line 15, in unary_f
    return fun(*subargs, **kwargs)
  File "C:/Users/JoshuaF/Desktop/gripper/softDrone/bug_test.py", line 5, in test
    a = np.empty_like(input)
  File "C:\Miniconda3\lib\site-packages\autograd\tracer.py", line 45, in f_wrapped
    node = node_constructor(ans, f_wrapped, argvals, kwargs, argnums, parents)
  File "C:\Miniconda3\lib\site-packages\autograd\core.py", line 35, in __init__
    .format(fun_name, parent_argnums))
NotImplementedError: VJP of empty_like wrt argnums (0,) not defined

有什么方法可以在按元素组装的 numpy 数组上使用 Autograd 吗?

根据教程 https://github.com/HIPS/autograd/blob/master/docs/tutorial.md,很遗憾,autograd 函数似乎不支持数组赋值。