def _find_penultimate_layer(model, layer_idx, penultimate_layer_idx):
if penultimate_layer_idx is None:<EOL><INDENT>for idx, layer in utils.reverse_enumerate(model.layers[:layer_idx - <NUM_LIT:1>]):<EOL><INDENT>if isinstance(layer, Wrapper):<EOL><INDENT>layer = layer.layer<EOL><DEDENT>if isinstance(layer, (_Conv, _Pooling1D, _Pooling2D, _Pooling3D)):<EOL><INDENT>penultimate_layer_idx = idx<EOL>break<EOL><DEDENT><DEDENT><DEDENT>if penultimate_layer_idx is None:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'.format(layer_idx))<EOL><DEDENT>if layer_idx < <NUM_LIT:0>:<EOL><INDENT>layer_idx = len(model.layers) + layer_idx<EOL><DEDENT>if penultimate_layer_idx > layer_idx:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return model.layers[penultimate_layer_idx]<EOL>
Searches for the nearest penultimate `Conv` or `Pooling` layer.

    Args:
        model: The `keras.models.Model` instance.
        layer_idx: The layer index within `model.layers`.
        penultimate_layer_idx: The pre-layer to `layer_idx`. If set to None, the nearest penultimate
            `Conv` or `Pooling` layer is used.

    Returns:
        The penultimate layer.