Helpers for writing the private$.shapes_out() method of a PipeOpTorch. They all propagate
unknown (NA) dimensions, see the "Shape Inference" section of PipeOpTorch for the shape conventions.
Arguments
- x
(
integer()|list()ofinteger()|NULL)
The shape(s) to format.NULLstands for an unknown shape.- shapes
(
list()ofinteger())
The input shapes, all with the same number of dimensions.- id
(
character(1))
The id of thePipeOp, which the error message names.- dim
(
integer())
The dimension(s) the operator addresses. Negative ones count back from the last dimension:-1is the last dimension,-2the one before it, and so on.- shape
(
integer())
The input shape.- insert
(
logical(1))
Whether a dimension is inserted rather than addressed, as bynn_unsqueeze(): there is then one more position than the shape has dimensions,-1appending a new last one.
Value
broadcast_shapes() returns an integer() shape, resolve_dim() an integer() of the same
length as dim, and shape_to_str() a character(1).
Details
broadcast_shapes()applies the broadcasting rules oftorch, generalized to shapes that may containNA. Per dimension a known size that is not 1 wins; if all known sizes are 1 and some input is unknown, the result is unknown, because the unknown one may be greater than 1 and would then determine the size. The shapes must already have the same number of dimensions: shorter ones are not left-padded with 1s, because the first dimension is the batch dimension.resolve_dim()resolves dimension indices that count from the end, as intorch, to positive ones. Indices that are out of range stay out of range, so thatassert_dim_in_range()reports them.shape_to_str()formats a shape, or alist()of them, for an error message.
See also
Other Shape Inference:
infer_shapes(),
reshape_output_shape()