Skip to content

sometimes

Sometimes

Bases: NumpyOp

Perform a NumpyOp with a given probability.

Parameters:

Name Type Description Default
numpy_op NumpyOp

The operator to be performed.

required
prob float

The probability of execution, which should be in the range: [0-1).

0.5
Source code in fastestimator\fastestimator\op\numpyop\meta\sometimes.py
class Sometimes(NumpyOp):
    """Perform a NumpyOp with a given probability.

    Args:
        numpy_op: The operator to be performed.
        prob: The probability of execution, which should be in the range: [0-1).
    """
    def __init__(self, numpy_op: NumpyOp, prob: float = 0.5) -> None:
        super().__init__(inputs=numpy_op.inputs, outputs=numpy_op.outputs, mode=numpy_op.mode)
        self.numpy_op = numpy_op
        self.prob = prob

    def forward(self, data: Union[np.ndarray, List[np.ndarray]],
                state: Dict[str, Any]) -> Union[np.ndarray, List[np.ndarray]]:
        """Execute the wrapped operator a certain fraction of the time.

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

        Returns:
            The original `data`, or the `data` after running it through the wrapped operator.
        """
        if self.prob > np.random.uniform():
            data = self.numpy_op.forward(data, state)
        return data

forward

Execute the wrapped operator a certain fraction of the time.

Parameters:

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

The information to be passed to the wrapped operator.

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 original data, or the data after running it through the wrapped operator.

Source code in fastestimator\fastestimator\op\numpyop\meta\sometimes.py
def forward(self, data: Union[np.ndarray, List[np.ndarray]],
            state: Dict[str, Any]) -> Union[np.ndarray, List[np.ndarray]]:
    """Execute the wrapped operator a certain fraction of the time.

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

    Returns:
        The original `data`, or the `data` after running it through the wrapped operator.
    """
    if self.prob > np.random.uniform():
        data = self.numpy_op.forward(data, state)
    return data