Reputation: 23
I'm trying to slice the tensor into small ones as long as there's still some columns using tf.while_loop
.
Note : I'm using this way because I cannot loop over a value in a placeholder at the graph construction time ( without session ) considered as a tensor and not integer.
[ 5 7 8 ]
[ 7 4 1 ] =>
[5 7 ] [ 7 8 ]
[7 4 ] [ 4 1 ]
This is my code:
i = tf.constant(0)
result = tf.subtract(tf.shape(f)[1],1)
c = lambda result : tf.greater(result, 0)
b = lambda i: [i+1, tf.slice(x, [0,i],[2, 3])]
o= tf.while_loop(c, b,[i])
with tf.Session() as sess:
print (sess.run(o))
However, I get this error :
ValueError: The two structures don't have the same nested structure.
First structure: type=list str=[<tf.Tensor 'while_2/Identity:0' shape=() dtype=int32>]
Second structure: type=list str=[<tf.Tensor 'while_2/add:0' shape=() dtype=int32>, <tf.Tensor 'while_2/Slice:0' shape=(2, 3) dtype=int32>]
I would like to return the sub tensor everytime
Upvotes: 1
Views: 743
Reputation: 15119
There are several problems with your code:
You are not passing any structure/tensor to receive the values of your tf.slice(...)
. Your lambda
b
should have a signature such as lambda i, res : i+1, ...
Tensors edited through a tf.while_loop
should have a fixed shape. If you want to build a loop to collect slices, then you should first initialize the tensor res
with the appropriate shape to contain all the slice values, e.g. res = tf.zeros([result, 2, 2])
.
Note: Regarding your particular application (collecting all pairs of neighbor columns), this could be done without a tf.while_loop
:
import tensorflow as tf
x = tf.convert_to_tensor([[ 5, 7, 8, 9 ],
[ 7, 4, 1, 0 ]])
num_rows, num_cols = tf.shape(x)[0], tf.shape(x)[1]
# Building tuples of neighbor column indices:
n = 2 # or 5 cf. comment
idx_neighbor_cols = [tf.range(i, num_cols - n + i) for i in range(n)]
idx_neighbor_cols = tf.stack(idx_neighbor_cols, axis=-1)
# Finally gathering the column pairs accordingly:
res = tf.transpose(tf.gather(x, idx_neighbor_cols, axis=1), [1, 0, 2])
with tf.Session() as sess:
print(sess.run(res))
# [[[5 7]
# [7 4]]
# [[7 8]
# [4 1]]
# [[8 9]
# [1 0]]]
Upvotes: 1