keras-team/keras · error · ValueError
Argument `n` should be a positive integer. Received: n={n}
Error message
Argument `n` should be a positive integer. Received: n={n} What it means
RepeatVector.__init__ rejects n <= 0 after the int check: repeating a vector zero or a negative number of times would produce an empty or invalid axis, so Keras fails fast at construction time.
Source
Thrown at keras/src/layers/reshaping/repeat_vector.py:35
Args:
n: Integer, repetition factor.
Input shape:
2D tensor with shape `(batch_size, features)`.
Output shape:
3D tensor with shape `(batch_size, n, features)`.
"""
def __init__(self, n, **kwargs):
super().__init__(**kwargs)
if not isinstance(n, int) or isinstance(n, bool):
raise TypeError(
f"Expected an integer value for `n`, got {type(n)}."
)
if n <= 0:
raise ValueError(
f"Argument `n` should be a positive integer. Received: n={n}"
)
self.n = n
self.input_spec = InputSpec(ndim=2)
def compute_output_shape(self, input_shape):
return (input_shape[0], self.n, input_shape[1])
def call(self, inputs):
input_shape = ops.shape(inputs)
reshaped = ops.reshape(inputs, (input_shape[0], 1, input_shape[1]))
return ops.repeat(reshaped, self.n, axis=1)
def get_config(self):
config = {"n": self.n}
base_config = super().get_config()
return {**base_config, **config}
View on GitHub (pinned to 7a34a03db6)
Solutions
- Ensure the value feeding n is >= 1; add a max(1, ...) guard only if a 1-repeat is acceptable semantics
- Trace where n comes from — usually an upstream calculation returning 0 (empty list length, division rounding down)
- Validate configs at load time before layer construction
Example fix
# before n = len(seq) - 1 # 0 when len(seq)==1 layer = RepeatVector(n=n) # after n = max(len(seq), 1) layer = RepeatVector(n=n)
Defensive patterns
Strategy: validation
Validate before calling
def validated_n(n):
if not (isinstance(n, int) and not isinstance(n, bool)):
raise TypeError('n must be int')
if n <= 0:
raise ValueError(f'n must be >= 1, got {n}')
return n Type guard
def is_positive_int(v) -> bool:
return isinstance(v, int) and not isinstance(v, bool) and v > 0 Prevention
- Guard hyperparameter-search values with max(1, ...) when semantics allow
- Check upstream length/ratio computations that can evaluate to 0
- Fail fast on config values before model build
When it happens
Trigger: RepeatVector(n=0) or RepeatVector(n=-3). Typically the value comes from a computation or config that accidentally evaluates to zero or negative.
Common situations: Hyperparameter search proposing 0; deriving n from a length or batch-size expression that can be 0 for empty inputs; default/placeholder config values that were never replaced.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Expected an integer value for `n`, got {type(n)}.
- Argument `size` should be a positive integer. Received: size
- Unknown activation function '{activation}' cannot be seriali
- Could not interpret activation function identifier: {identif
- ConvNeXt does not support the `channels_first` image data fo
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/397b511bb1ad7c0f.
Report an issue: GitHub.