09C. Conv2D

Входной файл:Стандартный вход  
Выходной файл:Стандартный выход  

Требуется реализовать на языке Python класс, описывающий двумерный свёрточный слой нейронной сети:


import numpy as np

class Conv2D:
    """Implements 2d convolution layer"""
    
    def __init__(self, 
             n_in: int,
             n_out: int,
             kernel_size: tuple[int, int],
             strides: tuple[int, int] = (1, 1),
             padding: str = 'valid',
             use_bias: bool = True,
             dilations: tuple[int, int] = (1, 1)
        ):
        """Initialized Conv2D layer.
        The weights are initialized using uniformly distributed values in range [-1, 1]. Bias vector is not initialized if `use_bias` is False.
        Weights tensor has the shape (`kernel_size[0]`, `kernel_size[1]`, `n_in`, `n_out`), bias vector has the shape (`n_out`, ).

        Arguments:
            n_in: Positive integer, dimensionality of input space.
            n_out: Positive integer, dimensionality of output space.
            kernel_size: Pair of positive integers, convolution kernel dimensions.
            strides: Pair of positive integers, convolution strides along each dimension.
            padding: Either 'valid' or 'same', padding to be used by the layer, see explanation below.
            use_bias: Whether the layer uses a bias vector.
            dilations: Pair of positive integers, convolution kernel dilations along each dimension.

        Padding:
            'valid': no padding is applied.
            'same': padding along every dimension is computed as follows
                if dimension_size % stride == 0:
                    total_pad = max((kernel_size - 1) * dilation + 1 - stride)
                else:
                    total_pad = max((kernel_size - 1) * dilation + 1 - dimension_size % stride)
                pad_before = total_pad // 2
                pad_after = total_pad - pad_before"""
        pass

    @property
    def weights(self) -> tuple[np.ndarray, np.ndarray] | tuple[np.ndarray]:
        """Returns weights used by the layer. Weight tensor and bias vector if use_bias is True"""
        pass

    def __call__(self, x: np.ndarray) -> np.ndarray:
        """Performs the layer forward pass.

        Arguments:
            x: Input array of shape (`batch_size`, `height`, `width`, `n_in`).

        Returns:
            An array of shape (`batch_size`, `new_height`, `new_width`, `n_out`)."""
        pass

    def grad(self, x: np.ndarray, gradOutput: np.ndarray) -> tuple[np.ndarray, tuple[np.ndarray, np.ndarray] | tuple[np.ndarray]]:
        """Computes layer gradients

        Arguments:
            gradOutput: Gradient of loss function with respect to the layer output, an array of shape (`batch_size`, `new_height`, `new_width`, `n_out`).

        Returns:
            A tuple object:
                Gradient of loss function with respect to the layer input, an array of shape (`batch_size`, `height`, `width`, `n_in`)
                Gradient of loss function with respect to the layer's weights:
                    An array of shape (`kernel_size[0]`, `kernel_size[1]`, `n_in`, `n_out`).
                    Optional array of shape (`n_out`, )."""
        pass

Для реализации класса разрешено использовать только модуль numpy.

Формат выходных данных

Код решения должен содержать только импортируемые модули, определение и реализацию класса.


0.042s 0.007s 13