简体   繁体   English

Tensorflow tf.map_fn 在不规则张量上失败,类型为“RaggedTensor”的对象没有 len

[英]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.

相关问题 tensorflow api 2.0张量对象只有在启用了eager执行时才可迭代。 要迭代此张量,请使用tf.map_fn - tensorflow api 2.0 tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn Tensorflow 错误。 TypeError:张量对象仅在启用急切执行时才可迭代。 要迭代此张量,请使用 tf.map_fn - Tensorflow error. TypeError: Tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn tensorflow:在tf.map_fn的fn中创建变量返回值错误 - tensorflow: creating variables in fn of tf.map_fn returns value error 'map' 类型的对象在 Python 3 中没有 len() - Object of type 'map' has no len() in Python 3 信号值张量上的tensorflow map_fn - tensorflow map_fn on signle value Tensor TypeError:“地图”类型的对象没有len()Python3 - TypeError: object of type 'map' has no len() Python3 Map 对象在 Python 3 中没有 len() - Map object has no len() in Python 3 Python 3.5 TypeError上的Gensim 1.0.1:类型为“ map”的对象没有len()吗? - Gensim 1.0.1 on Python 3.5 TypeError: object of type 'map' has no len()? 对象'NewsletterUsuario'的类型没有len() - type of object 'NewsletterUsuario' has no len() 'slice'类型的对象没有len()吗? - object of type 'slice' has no len()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM