Understand tf.layers.conv2d() with Examples – TensorFlow Tutorial

By | July 28, 2021

To implement 2D convolution operation, we can use tf.nn.conv2d(),here is the tutorial:

Understand tf.nn.conv2d(): Compute a 2-D Convolution in TensorFlow – TensorFlow Tutorial

However, we also can use tf.layers.conv2d() method. In this tutorial, we will use some examples to show you how to use it correctly.

Syntax

tf.layers.conv2d() is defined as:

tf.layers.conv2d(inputs, filters, kernel_size,
strides=(1, 1),
data_format='channels_last',
dilation_rate=(1, 1),
activation=None,
use_bias=True,
kernel_initializer=None,
bias_initializer=tf.zeros_initializer()
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
trainable=True,
name=None,
reuse=None)

It can create a convolution netwrok based on filters and kernel_size.

Important Parameters

inputs: the input tensor, the shape of it is [batch, in_height, in_width, in_channels], which is same to tf.nn.conv2d().

filters: the dimensionality of the output. As a filter, the shape of it is [filter_height, filter_width, in_channels, out_channels] in tf.nn.conv2d().

In tf.layers.conv2d(), parameter filters = out_channels

kernel_size: an integer or a tuple / list. It is the height and width of the 2D convolution filter window.

For example:

if kernel_size = 3, it means filter_height = 3, filter_width = 3

if kernel_size = [3, 4], it means filter_height = 3, filter_width = 4

The relationship of filters and kernel_size between tf.nn.conv2d() and tf.layers.conv2d() is below:

We will use some examples to explain this.

How to use tf.layers.conv2d()?

Here is an example:

import tensorflow as tf
import numpy as np
g = tf.Variable(tf.truncated_normal([64, 40, 30, 200], stddev=0.1), name="inputs")
K = 10
s = tf.layers.conv2d(g, K, 1, use_bias=True,
kernel_initializer=None,
name='assignment')

init = tf.global_variables_initializer()
init_local = tf.local_variables_initializer()
with tf.Session() as sess:
sess.run([init, init_local])
np.set_printoptions(precision=4, suppress=True)
a =sess.run(s)
print(a.shape)
for n in tf.trainable_variables():
print(n)

Here, the input is g, the shape of it is [64, 40, 30, 200]. It means the batch = 64, in_height = 40, in_width=30, in_channels = 200

K is the filters in tf.layers.conv2d() 10, it means the out_channels = 10

kenerl_size is 1 in tf.layers.conv2d(), which means the height of width of filter is 1 in a convolution network.

Run this code, we will get:

(64, 40, 30, 10)
<tf.Variable 'inputs:0' shape=(64, 40, 30, 200) dtype=float32_ref>
<tf.Variable 'assignment/kernel:0' shape=(1, 1, 200, 10) dtype=float32_ref>
<tf.Variable 'assignment/bias:0' shape=(10,) dtype=float32_ref>

We can find:

If we set K = 20, the kernel_size = [3, 30], here is the example:

import tensorflow as tf
import numpy as np
#5*4*10
g = tf.Variable(tf.truncated_normal([64, 40, 30, 200], stddev=0.1), name="inputs")
K = 20
s = tf.layers.conv2d(g, K, [3, 30], use_bias=True,
kernel_initializer=None,
name='assignment')

Run this code, we will get:

(64, 38, 1, 20)
<tf.Variable 'inputs:0' shape=(64, 40, 30, 200) dtype=float32_ref>
<tf.Variable 'assignment/kernel:0' shape=(3, 30, 200, 20) dtype=float32_ref>
<tf.Variable 'assignment/bias:0' shape=(20,) dtype=float32_ref>

From the result, we also can find: