Q:

tensorflow periodic padding

In tensorflow I cannot find a straightforward possibility to do a convolution (tf.nn.conv2d) with periodic boundary conditions. 
E.g. take the tensor
[[1,2,3],
 [4,5,6],
 [7,8,9]]

and any 3x3 filter.  A convolution with periodic boundary conditions could in principle be done by doing a periodic padding to 5x5
[[9,7,8,9,7],
 [3,1,2,3,1],
 [6,4,5,6,4],
 [9,7,8,9,7],
 [3,1,2,3,1]]

and subsequently a convolution with the filter in "valid" mode.  However, the function tf.pad unfortunately does not support periodic padding. 
Is there a simple workaround?  

A:

The following should work for your case :
import tensorflow as tf
a = tf.constant([[1,2,3],[4,5,6],[7,8,9]])
b = tf.tile(a, [3, 3])
result = b[2:7, 2:7]
sess = tf. InteractiveSession()
print(result.eval())

# prints the following 
array([[9, 7, 8, 9, 7],
       [3, 1, 2, 3, 1],
       [6, 4, 5, 6, 4],
       [9, 7, 8, 9, 7],
       [3, 1, 2, 3, 1]], dtype=int32)

As noted in the comments, this is a little inefficient in terms of memory.  If memory is an issue for you, but are willing to spend some compute, the following will also work :
pre = tf.constant([[0, 0, 1], [1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 0, 0]])
post = tf.transpose(pre)
result = tf.matmul(tf.matmul(pre, a), post)
print(result.eval())

