Understand the Shape of Tensor Returned by tf.nn.conv2d() – TensorFlow Tutorial

By | August 9, 2020

TensorFlow tf.nn.conv2d() function can build a convolution network. Here is a tutorial:

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

However, how about the shape of returned tensor? In this tutorial, we will discuss this topic.

We have known the shape of tensor returned by tf.nn.conv2d()  is: [batch, out_height, out_width, out_channels ]out_height and out_width is determinded by filterstridespadding and dilations.

If dilations=[1, 1, 1, 1], out_height and out_width can be computed as following.

if padding = ‘SAME’

out_height = ceil(float(in_height) / float(strides[1]))

out_width = ceil(float(in_width) / float(strides[2]))

if padding = ‘VALID’

out_height = ceil(float(in_height - filter_height + 1) / float(strides[1]))
out_width = ceil(float(in_width - filter_width + 1) / float(strides[2]))

The difference between SAME and VALID padding is here:

Understand the Difference Between ‘SAME’ and ‘VALID’ Padding in Convolution Networks

Here strides = [1, stride, stride, 1]

Leave a Reply