mindspore.numpy.gradient
- mindspore.numpy.gradient(f, *varargs, axis=None, edge_order=1)[source]
Returns the gradient of a N-dimensional array. The gradient is computed using second order accurate central differences in the interior points and either first or second order accurate one-sides (forward or backwards) differences at the boundaries. The returned gradient hence has the same shape as the input array.
Note
Currently we only support edge_order =1 and uniform spacing of varargs.
- Parameters
f (Union[tuple, list, Tensor]) – An N-dimensional array containing samples of a scalar function.
varargs (Union[tuple[number], tuple[tensor scalar]], optional) – Spacing between f values. Default unitary spacing for all dimensions. Spacing can be specified using: 1. single scalar to specify a sample distance for all dimensions. 2. N scalars to specify a constant sample distance for each dimension.
axis (Union[None, int, tuple(int), list(int)], optional) – Gradient is calculated only along the given axis or axes. The default
(axis = None)
is to calculate the gradient for all the axes of the input tensor. axis may be negative, in which case it counts from the last to the first axis.edge_order (int, optional) – Gradient is calculated using N-th order accurate differences at the boundaries. Default:
1
.
- Returns
gradient, a list of tensors (or a single tensor if there is only one dimension to be calculated). Each derivative has the same shape as f.
- Raises
TypeError – If the inputs have types not specified above.
ValueError – If axis values out of bounds, or shape of f has entries < 1.
NotImplementedError – If edge_order != 1, or varargs contains non-scalar entries.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> output = np.gradient([[1, 2, 6], [3, 4, 5]], axis=-1) >>> print(output) [[1. 2.5 4. ] [1. 1. 1. ]]