Skip to content

_feed_forward

feed_forward

Run a forward step on a given model.

This method can be used with TensorFlow models:

m = fe.architecture.tensorflow.LeNet(classes=2)
x = tf.ones((3,28,28,1))  # (batch, height, width, channels)
b = fe.backend.feed_forward(m, x)  # [[~0.5, ~0.5], [~0.5, ~0.5], [~0.5, ~0.5]]

This method can be used with PyTorch models:

m = fe.architecture.pytorch.LeNet(classes=2)
x = torch.ones((3,1,28,28))  # (batch, channels, height, width)
b = fe.backend.feed_forward(m, x)  # [[~0.5, ~0.5], [~0.5, ~0.5], [~0.5, ~0.5]]

Parameters:

Name Type Description Default
model Union[Model, Module]

A neural network to run the forward step through.

required
x Union[Tensor, ndarray]

One or more input tensor for the model. This value will be auto-cast to either a tf.Tensor or torch.Tensor as applicable for the model.

()
training bool

Whether this forward step is part of training or not. This may impact the behavior of model layers such as dropout.

True

Returns:

Type Description
Tensor

The result of model(x).

Raises:

Type Description
ValueError

If model is an unacceptable data type.

Source code in fastestimator/fastestimator/backend/_feed_forward.py
def feed_forward(model: Union[tf.keras.Model, torch.nn.Module], *x: Union[Tensor, np.ndarray],
                 training: bool = True) -> Tensor:
    """Run a forward step on a given model.

    This method can be used with TensorFlow models:
    ```python
    m = fe.architecture.tensorflow.LeNet(classes=2)
    x = tf.ones((3,28,28,1))  # (batch, height, width, channels)
    b = fe.backend.feed_forward(m, x)  # [[~0.5, ~0.5], [~0.5, ~0.5], [~0.5, ~0.5]]
    ```

    This method can be used with PyTorch models:
    ```python
    m = fe.architecture.pytorch.LeNet(classes=2)
    x = torch.ones((3,1,28,28))  # (batch, channels, height, width)
    b = fe.backend.feed_forward(m, x)  # [[~0.5, ~0.5], [~0.5, ~0.5], [~0.5, ~0.5]]
    ```

    Args:
        model: A neural network to run the forward step through.
        x: One or more input tensor for the `model`. This value will be auto-cast to either a tf.Tensor or torch.Tensor
            as applicable for the `model`.
        training: Whether this forward step is part of training or not. This may impact the behavior of `model` layers
            such as dropout.

    Returns:
        The result of `model(x)`.

    Raises:
        ValueError: If `model` is an unacceptable data type.
    """
    if isinstance(model, tf.keras.Model):
        x = to_tensor(x, "tf")
        x = model(*x, training=training)
    elif isinstance(model, torch.nn.Module):
        model.train(mode=training)
        x = to_tensor(x, "torch")
        x = model(*x)
    else:
        raise ValueError("Unrecognized model instance {}".format(type(model)))
    return x