Reputation: 349
I have some data where I have a 2D array A
and a kernel K
. I would like to compute a convolution of these. How do I do this in TensorFlow. It looks like the built-in convolution operations take a 4D tensor as input, since they assume that we're computing a batch with multiple color channels. How do I use these ops with my data?
Example:
a = np.random.randint(2, size=(10,10))
k = [[1,1,1],[1,1,1],[1,1,1]]
# Compute convolution of these??
c = ...
Upvotes: 2
Views: 3706
Reputation: 41
In addition to kaufmanu's answer, you can use tf.squeeze
if you need a 2D result. I.e.,
c=tf.squeeze(tf.nn.conv2d(a_tensor, k_weight,padding='VALID',strides=[1, 1, 1, 1]))
to perform the convolution.
Upvotes: 1
Reputation: 2860
The answers posted so far all miss one important point: Tensorflow does not compute a convolution, but a cross-correlation as is stated in the doc:
Note that although these ops are called "convolution", they are strictly speaking "cross-correlation" since the filter is combined with an input window without reversing the filter.
If you really want to compute a convolution, you will have to reverse the kernel before passing it into conv2d
, i.e. flip it once on the horizontal then on the vertical axis. Using Miriam's answer, this could look like this:
import tensorflow as tf
import numpy as np
a = np.random.randint(2, size=(10,10))
k = np.array([[1,1,1],[1,1,1],[1,1,1]],dtype=np.float32)
flip = [slice(None, None, -1), slice(None, None, -1)]
k = k[flip]
a=a.astype(np.float32)
a_tensor = tf.reshape(a, [1, 10, 10, 1])
k_weight = tf.reshape(np.array(k), [3,3,1,1])
c=tf.nn.conv2d(a_tensor, k_weight,padding='VALID',strides=[1, 1, 1, 1])
sess=tf.Session()
c.eval(session=sess)
Note that in this specific example flipping the kernel is technically in vain, because for symmetric kernels convolution and cross-correlation is the same thing. However, as soon as you have a non-symmetric kernels, you must flip it if you want Tensorflow to actually compute a convolution.
Upvotes: 1
Reputation: 33
import tensorflow as tf
import numpy as np
a = np.random.randint(2, size=(10,10))
k = [[1,1,1],[1,1,1],[1,1,1]]
tensor_a = tf.constant(a, tf.float32)
tensor_k = tf.constant(k, tf.float32)
tensor_res = tf.nn.convolution(tf.reshape(tensor_a, [1, 10, 10, 1]), tf.reshape(tensor_k, [3, 3, 1, 1]), padding='VALID')
sess = tf.Session()
print(sess.run(tensor_res))
The Computational Graph tutorial is here
Convolution helper doc
Upvotes: 3