Reputation: 367
I have a tensor with inside some integer value, i need to create a new tensor with all the value from the integer value*49
to value*49+49
.
Example: tf.random.uniform()
generates values [0,1]
Then i need a new tensor with values: `[0,1,2,3,..48, 98, 99, ..., 147]
But if i do something like this:
indices = tf.constant([], tf.int32)
for index in tf.random.uniform((51,), minval=0, maxval=62, dtype=tf.dtypes.int32):
tensor1 = tf.constant([x for x in range(index * 49, index * 49 + 49)])
indices = tf.concat([indices, tensor1], 0)
temp = tf.gather(huge_tensor, indices, axis=0)
I get this error:
OperatorNotAllowedInGraphError: iterating over tf.Tensor is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.
I need to do something like this because i will later use tf.gather
to gather from another huge tensor, all the rows using the indices
tensor. If there is a way to tell tf.gather
to grab all the rows directly instead of creating the new tensor indices
is even better.
Upvotes: 0
Views: 652
Reputation: 26718
You could try using tf.TensorArray
and tf.range
:
import tensorflow as tf
x = tf.random.uniform((51,), minval=0, maxval=62, dtype=tf.dtypes.int32)
indices = tf.TensorArray(tf.int32, size=0, dynamic_size=True)
huge_tensor = tf.random.normal((3136, 512))
for i in tf.range(tf.shape(x)[0]):
indices = indices.write(indices.size(), tf.range(x[i] * 49, x[i] * 49 + 49))
tf.print(indices.stack().shape)
result = tf.gather(huge_tensor, indices.stack(), axis=0)
result_shape = tf.shape(result)
result = tf.reshape(result, (result_shape[0] * result_shape[1], result_shape[2]))
tf.print(result.shape)
TensorShape([51, 49])
TensorShape([2499, 512])
Using tf.range
, I create 1D sequences based on each value in x. Then I apply stack()
to the array indices
, which returns the sequences as a stacked tensor.
Upvotes: 1