14.1. Cross Correlation (CC)

Our implementation API:

Convolutional Neural Network (CNN) as node abstraction.

class fhez.nn.operations.cc.CC(weights: Optional[numpy.ndarray] = None, stride: Optional[numpy.ndarray] = None, bias: Optional[numpy.ndarray] = None, optimiser=None)

Convolutional Neural Network.

property b

Shorthand for bias.

backward(gradient: numpy.ndarray)

Compute computational filter gradient and input gradient.

property bias

Get sum of products bias coefficient.

property cost

Get computational cost of this CNN node.

forward(x: numpy.ndarray)

Compute convolutional filter forward pass and sums.

probe_shape(lst: list)

Get the shape of a list, assuming each sublist is the same length.

This function is recursive, sending the sublists down and terminating once a type error is thrown by the final point being a non-list

property schema

Get Marshmallow schema representation of this class.

Marshmallow schemas allow for easy and trustworthy serialisation and deserialisation of arbitrary objects either to inbulit types or json formats. This is an inherited member of the abstract class Serialise.

Note

Anything not listed here will inevitably be lost, ensure anything important is identified and expressley stated its type and structure.

property stride

Get stride over convolutions.

update()

Update node state/ weights for a single example.

updates()

Update node state/ weights for multiple examples simultaneously.

property w

Shorthand for weights.

property weights

Get cross convolved filter n-dimensional weights or error.

windex(data: list, filter: list, stride: list, dimension: int = 0, partial: list = [])

Recursive window index or Windex.

This function takes 3 lists; data, filter, and stride. Data is a regular multidimensional list, so in the case of a 32x32 pixel image you would expect a list of shape (32,32,3) 3 being the RGB channels. Filter is the convolutional filter which we seek to find all windows of inside the data. So for data (32,32,3) a standard filter could be applied of shape (3,3,3). Stride is a 1 dimensional list representing the strides for each dimension, so a stride list such as [1,2,3] on data (32,32,3) and filter (3,3,3), would move the window 1 in the first 32 dimension, 2 in the second 32 dim, and 3 in the 3 dimension.

This function returns a 1D list of all windows, which are themselves lists. These windows are the same length as the number of dimensions, and each dimension consists of indexes with which to slice the original data to create the matrix with which to convolve (cross correlate). An example given: data.shape=(4,4), filter.shape=(2,2), stride=[1,1]

list_of_window_indexes = [
    [[0, 1], [0, 1]], # 0th window
    [[0, 1], [1, 2]], # 1st window
    [[0, 1], [2, 3]], # ...
    [[1, 2], [0, 1]],
    [[1, 2], [1, 2]],
    [[1, 2], [2, 3]],
    [[2, 3], [0, 1]],
    [[2, 3], [1, 2]],
    [[2, 3], [2, 3]], # T_x-1 window
]

We get the indexes rather than the actual data for two reasons:

  • we want to be able to cache this calculation and use it for homogenus data that could be streaming into a convolutional neural networks, cutting the time per epoch down.

  • we want to use pure list slicing so that we can work with non- standard data, E.G Fully Homomorphically Encrypted lists.

windex_to_slice(window)

Convert x sides of window expression into slices to slice np.

property windows

Get current list of windows for cross correlation.