Given a vector = [1, 2, 3, 4, 5,6]
Is there a function which can convert it in to a Toeptliz matrix of 4 columns as given below ?
[ [1,2,4,5],[2,4,5,6], [3, 4,5,6]
I want to apply this transform to a batch of vectors.
Given a vector = [1, 2, 3, 4, 5,6]
Is there a function which can convert it in to a Toeptliz matrix of 4 columns as given below ?
[ [1,2,4,5],[2,4,5,6], [3, 4,5,6]
I want to apply this transform to a batch of vectors.
I think tf.linalg.LinearOperatorToeplitz
is what you’re looking for
No, that does this
col = [1., 2., 3.]
row = [1., 4., -9.]
operator = LinearOperatorToeplitz(col, row)operator.to_dense()
[[1., 4., -9.], [2., 1., 4.], [3., 2., 1.]]
my requirement is different.
Currently I use the following function do it. I would like to know whether there is any builtin function does it, so that I can avoid the for loop used here.
tf.stack([ Inf[ i:i+ width] for i in range(length-width+1)])
I don’t understand your 4 columns output Matrix example.
[ [1,2,4,5],[2,4,5,6], [3, 4,5,6]
Is it a Toeplitz matrix?
I still think the operation mentioned is the solution (it’s actually more general that what OP asks for, but can me made to work for OP’s case):
>>> import tensorflow as tf
>>> v = [1, 2, 3, 4, 5,6]
>>> tf.linalg.LinearOperatorToeplitz(v, v).to_dense()
<tf.Tensor: shape=(6, 6), dtype=int32, numpy=
array([[1, 2, 3, 4, 5, 6],
[2, 1, 2, 3, 4, 5],
[3, 2, 1, 2, 3, 4],
[4, 3, 2, 1, 2, 3],
[5, 4, 3, 2, 1, 2],
[6, 5, 4, 3, 2, 1]], dtype=int32)>
Let’s say input vector is,
`v = [1, 2, 3, 4, 5, 6, 7,…100],
If I want to get a output matrix with number of columns 6, then output is
[ [ 1, 2 , 3, 4, 5, 6 ],
[ 2, 3, 4, 5, 6, 7 ] ,
[ 3, 4, 5, 6, 7, 8 ],
.............,
[ 95, 96, 97, 98, 99,100 ] ]
`
I don’t think tf.linalg.LinearOperatorToeplitz(v, v).to_dense() will give the above output.
I made a mistake. That’s is not a toeplitz matrix.
I am looking for tensorflow equivalent of below method in pytorch
x = torch.arange(1., 8)
x
tensor([ 1., 2., 3., 4., 5., 6., 7.])
x.unfold(0, 2, 1)
tensor([[ 1., 2.],
[ 2., 3.],
[ 3., 4.],
[ 4., 5.],
[ 5., 6.],
[ 6., 7.]])
x.unfold(0, 2, 2)
tensor([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])
For these specific input cases you can use
import tensorflow as tf
x = tf.range(1,8)
out_frame_2_1 = tf.signal.frame(x, 2, 1)
out_frame_2_2 = tf.signal.frame(x, 2, 2)
print(out_frame_2_1)
print(out_frame_2_2)
But more in general for pytorch unfold see
https://github.com/tensorflow/tensorflow/issues/31072
For more complex use cases you could have some performance issues to solve. See:
Thanks. This is what I was looking for.