So I wanna return a list of object in tensorflow, the list object is constructed using tensor X with size [n,k,d] when i tried:
listofobject = [Myobject(x) for x in X]
it returns:
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 also tried:
listofobject = tf.map_fn(lambda x:Myobject(x), X)
and got this error:
ValueError: Tried to convert 'y' to a tensor and failed. Error: None values not supported.
when I check inside the object's constructor, at some point my x shape is [None, d] not sure what I am missing here. My question is, what is the proper way to return a list of objects like this in tensorflow?
CodePudding user response:
This works for me,
import tensorflow as tf
class Myobject:
def __init__(self, x):
self.x = x
X = tf.random.uniform(shape = (3, 2 ,1))
listofobject = [Myobject(x) for x in X]
print(listofobject[1].x)
<tf.Tensor: shape=(2, 1), dtype=float32, numpy=
array([[0.09055722],
[0.7195903 ]], dtype=float32)>
CodePudding user response:
Could you display more detail on why you need it to.
Two Methods:
tf.ragged.constant([Myobject(x) for x in X])
2:
tf.map_fn(Myobject,elems=X,fn_ouput_signature=tf.RaggedTensorSpec(dtype=tf.float32orWHATEVER))
Because it is ragged that is the problem or the values are placeholders could you please alboarate.