assert_ndim: Assert the Number of Dimensions of a Shape

View source: R/shape.R

assert_ndimR Documentation

Assert the Number of Dimensions of a Shape

Description

Rejects a shape with the wrong number of dimensions. Give either ndim, the number(s) of dimensions the operator accepts, or the bounds min and max (either on its own), which is what an operator that accepts a range of them uses, as batch normalization does.

Usage

assert_ndim(shape, ndim = NULL, id, min = NULL, max = NULL)

Arguments

shape

(integer())
The input shape, with NA for the dimensions whose size is unknown.

ndim

(integer())
The number(s) of dimensions the operator accepts, the batch dimension included. Alternatively give min and/or max.

id

(character(1))
The id of the PipeOp, which the error message names.

min

(integer(1))
The smallest number of dimensions the operator accepts, NULL for no lower bound.

max

(integer(1))
The largest number of dimensions the operator accepts, NULL for no upper bound.

Value

The shape, invisibly.

See Also

Other Shape Assertions: assert_dim_in_range(), assert_known_dims(), assert_not_batch_dim(), assert_positive_extent(), assert_same_batch_size(), assert_same_ndim(), assert_shape(), assert_shapes()

Examples

assert_ndim(c(NA, 3, 32, 32), 4L, id = "nn_conv2d")
try(assert_ndim(c(NA, 3), 4L, id = "nn_conv2d"))
# batch normalization accepts a range
try(assert_ndim(c(NA, 3, 4, 5), id = "nn_batch_norm1d", min = 2L, max = 3L))

mlr3torch documentation built on Oct. 30, 2026, 5:10 p.m.