fix code check r1.3

This commit is contained in:
yanglf1121 2021-07-31 15:34:34 +08:00
parent ce09544be2
commit 93eeddb9d6
4 changed files with 165 additions and 153 deletions

View File

@ -49,6 +49,7 @@ _reduce_min_keepdims = P.ReduceMin(True)
_reduce_max_keepdims = P.ReduceMax(True)
_reduce_mean_keepdims = P.ReduceMean(True)
def array(obj, dtype=None, copy=True, ndmin=0):
"""
Creates a tensor.
@ -2208,17 +2209,14 @@ def _pad_linear(arr, pad_width, end_values):
dtype = arr.dtype
end_values = _convert_pad_to_nd(end_values, ndim)
for i in range(ndim):
# shape [..., 1, ...]
left_value = _slice_along_axis(arr, i, 0, 1)
right_value = _slice_along_axis(arr, i, shape[i]-1, shape[i])
pad_before = ()
pad_after = ()
if pad_width[i][0] > 0:
# shape [..., pad_width[i][0], ...]
pad_before = (linspace(end_values[i][0], left_value, num=pad_width[i][0],
endpoint=False, dtype=dtype, axis=i).squeeze(i+1),)
if pad_width[i][1] > 0:
# shape [..., pad_width[i][1], ...]
pad_after = linspace(right_value, end_values[i][1], num=pad_width[i][1]+1,
endpoint=True, dtype=dtype, axis=i).squeeze(i+1)
pad_after = (_slice_along_axis(pad_after, i, 1, pad_width[i][1]+1),)
@ -2227,6 +2225,58 @@ def _pad_linear(arr, pad_width, end_values):
return arr
def _add_pads_before(arr, pad_args, mode):
"""handle pads before the array"""
idx, array_length, times_to_pad_before, additional_pad_before, reflect_type = pad_args
curr_pad = None
endpoint_adder = None
edge_before = _slice_along_axis(arr, idx, 0, 1)
if mode == "reflect":
endpoint_adder = 1
else:
endpoint_adder = 0
# Deal with paddings before the original array
for times in range(times_to_pad_before):
if times < times_to_pad_before - 1:
endpoint = array_length
else:
endpoint = additional_pad_before + endpoint_adder
if endpoint != endpoint_adder:
curr_pad = _slice_along_axis(arr, idx, endpoint_adder, endpoint)
curr_pad = flip(curr_pad, axis=idx)
if reflect_type == "odd":
curr_pad = 2 * edge_before - curr_pad
arr = P.Concat(idx)((curr_pad, arr))
edge_before = _slice_along_axis(arr, idx, 0, 1)
return arr
def _add_pads_after(arr, pad_args, mode):
"""handle pads after the array"""
idx, array_length, times_to_pad_after, additional_pad_after, reflect_type = pad_args
curr_pad = None
endpoint_adder = None
edge_end = _slice_along_axis(arr, idx, arr.shape[idx]-1, arr.shape[idx])
if mode == "reflect":
endpoint_adder = 1
else:
endpoint_adder = 0
# Deal with paddings after the original array
for times in range(times_to_pad_after):
if times < times_to_pad_after - 1:
startpoint = arr.shape[idx] - array_length
else:
startpoint = arr.shape[idx] - additional_pad_after - endpoint_adder
if startpoint != arr.shape[idx] - endpoint_adder:
curr_pad = _slice_along_axis(arr, idx, startpoint, arr.shape[idx] - endpoint_adder)
curr_pad = flip(curr_pad, axis=idx)
if reflect_type == "odd":
curr_pad = 2 * edge_end - curr_pad
arr = P.Concat(idx)((arr, curr_pad))
edge_end = _slice_along_axis(arr, idx, arr.shape[idx]-1, arr.shape[idx])
return arr
def _pad_symmetric(arr, pad_width, reflect_type):
"""pad the array with symmetric paddings"""
for i in range(arr.ndim):
@ -2235,41 +2285,18 @@ def _pad_symmetric(arr, pad_width, reflect_type):
has_pad_before = (pad_width[i][0] > 0)
has_pad_after = (pad_width[i][1] > 0)
edge_before = _slice_along_axis(arr, i, 0, 1)
edge_end = _slice_along_axis(arr, i, array_length-1, array_length)
times_to_pad_before = pad_width[i][0] // array_length + 1
additional_pad_before = pad_width[i][0] % array_length
times_to_pad_after = pad_width[i][1] // array_length + 1
additional_pad_after = pad_width[i][1] % array_length
curr_pad = None
if has_pad_before:
# Deal with paddings before the original array
for times in range(times_to_pad_before):
if times < times_to_pad_before - 1:
endpoint = array_length
else:
endpoint = additional_pad_before
if endpoint != 0:
curr_pad = _slice_along_axis(arr, i, 0, endpoint)
curr_pad = flip(curr_pad, axis=i)
if reflect_type == "odd":
curr_pad = 2 * edge_before - curr_pad
arr = P.Concat(i)((curr_pad, arr))
edge_before = _slice_along_axis(arr, i, 0, 1)
pad_args = (i, array_length, times_to_pad_before, additional_pad_before, reflect_type)
arr = _add_pads_before(arr, pad_args, "symmetric")
if has_pad_after:
# Deal with paddings after the original array
for times in range(times_to_pad_after):
if times < times_to_pad_after - 1:
startpoint = arr.shape[i] - array_length
else:
startpoint = arr.shape[i] - additional_pad_after
if startpoint != arr.shape[i]:
curr_pad = _slice_along_axis(arr, i, startpoint, arr.shape[i])
curr_pad = flip(curr_pad, axis=i)
if reflect_type == "odd":
curr_pad = 2 * edge_end - curr_pad
arr = P.Concat(i)((arr, curr_pad))
edge_end = _slice_along_axis(arr, i, arr.shape[i]-1, arr.shape[i])
pad_args = (i, array_length, times_to_pad_after, additional_pad_after, reflect_type)
arr = _add_pads_after(arr, pad_args, "symmetric")
return arr
@ -2278,7 +2305,6 @@ def _pad_reflect(arr, pad_width, reflect_type):
pad the array with reflect paddings, this is very similar to symmetric paddings,
but differs at how edges are selected.
"""
# pylint: disable=too-many-nested-blocks
for i in range(arr.ndim):
array_length = arr.shape[i]
if array_length == 1:
@ -2288,42 +2314,19 @@ def _pad_reflect(arr, pad_width, reflect_type):
has_pad_before = (pad_width[i][0] > 0)
has_pad_after = (pad_width[i][1] > 0)
edge_before = _slice_along_axis(arr, i, 0, 1)
edge_end = _slice_along_axis(arr, i, array_length-1, array_length)
pad_size = array_length - 1
times_to_pad_before = pad_width[i][0] // pad_size + 1
additional_pad_before = pad_width[i][0] % pad_size
times_to_pad_after = pad_width[i][1] // pad_size + 1
additional_pad_after = pad_width[i][1] % pad_size
curr_pad = None
if has_pad_before:
# Deal with paddings before the original array
for times in range(times_to_pad_before):
if times < times_to_pad_before - 1:
endpoint = array_length
else:
endpoint = additional_pad_before + 1
if endpoint != 1:
curr_pad = _slice_along_axis(arr, i, 1, endpoint)
curr_pad = flip(curr_pad, axis=i)
if reflect_type == "odd":
curr_pad = 2 * edge_before - curr_pad
arr = P.Concat(i)((curr_pad, arr))
edge_before = _slice_along_axis(arr, i, 0, 1)
pad_args = (i, array_length, times_to_pad_before, additional_pad_before, reflect_type)
arr = _add_pads_before(arr, pad_args, "reflect")
if has_pad_after:
# Deal with paddings after the original array
for times in range(times_to_pad_after):
if times < times_to_pad_after - 1:
startpoint = arr.shape[i] - array_length
else:
startpoint = arr.shape[i] - additional_pad_after - 1
if startpoint != arr.shape[i]-1:
curr_pad = _slice_along_axis(arr, i, startpoint, arr.shape[i]-1)
curr_pad = flip(curr_pad, axis=i)
if reflect_type == "odd":
curr_pad = 2 * edge_end - curr_pad
arr = P.Concat(i)((arr, curr_pad))
edge_end = _slice_along_axis(arr, i, arr.shape[i]-1, arr.shape[i])
pad_args = (i, array_length, times_to_pad_after, additional_pad_after, reflect_type)
arr = _add_pads_after(arr, pad_args, "reflect")
return arr
@ -2476,7 +2479,7 @@ def pad(arr, pad_width, mode="constant", stat_length=None, constant_values=0,
constant_values = _convert_pad_to_nd(constant_values, arr.ndim)
return _pad_constant(arr, pad_width, constant_values)
if mode in ("maximum", "minimum", "mean", "median"):
# TODO: support median mode once P.Sort/P.Median is supported on GPU/CPU
# support median mode once P.Sort/P.Median is supported on GPU/CPU
if mode == "median":
_raise_unimplemented_error("median mode is not supported yet")
return _pad_statistic(arr, pad_width, stat_length, stat_func[mode])

View File

@ -773,12 +773,12 @@ def atleast_1d(*arys):
>>> output = np.atleast_1d(a, b, c)
>>> print(output)
[Tensor(shape=[2, 3], dtype=Float32, value=
[[1.00000000e+000, 1.00000000e+000, 1.00000000e+000],
[1.00000000e+000, 1.00000000e+000, 1.00000000e+000]]),
Tensor(shape=[1], dtype=Float32, value= [1.00000000e+000]),
[[1.00000000e+00, 1.00000000e+00, 1.00000000e+00],
[1.00000000e+00, 1.00000000e+00, 1.00000000e+00]]),
Tensor(shape=[1], dtype=Float32, value= [1.00000000e+00]),
Tensor(shape=[5], dtype=Float32,
value= [1.00000000e+000, 1.00000000e+000, 1.00000000e+000,
1.00000000e+000, 1.00000000e+000])]
value= [1.00000000e+00, 1.00000000e+00, 1.00000000e+00,
1.00000000e+00, 1.00000000e+00])]
"""
return _atleast_xd(1, arys)
@ -810,12 +810,12 @@ def atleast_2d(*arys):
>>> output = np.atleast_2d(a, b, c)
>>> print(output)
[Tensor(shape=[2, 3], dtype=Float32, value=
[[1.00000000e+000, 1.00000000e+000, 1.00000000e+000],
[1.00000000e+000, 1.00000000e+000, 1.00000000e+000]]),
Tensor(shape=[1, 1], dtype=Float32, value= [[1.00000000e+000]]),
[[1.00000000e+00, 1.00000000e+00, 1.00000000e+00],
[1.00000000e+00, 1.00000000e+00, 1.00000000e+00]]),
Tensor(shape=[1, 1], dtype=Float32, value= [[1.00000000e+00]]),
Tensor(shape=[1, 5], dtype=Float32,
value= [[1.00000000e+000, 1.00000000e+000, 1.00000000e+000,
1.00000000e+000, 1.00000000e+000]])]
value= [[1.00000000e+00, 1.00000000e+00, 1.00000000e+00,
1.00000000e+00, 1.00000000e+00]])]
"""
return _atleast_xd(2, arys)
@ -850,12 +850,12 @@ def atleast_3d(*arys):
>>> output = np.atleast_3d(a, b, c)
>>> print(output)
[Tensor(shape=[2, 3, 1], dtype=Float32, value=
[[[1.00000000e+000], [1.00000000e+000], [1.00000000e+000]],
[[1.00000000e+000], [1.00000000e+000], [1.00000000e+000]]]),
Tensor(shape=[1, 1, 1], dtype=Float32, value= [[[1.00000000e+000]]]),
[[[1.00000000e+00], [1.00000000e+00], [1.00000000e+00]],
[[1.00000000e+00], [1.00000000e+00], [1.00000000e+00]]]),
Tensor(shape=[1, 1, 1], dtype=Float32, value= [[[1.00000000e+00]]]),
Tensor(shape=[1, 5, 1], dtype=Float32,
value= [[[1.00000000e+000], [1.00000000e+000], [1.00000000e+000],
[1.00000000e+000], [1.00000000e+000]]])]
value= [[[1.00000000e+00], [1.00000000e+00], [1.00000000e+00],
[1.00000000e+00], [1.00000000e+00]]])]
"""
res = []
for arr in arys:
@ -1444,6 +1444,7 @@ def _split(x, indices_or_sections, opname, axis=0):
should be integer, tuple(int) or list(int), but got", indices_or_sections)
return res
@constexpr
def convert_neg_indices(indices, ndim):
"""converts negative values in tuple/list indices"""
@ -1452,6 +1453,7 @@ def convert_neg_indices(indices, ndim):
indices = tuple([canonicalizer(axis) for axis in indices])
return indices
def _split_sub_tensors(x, indices, axis):
"""
Splits the input tensor `x` into multiple sub-tensors

View File

@ -2234,7 +2234,7 @@ def convolve(a, v, mode='full'):
a, v = v, a
a_size, v_size = v_size, a_size
v = v[::-1]
return _compute_1D_conv(a, v, mode).astype(final_dtype)
return _compute_1d_conv(a, v, mode).astype(final_dtype)
def _handle_weights(weights, num_samples):
@ -3923,6 +3923,23 @@ def _gradient_along_axis(f, h, axis):
return a_grad / h
def check_gradient_arguments(f, axis, edge_order):
"""check arguments for gradient"""
if edge_order != 1:
_raise_unimplemented_error("edge_order != 1 not implemented")
if not isinstance(f, Tensor):
f = asarray_const(f)
if f.dtype != mstype.float64:
f = f.astype(mstype.float32)
if axis is None:
axis = F.make_range(f.ndim)
else:
_check_axis_type(axis, True, True, True)
axis = _canonicalize_axis(axis, f.ndim)
axis = (axis,) if isinstance(axis, int) else axis
return f, axis, edge_order
def gradient(f, *varargs, axis=None, edge_order=1):
"""
Returns the gradient of a N-dimensional array.
@ -3969,18 +3986,7 @@ def gradient(f, *varargs, axis=None, edge_order=1):
[1. 1. 1. ]]
"""
# This implementation was adapted from Numpy and jax.numpy
if edge_order != 1:
_raise_unimplemented_error("edge_order != 1 not implemented")
if not isinstance(f, Tensor):
f = asarray_const(f)
if f.dtype != mstype.float64:
f = f.astype(mstype.float32)
if axis is None:
axis = F.make_range(f.ndim)
else:
_check_axis_type(axis, True, True, True)
axis = _canonicalize_axis(axis, f.ndim)
axis = (axis,) if isinstance(axis, int) else axis
f, axis, edge_order = check_gradient_arguments(f, axis, edge_order)
len_axes = len(axis)
n = len(varargs)
@ -4370,7 +4376,7 @@ def interp(x, xp, fp, left=None, right=None):
>>> print(np.interp(3.14, xp, fp, right=UNDEF))
-99.0
"""
# TODO implement period once sort is supported
# implement period once sort is supported
x, xp, fp = _to_tensor(x, xp, fp)
if F.rank(xp) != 1 or F.rank(fp) != 1:
_raise_value_error('xp and fp must be 1-d sequences')
@ -4378,7 +4384,6 @@ def interp(x, xp, fp, left=None, right=None):
if fp.size != size:
_raise_value_error('the y-coordinates must have the same length as `xp`')
shape = F.shape(x)
xp = xp.astype(mstype.float32)
fp = fp.astype(mstype.float32)
@ -4392,20 +4397,17 @@ def interp(x, xp, fp, left=None, right=None):
y_1 = F.gather_nd(fp, indices_1)
res = (y_0*(x_1 - x) + y_1*(x - x_0))/(x_1 - x_0)
res = F.select(F.equal(x_0, x_1), y_0, res)
# where x < xp[0], y = left or xp[0]
# where x > xp[-1], y = right or xp[-1]
idx_0 = _to_tensor([0])
idx_last = _to_tensor([size - 1])
if left is None:
left = F.gather_nd(fp, idx_0)
left = full(shape, left, mstype.float32)
left = full(F.shape(x), left, mstype.float32)
if right is None:
right = F.gather_nd(fp, idx_last)
right = full(shape, right, mstype.float32)
choose_left = F.tensor_lt(x, F.gather_nd(xp, idx_0))
choose_right = F.tensor_gt(x, F.gather_nd(xp, idx_last))
res = F.select(choose_left, left, res)
res = F.select(choose_right, right, res)
right = full(F.shape(x), right, mstype.float32)
res = F.select(F.tensor_lt(x, F.gather_nd(xp, idx_0)), left, res)
res = F.select(F.tensor_gt(x, F.gather_nd(xp, idx_last)), right, res)
return res
@ -4723,6 +4725,31 @@ def _factor_flattened_hist(nbin):
return factor
def _get_histogramdd_count(ndim, bin_edges, sample, weights):
"""Returns count for histogramdd."""
data_indices = []
nbin = ()
flattened_bin_size = 1
for i in F.make_range(ndim):
data_to_bins = searchsorted(bin_edges[i], sample[:, i], 'right')
bin_size = _type_convert(int, bin_edges[i].size)
data_to_bins = where_(sample[:, i] == bin_edges[i][-1], _to_tensor(bin_size - 1), data_to_bins)
data_indices.append(data_to_bins)
nbin += (bin_size + 1,)
flattened_bin_size *= (bin_size + 1)
factor = F.reshape(_to_tensor(_factor_flattened_hist(nbin)), (ndim, 1))
stacked_indices = stack(data_indices) * factor
if _get_device() == 'Ascend':
stacked_indices = F.cast(stacked_indices, mstype.float32)
flattened_hist = F.reduce_sum(stacked_indices.astype(mstype.float32), 0)
count = bincount(flattened_hist.astype(mstype.int32), weights, length=flattened_bin_size)
count = F.reshape(count, nbin)
slices = _list_comprehensions(ndim, F.make_slice(1, -1, 1), True)
count = count[slices]
return count
def histogramdd(sample, bins=10, range=None, weights=None, density=False): # pylint: disable=redefined-builtin
"""
Computes the multidimensional histogram of some data.
@ -4823,26 +4850,7 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False): # pyl
bin_edges.append(edges)
dedges.append(diff(edges))
data_indices = []
nbin = ()
flattened_bin_size = 1
for i in F.make_range(ndim):
data_to_bins = searchsorted(bin_edges[i], sample[:, i], 'right')
bin_size = _type_convert(int, bin_edges[i].size)
data_to_bins = where_(sample[:, i] == bin_edges[i][-1], _to_tensor(bin_size - 1), data_to_bins)
data_indices.append(data_to_bins)
nbin += (bin_size + 1,)
flattened_bin_size *= (bin_size + 1)
factor = F.reshape(_to_tensor(_factor_flattened_hist(nbin)), (ndim, 1))
stacked_indices = stack(data_indices) * factor
if _get_device() == 'Ascend':
stacked_indices = F.cast(stacked_indices, mstype.float32)
flattened_hist = F.reduce_sum(stacked_indices.astype(mstype.float32), 0)
count = bincount(flattened_hist.astype(mstype.int32), weights, length=flattened_bin_size)
count = F.reshape(count, nbin)
slices = _list_comprehensions(ndim, F.make_slice(1, -1, 1), True)
count = count[slices]
count = _get_histogramdd_count(ndim, bin_edges, sample, weights)
if density:
s = F.reduce_sum(count.astype(mstype.float32))
@ -5079,7 +5087,7 @@ def polysub(a1, a2):
>>> print(np.polysub([2, 10, -2], [3, 10, -4]))
[-1 0 2]
"""
return polyadd(a1, -_to_tensor(a2))
return polyadd(a1, F.neg_tensor(_to_tensor(a2)))
def polyval(p, x):
@ -5485,51 +5493,48 @@ def ravel_multi_index(multi_index, dims, mode='clip', order='C'):
return sum_((multi_index * strides).astype('float32'), axis=0)
def _vector_norm(x, ord, axis, keepdims): # pylint: disable=redefined-builtin
def _vector_norm(x, _ord, axis, keepdims):
"""Returns norm of a vector."""
if _in(ord, ('fro', 'nuc')):
if _in(_ord, ('fro', 'nuc')):
_raise_value_error('Frobenius norm and nuclear norm are only defined for vectors')
if ord is None:
ord = 2
if ord == inf:
if _ord is None:
_ord = 2
if _ord == inf:
res = P.ReduceMax(keepdims)(absolute(x), axis)
elif ord == -inf:
elif _ord == -inf:
res = P.ReduceMin(keepdims)(absolute(x), axis)
elif ord == 0:
elif _ord == 0:
res = P.ReduceSum(keepdims)(F.not_equal(x, 0).astype(mstype.float32), axis)
else:
res = power(P.ReduceSum(keepdims)(power(absolute(x), ord), axis), 1./ord)
res = power(P.ReduceSum(keepdims)(power(absolute(x), _ord), axis), 1./_ord)
return res
def _matrix_norm(x, ord, axis, keepdims): # pylint: disable=redefined-builtin
def _matrix_norm(x, _ord, axis, keepdims):
"""Returns norm of a matrix."""
if ord == 0:
if _ord == 0:
_raise_value_error('for 0 axis, norm is defined only for 2-D matrices')
if ord == 'nuc':
if _ord == 'nuc':
_raise_unimplemented_error('nuclear norm is not implemented')
if _in(ord, (2, -2)):
if _in(_ord, (2, -2)):
_raise_unimplemented_error('2-norm is not implemented for matrices')
if _in(ord, (None, 'fro')):
res = F.sqrt(P.ReduceSum(keepdims)(F.square(x), axis))
else:
axis0, axis1 = axis
if not keepdims:
if _check_is_inf(_abs(ord)) and axis0 > axis1:
axis0 -= 1
elif _abs(ord) == 1 and axis1 > axis0:
axis1 -= 1
if _check_is_inf(ord):
res = P.ReduceMax(keepdims)(P.ReduceSum(keepdims)(absolute(x), axis1), axis0)
elif _check_is_inf(ord, True):
res = P.ReduceMin(keepdims)(P.ReduceSum(keepdims)(absolute(x), axis1), axis0)
elif ord == 1:
res = P.ReduceMax(keepdims)(P.ReduceSum(keepdims)(absolute(x), axis0), axis1)
elif ord == -1:
res = P.ReduceMin(keepdims)(P.ReduceSum(keepdims)(absolute(x), axis0), axis1)
else:
return _raise_value_error('invalid norm order for matrices')
return res
if _in(_ord, (None, 'fro')):
return F.sqrt(P.ReduceSum(keepdims)(F.square(x), axis))
axis0, axis1 = axis
if not keepdims:
if _check_is_inf(_abs(_ord)) and axis0 > axis1:
axis0 -= 1
elif _abs(_ord) == 1 and axis1 > axis0:
axis1 -= 1
if _check_is_inf(_ord):
return P.ReduceMax(keepdims)(P.ReduceSum(keepdims)(absolute(x), axis1), axis0)
if _check_is_inf(_ord, True):
return P.ReduceMin(keepdims)(P.ReduceSum(keepdims)(absolute(x), axis1), axis0)
if _ord == 1:
return P.ReduceMax(keepdims)(P.ReduceSum(keepdims)(absolute(x), axis0), axis1)
if _ord == -1:
return P.ReduceMin(keepdims)(P.ReduceSum(keepdims)(absolute(x), axis0), axis1)
return _raise_value_error('invalid norm order for matrices')
def norm(x, ord=None, axis=None, keepdims=False): # pylint: disable=redefined-builtin
@ -5827,11 +5832,11 @@ def correlate(a, v, mode='valid'):
v = v.astype(promote_dtype)
if a.size < v.size:
a, v = v, a
return _compute_1D_conv(a, v, mode)[::-1]
return _compute_1D_conv(a, v, mode)
return _compute_1d_conv(a, v, mode)[::-1]
return _compute_1d_conv(a, v, mode)
def _compute_1D_conv(a, v, mode):
def _compute_1d_conv(a, v, mode):
"""Returns a 1-D sequence which is the cross-correlate of two 1-D sequences (`a` and `v`)."""
v_size = F.shape_mul(v.shape)
if mode not in ('same', 'full', 'valid'):

View File

@ -136,6 +136,8 @@ def _can_broadcast(*shapes):
_infer_out_shape(*shapes)
except ValueError:
return False
finally:
pass
return True