无法构建在参差不齐的张量上循环的 Tensorflow 自定义层

Tesnorflow custom layer that loops over ragged tensor cannot be built

我正在尝试在 tensorflow 中自定义一个层。该层必须将长度不明的参差不齐的 tesnor 作为输入。但是在尝试构建层时代码卡住了。即使是下面附带的简单代码也无法正常工作。

import tensorflow as tf
class myLayer(tf.keras.layers.Layer):
    def __init__(self):
        super(myLayer, self).__init__()
        self._supports_ragged_inputs = True


    def call(self, inputs):
        # Try to loop over ragged tensor
        for x in inputs:
            pass
        return tf.constant(0)

# Input is ragged tensor
inputs = tf.keras.layers.Input(shape=(None, 1), ragged=True)

layer1 = myLayer()
output = layer1(inputs)

当我 运行 你在 Tensorflow version 2.2.0 中的代码时,我在 for 循环中得到以下错误 -

错误-

ValueError: in user code:

    <ipython-input-24-1681d59017fc>:10 call  *
        for x in inputs:
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/operators/control_flow.py:359 for_stmt
        iter_, extra_test, body, get_state, set_state, symbol_names, opts)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/operators/control_flow.py:491 _tf_ragged_for_stmt
        opts)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/operators/control_flow.py:885 _tf_while_stmt
        aug_test, aug_body, init_vars, **opts)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/control_flow_ops.py:2688 while_loop
        back_prop=back_prop)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/while_v2.py:104 while_loop
        maximum_iterations)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/while_v2.py:1258 _build_maximum_iterations_loop_var
        maximum_iterations, dtype=dtypes.int32, name="maximum_iterations")
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1317 convert_to_tensor
        (dtype.name, value.dtype.name, value))

    ValueError: Tensor conversion requested dtype int32 for Tensor with dtype int64: <tf.Tensor 'my_layer_15/strided_slice:0' shape=() dtype=int64>

所以我刚刚进行了以下实验,以了解使用 inputsfor 循环和 enumerate 产生的数据类型。 for 循环生成 tensor class 而 enumerate 生成 int class.

实验代码-

inputs = tf.keras.layers.Input(shape=(None, 1), ragged=True)

for x in inputs:
  print(type(x))
  break

for i,x in enumerate(inputs):
  print(type(i))
  break

输出-

<class 'tensorflow.python.framework.ops.Tensor'>
<class 'int'>

所以我修改了你的代码如下,它工作正常 -

固定代码-

import tensorflow as tf
class myLayer(tf.keras.layers.Layer):
    def __init__(self):
        super(myLayer, self).__init__()
        self._supports_ragged_inputs = True


    def call(self, inputs):
        # Try to loop over ragged tensor
        # for x in inputs:  # Throws Error
        for i,x in enumerate(inputs): #Enumerate Works fine
          break                       #Using break as pass will go into loop 
        return tf.constant(0)

# Input is ragged tensor
inputs = tf.keras.layers.Input(shape=(None, 1), ragged=True)

layer1 = myLayer()
output = layer1(inputs)
print(output)

输出-

Tensor("my_layer_17/Identity:0", shape=(), dtype=int32)

希望这能回答您的问题。快乐学习。