title: What does tf.nn.conv2d do in tensorflow?
body: <p>I was looking at the docs of tensorflow about <code>tf.nn.conv2d</code> <a href="https://www.tensorflow.org/api_docs/python/tf/nn/conv2d" rel="noreferrer">here</a>. But I can't understand what it does or what it is trying to achieve. It says on the docs,</p>
  <blockquote>
  <p>#1 : Flattens the filter to a 2-D matrix with shape</p>
  <p><code>[filter_height * filter_width * in_channels, output_channels]</code>.</p>
  </blockquote>
  <p>Now what does that do? Is that element-wise multiplication or just plain matrix multiplication? I also could not understand the other two points mentioned in the docs. I have written them below :</p>
  <blockquote>
  <p># 2: Extracts image patches from the the input tensor to form a virtual tensor of shape</p>
  <p><code>[batch, out_height, out_width, filter_height * filter_width * in_channels]</code>.</p>
  <p># 3: For each patch, right-multiplies the filter matrix and the image patch vector.</p>
  </blockquote>
  <p>It would be really helpful if anyone could give an example, a piece of code (extremely helpful) maybe and explain what is going on there and why the operation is like this.</p>
  <p>I've tried coding a small portion and printing out the shape of the operation. Still, I can't understand.</p>
  <p>I tried something like this:</p>
  <pre class="lang-python prettyprint-override"><code>op = tf.shape(tf.nn.conv2d(tf.random_normal([1,10,10,10]), 
                tf.random_normal([2,10,10,10]), 
                strides=[1, 2, 2, 1], padding='SAME'))
  
  with tf.Session() as sess:
      result = sess.run(op)
      print(result)
  </code></pre>
  <p>I understand bits and pieces of convolutional neural networks. I studied them <a href="http://cs231n.github.io/convolutional-networks/" rel="noreferrer">here</a>. But the implementation on tensorflow is not what I expected. So it raised the question.</p>
  <p><strong>EDIT</strong>:
  So, I implemented a much simpler code. But I can't figure out what's going on. I mean how the results are like this. It would be extremely helpful if anyone could tell me what process yields this output.</p>
  <pre class="lang-python prettyprint-override"><code>input = tf.Variable(tf.random_normal([1,2,2,1]))
  filter = tf.Variable(tf.random_normal([1,1,1,1]))
  
  op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
  init = tf.initialize_all_variables()
  with tf.Session() as sess:
      sess.run(init)
  
      print(&quot;input&quot;)
      print(input.eval())
      print(&quot;filter&quot;)
      print(filter.eval())
      print(&quot;result&quot;)
      result = sess.run(op)
      print(result)
  </code></pre>
  <p>output</p>
  <pre><code>input
  [[[[ 1.60314465]
     [-0.55022103]]
  
    [[ 0.00595062]
     [-0.69889867]]]]
  filter
  [[[[-0.59594476]]]]
  result
  [[[[-0.95538563]
     [ 0.32790133]]
  
    [[-0.00354624]
     [ 0.41650501]]]]
  </code></pre>