0

我正在尝试采用函数的梯度,在该函数中我单独分配 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?

4

1 回答 1

0

根据教程https://github.com/HIPS/autograd/blob/master/docs/tutorial.md,不幸的是,autograd 函数不支持数组分配。

于 2019-11-07T00:05:23.300 回答