n should be a positive integer or None
Package:
tensorflow
158813

Exception Class:
ValueError
Raise code
def _validate_dct_arguments(input_tensor, dct_type, n, axis, norm):
"""Checks that DCT/IDCT arguments are compatible and well formed."""
if axis != -1:
raise NotImplementedError("axis must be -1. Got: %s" % axis)
if n is not None and n < 1:
raise ValueError("n should be a positive integer or None")
if dct_type not in (1, 2, 3, 4):
raise ValueError("Types I, II, III and IV (I)DCT are supported.")
if dct_type == 1:
if norm == "ortho":
raise ValueError("Normalization is not supported for the Type-I DCT.")
if input_tensor.shape[-1] is not None and input_tensor.shape[-1] < 2:
raise ValueError(
Links to the raise (1)
https://github.com/tensorflow/tensorflow/blob/7acd515ec218b414d5b16e6710268ac03d9f5421/tensorflow/python/ops/signal/dct_ops.py#L37Ways to fix
tf.signal.dct computes the 1D [Discrete Cosine Transform (DCT)][dct] of input.
Usage:
result = tf.signal.dct(input,
type=2,
n=None,
axis=-1,
norm=None,
name=None)
The parameter n here is the length of the transform and it should be a positive integer. If a negative value is given it causes an error.
Reproducing the error:
pipenv install tensorflow
from tensorflow.python.ops.signal import dct_ops
signals = np.random.rand(4,3)
n = -3 # this value cause the error
tf_dct = dct_ops.dct(signals, n=n,norm="ortho")
print(tf_dct)
The error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-27-13c3895d48f9> in <module>
3 signals = np.random.rand(4,3)
4 n = -3
----> 5 tf_dct = dct_ops.dct(signals, n=n,norm="ortho")
~/my_env_project/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
204 """Call target, and fall back on dispatchers if there is a TypeError."""
205 try:
--> 206 return target(*args, **kwargs)
207 except (TypeError, ValueError):
208 # Note: convert_to_eager_tensor currently raises a ValueError, not a
~/my_env_project/lib/python3.8/site-packages/tensorflow/python/ops/signal/dct_ops.py in dct(input, type, n, axis, norm, name)
97 [dct]: https://en.wikipedia.org/wiki/Discrete_cosine_transform
98 """
---> 99 _validate_dct_arguments(input, type, n, axis, norm)
100 with _ops.name_scope(name, "dct", [input]):
101 input = _ops.convert_to_tensor(input)
~/my_env_project/lib/python3.8/site-packages/tensorflow/python/ops/signal/dct_ops.py in _validate_dct_arguments(input_tensor, dct_type, n, axis, norm)
35 raise NotImplementedError("axis must be -1. Got: %s" % axis)
36 if n is not None and n < 1:
---> 37 raise ValueError("n should be a positive integer or None")
38 if dct_type not in (1, 2, 3, 4):
39 raise ValueError("Types I, II, III and IV (I)DCT are supported.")
ValueError: n should be a positive integer or None
Fixed version of the code:
from tensorflow.python.ops.signal import dct_ops
signals = np.random.rand(4,3)
n = 3
tf_dct = dct_ops.dct(signals, n=n,norm="ortho")
print(tf_dct)
Output:
tf.Tensor( [[ 0.70799171 -0.32111126 0.04285833] [ 1.21643631 -0.44179349 -0.26358989] [ 0.77752386 0.3508207 0.2477764 ] [ 0.75556746 -0.17665882 0.10850445]], shape=(4, 3), dtype=float64)
Add a possible fix
Please authorize to post fix