[英]Tensorflow tf.map_fn over ragged tensor fails with object of type 'RaggedTensor' has no len
This Tensorflow doc gives this example of using tf.map_fn
on ragged tensors which works for Tensorflow 2.4.1 and above:这个 Tensorflow 文档给出了在不规则张量上使用
tf.map_fn
示例,该张量适用于 Tensorflow 2.4.1 及更高版本:
digits = tf.ragged.constant([[3, 1, 4, 1], [], [5, 9, 2], [6], []])
print(tf.map_fn(tf.math.square, digits))
However the following example results in error "object of type 'RaggedTensor' has no len" when run in Tensorflow 2.4.1 or Tensorflow 2.5:但是,在 Tensorflow 2.4.1 或 Tensorflow 2.5 中运行时,以下示例会导致错误“'RaggedTensor' 类型的对象没有 len”:
import tensorflow as tf
X=tf.ragged.constant([[1.,2.],[3.,4.,5.]], dtype=tf.float32)
@tf.function
def powerX(i):
global X
return X**i
Y = tf.map_fn(powerX, tf.range(3, dtype=tf.float32))
Is there a way to make this work?有没有办法使这项工作? I don't understand the error being thrown.
我不明白抛出的错误。 In general I am trying to get full parallelism by mapping a user defined function which has only Tensorflow operations over a ragged tensor with results being ragged tensors.
一般来说,我试图通过映射一个用户定义的函数来获得完全并行性,该函数在一个参差不齐的张量上只有 Tensorflow 操作,结果是参差不齐的张量。
tf.map_fn
requires an output signature. tf.map_fn
需要输出签名。 I'm not sure why it can't infer this from the input but that's a question for the tensorflow people.我不确定为什么它不能从输入中推断出这一点,但这是 tensorflow 人员的问题。 The following code will work for you.
以下代码将为您工作。
import tensorflow as tf
X=tf.ragged.constant([[1.,2.],[3.,4.,5.]], dtype=tf.float32)
@tf.function
def powerX(i):
global X
return X**i
signature = tf.type_spec_from_value(powerX(X))
Y = tf.map_fn(powerX, tf.range(3, dtype=tf.float32),fn_output_signature=signature)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.