I am creating my own neural network library and I am now creating a convolution algorithm. I am trying to part the input to local receptive fields, and then multiply it by the respective weights, sum the multiplied receptive fields, add the biases and return the result. I am using the 'numpy.lib.stride_tricks.as_strided' function, and I'm not sure how set the strides. I would really appreciate your help. Here's the code:
"""
the matrix shape is:
mini_batch_size X
numberOfFilters X
numberOfInputFeatureMaps X
sizeOfInputImage[0] X
sizeOfInputImage[1]
the kernel shape is:
mini_batch_size X
numberOfFilters X
sizeOfLocalReceptiveField[0] X
sizeOfLocalReceptiveField[1]
"""
s1, s2, s3, s4, s5 = matrix.strides
imageWidth, imageHeight = matrix.shape[-2:]
localReceptiveFieldWidth, localReceptiveFieldHeight = kernel.shape[-2:]
matrix_shape = matrix.shape
numberOfLocalReceptiveFields =
(1 + (imageWidth - localReceptiveFieldWidth) // stride) *
(1 + (imageHeight - localReceptiveFieldHeight) // stride) *
matrix_shape[1]
view_shape = (
matrix_shape[0], # mini batch size
matrix_shape[1], # number of filters
numberOfLocalReceptiveFields,
localReceptiveFieldWidth,
localReceptiveFieldHeight
)
strides = () # don't know what to put here
subs = numpy.lib.stride_tricks.as_strided(matrix, view_shape, strides=strides)