Skip to content

grid_compression_layer

Bases: layer

A layer for compressing grid-based inputs using pooling metrics and patch-based aggregation.

This layer supports operations to reduce the dimensionality of grid-based inputs by applying pooling metrics and packing strategies.

Parameters:

Name Type Description Default
h int

The height of the input grid.

required
w int

The width of the input grid.

required
channel_num int

The number of input channels.

required
d int

The depth of the input grid. Default is 1 (2D grid).

1
name str

The name of the layer. Default is 'grid_compression_layer'.

'grid_compression_layer'
pooling_metric str

The metric used for pooling ('batch_max', 'batch_min', etc.). Default is 'batch_max'.

'batch_max'
patch_shape str

The shape of the patch ('cuboid', 'cylinder', or 'sphere'). Default is 'cuboid'.

'cuboid'
p_h int

The height of the patch.

None
p_h_prime int

The height of the inner patch (for hierarchical packing).

None
p_w int

The width of the patch.

None
p_w_prime int

The width of the inner patch (for hierarchical packing).

None
p_d int

The depth of the patch (only for 3D grids).

0
p_d_prime int

The depth of the inner patch (for hierarchical packing).

None
p_r int

The radius of the patch (for spherical or cylindrical patches).

None
cd_h int

The compression degree along the height dimension.

None
cd_w int

The compression degree along the width dimension.

None
cd_d int

The compression degree along the depth dimension. Default is 1.

1
with_dropout bool

If True, applies dropout. Default is False.

False
p float

The dropout probability. Default is 0.5.

0.5
packing_strategy str

The packing strategy ('densest_packing' or other supported modes). Default is 'densest_packing'.

'densest_packing'
parameters_init_method str

The initialization method for parameters. Default is 'xavier_normal'.

'xavier_normal'
device str

The device for computations ('cpu' or 'cuda'). Default is 'cpu'.

'cpu'
*args

Additional arguments passed to the parent class.

()
**kwargs

Additional arguments passed to the parent class.

()

Methods:

Name Description
get_output_grid_shape

Returns the shape of the output grid after packing.

Raises:

Type Description
ValueError

If invalid patch shape, dimensions, or configuration is provided.

Source code in tinybig/layer/grid_based_layers.py
class grid_compression_layer(layer):
    """
    A layer for compressing grid-based inputs using pooling metrics and patch-based aggregation.

    This layer supports operations to reduce the dimensionality of grid-based inputs by applying pooling metrics
    and packing strategies.

    Parameters
    ----------
    h : int
        The height of the input grid.
    w : int
        The width of the input grid.
    channel_num : int
        The number of input channels.
    d : int, optional
        The depth of the input grid. Default is 1 (2D grid).
    name : str, optional
        The name of the layer. Default is 'grid_compression_layer'.
    pooling_metric : str, optional
        The metric used for pooling ('batch_max', 'batch_min', etc.). Default is 'batch_max'.
    patch_shape : str, optional
        The shape of the patch ('cuboid', 'cylinder', or 'sphere'). Default is 'cuboid'.
    p_h : int, optional
        The height of the patch.
    p_h_prime : int, optional
        The height of the inner patch (for hierarchical packing).
    p_w : int, optional
        The width of the patch.
    p_w_prime : int, optional
        The width of the inner patch (for hierarchical packing).
    p_d : int, optional
        The depth of the patch (only for 3D grids).
    p_d_prime : int, optional
        The depth of the inner patch (for hierarchical packing).
    p_r : int, optional
        The radius of the patch (for spherical or cylindrical patches).
    cd_h : int, optional
        The compression degree along the height dimension.
    cd_w : int, optional
        The compression degree along the width dimension.
    cd_d : int, optional
        The compression degree along the depth dimension. Default is 1.
    with_dropout : bool, optional
        If True, applies dropout. Default is False.
    p : float, optional
        The dropout probability. Default is 0.5.
    packing_strategy : str, optional
        The packing strategy ('densest_packing' or other supported modes). Default is 'densest_packing'.
    parameters_init_method : str, optional
        The initialization method for parameters. Default is 'xavier_normal'.
    device : str, optional
        The device for computations ('cpu' or 'cuda'). Default is 'cpu'.
    *args, **kwargs
        Additional arguments passed to the parent class.

    Methods
    -------
    get_output_grid_shape():
        Returns the shape of the output grid after packing.

    Raises
    ------
    ValueError
        If invalid patch shape, dimensions, or configuration is provided.
    """
    def __init__(
        self,
        h: int, w: int, channel_num: int,
        d: int = 1,
        name: str = 'grid_compression_layer',
        pooling_metric: str = 'batch_max',
        patch_shape: str = 'cuboid',
        p_h: int = None, p_h_prime: int = None,
        p_w: int = None, p_w_prime: int = None,
        p_d: int = 0, p_d_prime: int = None,
        p_r: int = None,
        cd_h: int = None, cd_w: int = None, cd_d: int = 1,
        with_dropout: bool = False, p: float = 0.5,
        packing_strategy: str = 'densest_packing',
        # other parameters
        parameters_init_method: str = 'xavier_normal',
        device: str = 'cpu', *args, **kwargs
    ):
        """
        Initialize the grid_compression_layer.

        This layer compresses grid inputs by applying pooling metrics and packing strategies for dimensionality reduction.

        Parameters
        ----------
        h : int
            The height of the input grid.
        w : int
            The width of the input grid.
        channel_num : int
            The number of input channels.
        d : int, optional
            The depth of the input grid. Default is 1 (for 2D grids).
        name : str, optional
            The name of the layer. Default is 'grid_compression_layer'.
        pooling_metric : str, optional
            The metric used for pooling (e.g., 'batch_max', 'batch_min', 'batch_avg'). Default is 'batch_max'.
        patch_shape : str, optional
            The shape of the patch ('cuboid', 'cylinder', or 'sphere'). Default is 'cuboid'.
        p_h : int, optional
            The height of the patch.
        p_h_prime : int, optional
            The height of the inner patch (used for hierarchical packing).
        p_w : int, optional
            The width of the patch.
        p_w_prime : int, optional
            The width of the inner patch (used for hierarchical packing).
        p_d : int, optional
            The depth of the patch.
        p_d_prime : int, optional
            The depth of the inner patch (used for hierarchical packing).
        p_r : int, optional
            The radius of the patch (used for spherical or cylindrical patches).
        cd_h : int, optional
            The compression degree along the height dimension.
        cd_w : int, optional
            The compression degree along the width dimension.
        cd_d : int, optional
            The compression degree along the depth dimension. Default is 1.
        with_dropout : bool, optional
            If True, applies dropout after the compression step. Default is False.
        p : float, optional
            The dropout probability. Default is 0.5.
        packing_strategy : str, optional
            The packing strategy used ('densest_packing' or other supported strategies). Default is 'densest_packing'.
        parameters_init_method : str, optional
            The initialization method for the parameters. Default is 'xavier_normal'.
        device : str, optional
            The device to use ('cpu' or 'cuda'). Default is 'cpu'.
        *args, **kwargs
            Additional arguments passed to the parent layer class.

        Raises
        ------
        ValueError
            If invalid patch shape or grid dimensions are provided.
        """
        print('* grid_compression_layer')
        heads = [
            grid_compression_head(
                h=h, w=w, d=d,
                channel_num=channel_num,
                pooling_metric=pooling_metric,
                patch_shape=patch_shape,
                p_h=p_h, p_h_prime=p_h_prime,
                p_w=p_w, p_w_prime=p_w_prime,
                p_d=p_d, p_d_prime=p_d_prime,
                p_r=p_r,
                cd_h=cd_h, cd_w=cd_w, cd_d=cd_d,
                packing_strategy=packing_strategy,
                with_dropout=with_dropout, p=p,
                parameters_init_method=parameters_init_method,
                device=device, *args, **kwargs
            )
        ]
        assert len(heads) >= 1
        m, n = heads[0].get_m(), heads[0].get_n()
        print('--------------------------')
        super().__init__(name=name, m=m, n=n, heads=heads, device=device, *args, **kwargs)

    def get_output_grid_shape(self):
        """
        Get the shape of the output grid after packing.

        Returns
        -------
        tuple
            A tuple representing the dimensions of the output grid (height, width, depth).
        """
        assert len(self.heads) >= 1
        return self.heads[0].get_output_grid_shape()

__init__(h, w, channel_num, d=1, name='grid_compression_layer', pooling_metric='batch_max', patch_shape='cuboid', p_h=None, p_h_prime=None, p_w=None, p_w_prime=None, p_d=0, p_d_prime=None, p_r=None, cd_h=None, cd_w=None, cd_d=1, with_dropout=False, p=0.5, packing_strategy='densest_packing', parameters_init_method='xavier_normal', device='cpu', *args, **kwargs)

Initialize the grid_compression_layer.

This layer compresses grid inputs by applying pooling metrics and packing strategies for dimensionality reduction.

Parameters:

Name Type Description Default
h int

The height of the input grid.

required
w int

The width of the input grid.

required
channel_num int

The number of input channels.

required
d int

The depth of the input grid. Default is 1 (for 2D grids).

1
name str

The name of the layer. Default is 'grid_compression_layer'.

'grid_compression_layer'
pooling_metric str

The metric used for pooling (e.g., 'batch_max', 'batch_min', 'batch_avg'). Default is 'batch_max'.

'batch_max'
patch_shape str

The shape of the patch ('cuboid', 'cylinder', or 'sphere'). Default is 'cuboid'.

'cuboid'
p_h int

The height of the patch.

None
p_h_prime int

The height of the inner patch (used for hierarchical packing).

None
p_w int

The width of the patch.

None
p_w_prime int

The width of the inner patch (used for hierarchical packing).

None
p_d int

The depth of the patch.

0
p_d_prime int

The depth of the inner patch (used for hierarchical packing).

None
p_r int

The radius of the patch (used for spherical or cylindrical patches).

None
cd_h int

The compression degree along the height dimension.

None
cd_w int

The compression degree along the width dimension.

None
cd_d int

The compression degree along the depth dimension. Default is 1.

1
with_dropout bool

If True, applies dropout after the compression step. Default is False.

False
p float

The dropout probability. Default is 0.5.

0.5
packing_strategy str

The packing strategy used ('densest_packing' or other supported strategies). Default is 'densest_packing'.

'densest_packing'
parameters_init_method str

The initialization method for the parameters. Default is 'xavier_normal'.

'xavier_normal'
device str

The device to use ('cpu' or 'cuda'). Default is 'cpu'.

'cpu'
*args

Additional arguments passed to the parent layer class.

()
**kwargs

Additional arguments passed to the parent layer class.

()

Raises:

Type Description
ValueError

If invalid patch shape or grid dimensions are provided.

Source code in tinybig/layer/grid_based_layers.py
def __init__(
    self,
    h: int, w: int, channel_num: int,
    d: int = 1,
    name: str = 'grid_compression_layer',
    pooling_metric: str = 'batch_max',
    patch_shape: str = 'cuboid',
    p_h: int = None, p_h_prime: int = None,
    p_w: int = None, p_w_prime: int = None,
    p_d: int = 0, p_d_prime: int = None,
    p_r: int = None,
    cd_h: int = None, cd_w: int = None, cd_d: int = 1,
    with_dropout: bool = False, p: float = 0.5,
    packing_strategy: str = 'densest_packing',
    # other parameters
    parameters_init_method: str = 'xavier_normal',
    device: str = 'cpu', *args, **kwargs
):
    """
    Initialize the grid_compression_layer.

    This layer compresses grid inputs by applying pooling metrics and packing strategies for dimensionality reduction.

    Parameters
    ----------
    h : int
        The height of the input grid.
    w : int
        The width of the input grid.
    channel_num : int
        The number of input channels.
    d : int, optional
        The depth of the input grid. Default is 1 (for 2D grids).
    name : str, optional
        The name of the layer. Default is 'grid_compression_layer'.
    pooling_metric : str, optional
        The metric used for pooling (e.g., 'batch_max', 'batch_min', 'batch_avg'). Default is 'batch_max'.
    patch_shape : str, optional
        The shape of the patch ('cuboid', 'cylinder', or 'sphere'). Default is 'cuboid'.
    p_h : int, optional
        The height of the patch.
    p_h_prime : int, optional
        The height of the inner patch (used for hierarchical packing).
    p_w : int, optional
        The width of the patch.
    p_w_prime : int, optional
        The width of the inner patch (used for hierarchical packing).
    p_d : int, optional
        The depth of the patch.
    p_d_prime : int, optional
        The depth of the inner patch (used for hierarchical packing).
    p_r : int, optional
        The radius of the patch (used for spherical or cylindrical patches).
    cd_h : int, optional
        The compression degree along the height dimension.
    cd_w : int, optional
        The compression degree along the width dimension.
    cd_d : int, optional
        The compression degree along the depth dimension. Default is 1.
    with_dropout : bool, optional
        If True, applies dropout after the compression step. Default is False.
    p : float, optional
        The dropout probability. Default is 0.5.
    packing_strategy : str, optional
        The packing strategy used ('densest_packing' or other supported strategies). Default is 'densest_packing'.
    parameters_init_method : str, optional
        The initialization method for the parameters. Default is 'xavier_normal'.
    device : str, optional
        The device to use ('cpu' or 'cuda'). Default is 'cpu'.
    *args, **kwargs
        Additional arguments passed to the parent layer class.

    Raises
    ------
    ValueError
        If invalid patch shape or grid dimensions are provided.
    """
    print('* grid_compression_layer')
    heads = [
        grid_compression_head(
            h=h, w=w, d=d,
            channel_num=channel_num,
            pooling_metric=pooling_metric,
            patch_shape=patch_shape,
            p_h=p_h, p_h_prime=p_h_prime,
            p_w=p_w, p_w_prime=p_w_prime,
            p_d=p_d, p_d_prime=p_d_prime,
            p_r=p_r,
            cd_h=cd_h, cd_w=cd_w, cd_d=cd_d,
            packing_strategy=packing_strategy,
            with_dropout=with_dropout, p=p,
            parameters_init_method=parameters_init_method,
            device=device, *args, **kwargs
        )
    ]
    assert len(heads) >= 1
    m, n = heads[0].get_m(), heads[0].get_n()
    print('--------------------------')
    super().__init__(name=name, m=m, n=n, heads=heads, device=device, *args, **kwargs)

get_output_grid_shape()

Get the shape of the output grid after packing.

Returns:

Type Description
tuple

A tuple representing the dimensions of the output grid (height, width, depth).

Source code in tinybig/layer/grid_based_layers.py
def get_output_grid_shape(self):
    """
    Get the shape of the output grid after packing.

    Returns
    -------
    tuple
        A tuple representing the dimensions of the output grid (height, width, depth).
    """
    assert len(self.heads) >= 1
    return self.heads[0].get_output_grid_shape()