brainstorm/tests/test_handler_operations.py
def _conv2d_forward_batch(inputs, weights, bias, outputs, padding, stride):
    """
    Loop-based implementation of 2D convolution to check against.
    """

    num_filters = weights.shape[0]
    num_images, input_height, input_width,  num_input_maps = inputs.shape
    kernel_size = (weights.shape[1], weights.shape[2])

    if padding > 0:
        im = np.zeros((inputs.shape[0],
                       inputs.shape[1] + 2 * padding,
                       inputs.shape[2] + 2 * padding,
                       inputs.shape[3],))
        im[:, padding: -padding, padding: -padding, :] = inputs
        input_height += 2 * padding
        input_width += 2 * padding
    else:
        im = inputs
    for i in range(num_images):
        x_f = -1
        for m in range(0, input_height - kernel_size[0] + 1, stride[0]):
            x_f += 1
            y_f = -1
            for n in range(0, input_width - kernel_size[1] + 1, stride[1]):
                y_f += 1
                for f in range(num_filters):
                    for k in range(num_input_maps):
                        for x in range(kernel_size[0]):
                            for y in range(kernel_size[1]):
                                outputs[i, x_f, y_f, f] \
                                    += weights[f, x, y, k] * \
                                    im[i, m + x, n + y, k]

    for i in range(num_images):
        for j in range(outputs.shape[1]):
            for k in range(outputs.shape[2]):
                    for l in range(num_filters):
                        outputs[i, j, k, l] += bias[l]

    Loop-based implementation of 2D convolution to check against.
    
Loop-based implementation of 2D convolution to check against.
Loop - based implementation of 2D convolution to check against .
def _conv2d_forward_batch(inputs, weights, bias, outputs, padding, stride):
    num_filters = weights.shape[0]
    num_images, input_height, input_width,  num_input_maps = inputs.shape
    kernel_size = (weights.shape[1], weights.shape[2])
    if padding > 0:
        im = np.zeros((inputs.shape[0],
                       inputs.shape[1] + 2 * padding,
                       inputs.shape[2] + 2 * padding,
                       inputs.shape[3],))
        im[:, padding: -padding, padding: -padding, :] = inputs
        input_height += 2 * padding
        input_width += 2 * padding
    else:
        im = inputs
    for i in range(num_images):
        x_f = -1
        for m in range(0, input_height - kernel_size[0] + 1, stride[0]):
            x_f += 1
            y_f = -1
            for n in range(0, input_width - kernel_size[1] + 1, stride[1]):
                y_f += 1
                for f in range(num_filters):
                    for k in range(num_input_maps):
                        for x in range(kernel_size[0]):
                            for y in range(kernel_size[1]):
                                outputs[i, x_f, y_f, f] \
                                    += weights[f, x, y, k] * \
                                    im[i, m + x, n + y, k]
    for i in range(num_images):
        for j in range(outputs.shape[1]):
            for k in range(outputs.shape[2]):
                    for l in range(num_filters):
                        outputs[i, j, k, l] += bias[l]
def _conv2d_forward_batch ( inputs , weights , bias , outputs , padding , stride ) : num_filters = weights . shape [ 0 ] num_images , input_height , input_width , num_input_maps = inputs . shape kernel_size = ( weights . shape [ 1 ] , weights . shape [ 2 ] ) if padding > 0 : im = np . zeros ( ( inputs . shape [ 0 ] , inputs . shape [ 1 ] + 2 * padding , inputs . shape [ 2 ] + 2 * padding , inputs . shape [ 3 ] , ) ) im [ : , padding : - padding , padding : - padding , : ] = inputs input_height += 2 * padding input_width += 2 * padding else : im = inputs for i in range ( num_images ) : x_f = - 1 for m in range ( 0 , input_height - kernel_size [ 0 ] + 1 , stride [ 0 ] ) : x_f += 1 y_f = - 1 for n in range ( 0 , input_width - kernel_size [ 1 ] + 1 , stride [ 1 ] ) : y_f += 1 for f in range ( num_filters ) : for k in range ( num_input_maps ) : for x in range ( kernel_size [ 0 ] ) : for y in range ( kernel_size [ 1 ] ) : outputs [ i , x_f , y_f , f ] += weights [ f , x , y , k ] * im [ i , m + x , n + y , k ] for i in range ( num_images ) : for j in range ( outputs . shape [ 1 ] ) : for k in range ( outputs . shape [ 2 ] ) : for l in range ( num_filters ) : outputs [ i , j , k , l ] += bias [ l ]
Loop-based implementation of 2D convolution to check against.
Loop - based implementation of 2D convolution to check against .
"""
    Loop-based implementation of 2D convolution to check against.
    """
inputs
weights
bias
outputs
padding
stride