Issue
I want to generate a circulant matrix in tensorflow without using any for loops. For example my input is [1, 2, 3]
, and the expected output is [[1,2,3],[2,3,1],[3,1,2]]
. I think we can use nd convolution to do it, but tensorflow doesn't have nd convolution.
Solution
I think the simplest solution to this problem, without using an explicit loop, is to use tf.extract_volume_patches, which can be used to creating a sliding window over a tensor. You will just have to reshape the tensor accordingly:
import tensorflow as tf
tensor = tf.constant([1, 2, 3])
stride = 4
window_size = 3
tensor = tf.tile(tensor, [stride])
tensor = tf.reshape(tensor, [1, -1, 1, 1, 1])
windowed_tensor = tf.extract_volume_patches(tensor,
ksizes=[1, window_size, 1, 1, 1],
strides=[1, stride, 1, 1, 1],
padding='VALID')[0, :, 0, 0]
tf.print(windowed_tensor)
[[1 2 3]
[2 3 1]
[3 1 2]]
Answered By - AloneTogether
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.