Home > Back-end >  Why does TensorFlow Conv2D have two weights matrices?
Why does TensorFlow Conv2D have two weights matrices?

Time:11-22

I have a tf.keras.layers.Conv2D constructed like so:

>>> conv2d_layer = tf.keras.layers.Conv2D(filters=128, kernel_size=(3, 3), strides=2)

For reference that layer is part of a network where the prior layer is prior_layer = Conv2D(filters=64, kernel_size=(3, 3), strides=2).

When I call conv2d_layer.get_weights(), it returns a list with two entries:

>>> [w.shape for w in conv2d_layer.get_weights()]
[(3, 3, 64, 128), (128,)]

Why are there two np.ndarrays in conv2d_layer.get_weights()? What are their respective meanings?

CodePudding user response:

The first shape is for the weights of your conv2D, and the second one is the bias for the same layer, which is represented by a vector.

Looking at the documentation, you can see

For example, a Dense layer returns a list of two values: the kernel matrix and the bias vector. These can be used to set the weights of another Dense layer:

CodePudding user response:

You have 128 convolution filters, each filter has a bias and a kernel. The kernel has a size 3x3. Furthermore, the kernel depth is equal to the input depth (it means 64 in this example). So we have (3, 3, 64) for a kernel, and we have 128 filters, so all filter's shapes are equal to (3, 3, 64, 128). Also, we have a bias for every filter so the shape of the second weight is equal to (128,)

  • Related