Skip to content

one_of

OneOf

Bases: NumpyOp

Perform one of several possible NumpyOps.

Parameters:

Name Type Description Default
numpy_ops NumpyOp

A list of ops to choose between with uniform probability.

()
Source code in fastestimator\fastestimator\op\numpyop\meta\one_of.py
class OneOf(NumpyOp):
    """Perform one of several possible NumpyOps.

    Args:
        numpy_ops: A list of ops to choose between with uniform probability.
    """
    def __init__(self, *numpy_ops: NumpyOp) -> None:
        inputs = numpy_ops[0].inputs
        outputs = numpy_ops[0].outputs
        mode = numpy_ops[0].mode
        for op in numpy_ops[1:]:
            assert inputs == op.inputs, "All ops within a OneOf must share the same inputs"
            assert outputs == op.outputs, "All ops within a OneOf must share the same outputs"
            assert mode == op.mode, "All ops within a OneOf must share the same mode"
        super().__init__(inputs=inputs, outputs=outputs, mode=mode)
        self.numpy_ops = numpy_ops

    def forward(self, data: Union[np.ndarray, List[np.ndarray]],
                state: Dict[str, Any]) -> Union[np.ndarray, List[np.ndarray]]:
        """Execute a randomly selected op from the list of `numpy_ops`.

        Args:
            data: The information to be passed to one of the wrapped operators.
            state: Information about the current execution context, for example {"mode": "train"}.

        Returns:
            The `data` after application of one of the available numpyOps.
        """
        return random.choice(self.numpy_ops).forward(data, state)

forward

Execute a randomly selected op from the list of numpy_ops.

Parameters:

Name Type Description Default
data Union[np.ndarray, List[np.ndarray]]

The information to be passed to one of the wrapped operators.

required
state Dict[str, Any]

Information about the current execution context, for example {"mode": "train"}.

required

Returns:

Type Description
Union[np.ndarray, List[np.ndarray]]

The data after application of one of the available numpyOps.

Source code in fastestimator\fastestimator\op\numpyop\meta\one_of.py
def forward(self, data: Union[np.ndarray, List[np.ndarray]],
            state: Dict[str, Any]) -> Union[np.ndarray, List[np.ndarray]]:
    """Execute a randomly selected op from the list of `numpy_ops`.

    Args:
        data: The information to be passed to one of the wrapped operators.
        state: Information about the current execution context, for example {"mode": "train"}.

    Returns:
        The `data` after application of one of the available numpyOps.
    """
    return random.choice(self.numpy_ops).forward(data, state)