You should know about the use of convolutional layers first, to implement them with different filters. Convolutions are used to transform the original function into a form to get more information. Convolutions have been used in image processing to blur and sharpen images and many other applications.
If the image is larger than the size of the filter, we slide the filter to the various parts of the image and perform the convolution operation. Each time we do that, we generate a new pixel in the output image. The 2D convolution is most commonly used and abbreviated as conv2D.
TensorFlow's conv2d function calculates convolutions in batches and uses a slightly different format. For an input it is [batch, in_height, in_width, in_channels] for the kernel it is [filter_height, filter_width, in_channels, out_channels].
import tensorflow as tf
k = tf.constant([
[1, 0, 1],
[2, 1, 0],
[0, 0, 1] ],
i = tf.constant([
[4, 3, 1, 0],
[2, 1, 0, 1],
[1, 2, 4, 1],
[3, 1, 0, 2] ],
kernel = tf.reshape(k, [3, 3, 1, 1], name='kernel')
image = tf.reshape(i, [1, 4, 4, 1], name='image')
Connecting to a session to compute value:
res = tf.squeeze(tf.nn.conv2d(image, kernel, [1, 1, 1, 1], "VALID"))
# VALID means no padding
with tf.Session() as sess:
This code will return the result of 2d convolution computations.
Visit here if you want to know more about TensorFlow and Neural Network.