def numgrad(func: Callable, a: np.ndarray, delta: float = 1e-6) -> np.ndarray:
    "Numerical gradient of func(a) at `a`."
    grad = np.zeros(a.shape, a.dtype)
    for index, _ in np.ndenumerate(grad):
        delta_array = np.zeros(a.shape, a.dtype)
        delta_array[index] = delta / 2
        grad[index] = np.sum(
            (func(a + delta_array) - func(a - delta_array)) / delta)
    return grad
Beispiel #2
0
 def multiply_by_locgrad(path_value):
     result = np.zeros(flatshape)
     result[np.arange(result.shape[0]), idx] = 1
     swapped_shape = list(a.shape)
     swapped_shape[axis], swapped_shape[-1] = swapped_shape[
         -1], swapped_shape[axis]
     result = result.reshape(swapped_shape)
     result = np.swapaxes(result, axis, -1)
     return path_value * result
Beispiel #3
0
def onehot(y: np.ndarray, n_classes: int) -> np.array:
    "Onehot encode vector y with classes 0 to n_classes-1."
    result = np.zeros([len(y), n_classes])
    result[np.arange(len(y)), y] = 1
    return result
Beispiel #4
0
 def multiply_by_locgrad_b(path_value):
     return np.where(
         condition,
         np.zeros(path_value.shape, a.array.dtype),
         path_value,
     )
Beispiel #5
0
 def multiply_by_locgrad(path_value):
     result = np.zeros(a.array.shape, a.array.dtype)
     if axis:  # Expand dims so they can be broadcast.
         path_value = np.expand_dims(path_value, axis)
     return result + path_value
Beispiel #6
0
 def multiply_by_locgrad(path_value):  # TODO: a faster method
     result = np.zeros(a.shape, a.dtype)
     np_add_at(np_strided_sliding_view(result, window_shape, strides), None,
               path_value)
     return result
Beispiel #7
0
 def multiply_by_locgrad(path_value):
     "(Takes into account elements indexed multiple times.)"
     result = np.zeros(a.array.shape, a.array.dtype)
     np_add_at(result, indices, path_value)
     return result
Beispiel #8
0
 def multiply_by_locgrad_b(path_value):
     path_value = np.sum(path_value,
                         axis=b_repeatdims).reshape(b.array.shape)
     return np.zeros(b.array.shape, b.array.dtype) + path_value