How does Keras choose filters?

In one of week 2’s assignments we passed the number of filters (F1, F2, F3, etc) we’d like to use in a conv2D layer. However, there was no definition of these filters anywhere. My question is, does keras (tensorflow) have a library of filters that it simply picks from given the number you’d like to use?


The filter shapes are chosen by the experimenter.
The filter values are learned during training.

OK - So how is this done in practice in Keras? If I look at the call for conv2D it might look something like:

X = Conv2D(filters = F1, kernel_size = 3, strides = (1,1), padding = 'valid', kernel_initializer = initializer(seed=0))(X)

where F1 is simply a number of filters i.e. an integer. Nowhere in this assignment did I specify what the filter values are, that is, what the numbers in the 3x3 matrix in this case will be.

Look at the code used by the unit test for this function.

Sorry TMost, I’m not sure what you mean by look at the code, which code? Where can I access this code? If there is a specific place where the filters are specified perhaps you could point me to that?


The unit test code is in the notebook cell that has the unit test.
It’s the cell right after the cell where you wrote the function code.
It will have the word “test” in the function name.

OK, let’s take a look at the test block.

Firstly the conv2D function that I am calling is written inside a function

def identity_block(X, f, filters, training=True, initializer=random_uniform)

This function doesn’t seem to take the form of the filters, just the number of filters (as a numpy array) in different layers.

In the test block the function above is called as

identity_block(X, f=2, filters=[4, 4, 3],
initializer=lambda seed=0:constant(value=1),

here you can see that the ‘filters’ variable is passed as an array of numbers. The rest of the test code you referred me to is pasted below, can you point out here where the filters are defined?

X1 = np.ones((1, 4, 4, 3)) * -1
X2 = np.ones((1, 4, 4, 3)) * 1
X3 = np.ones((1, 4, 4, 3)) * 3

X = np.concatenate((X1, X2, X3), axis = 0).astype(np.float32)

A3 = identity_block(X, f=2, filters=[4, 4, 3],
initializer=lambda seed=0:constant(value=1),
print(’\033[1mWith training=False\033[0m\n’)
A3np = A3.numpy()
print(np.around(A3.numpy()[:,(0,-1),:,:].mean(axis = 3), 5))
resume = A3np[:,(0,-1),:,:].mean(axis = 3)
print(resume[1, 1, 0])

print(’\n\033[1mWith training=True\033[0m\n’)
A4 = identity_block(X, f=2, filters=[3, 3, 3],
initializer=lambda seed=0:constant(value=1),
print(np.around(A4.numpy()[:,(0,-1),:,:].mean(axis = 3), 5))