如何在 TensorFlow 中将“for 循环”的迭代器范围作为 keras 输入层传递?

How to pass iterator range for `for loop` as keras input layer in TensorFlow?

我想创建一个接受多个输入的模型,其中一个输入是自定义层中循环必须 运行 的次数,示例实现如下:

import tensorflow as tf

class TrialLayer(tf.keras.layers.Layer):
    def __init__(self):
        super().__init__()
        self.d = tf.Variable(2.0)

    def call(self, a, b,c):
        e = 0.0
        # iterator = tf.shape(tf.range(c)) # fails
        for i in range(c):
            e = e + a+b+self.d
        return e
# =============================================================================

input_a = tf.keras.layers.Input(shape=(1), dtype=tf.float32)
input_b = tf.keras.layers.Input(shape=(1), dtype=tf.float32)
input_c = tf.keras.layers.Input(shape=(1), dtype=tf.int32)

tl = TrialLayer()(input_a, input_b, input_c)

model = tf.keras.models.Model(inputs=[input_a,input_b,input_c], outputs=tl)

print(model([2.0,3.0,4]))

这给出了错误

 ValueError: Shape must be rank 0 but is rank 2
         for 'limit' for '{{node trial_layer_1/range}} = Range[Tidx=DT_INT32](trial_layer_1/range/start, trial_layer_1/Maximum, trial_layer_1/range/delta)' with input shapes: [], [?,1], [].

如何将迭代器值作为输入传递?

也许可以尝试将 tf.Variabletf.while_loop 结合使用,如下所示:

import tensorflow as tf

class TrialLayer(tf.keras.layers.Layer):
    def __init__(self):
        super().__init__()
        self.d = tf.Variable(2.0)

    def call(self, a, b, c):
        e = tf.Variable(0.0, shape=tf.TensorShape(None))
        i = tf.constant(0)
        while_condition = lambda i: tf.math.less_equal(i, c)
        def body(i):
            e.assign_add(a+b+self.d)
            return [tf.add(i, 1)]
        _ = tf.while_loop(while_condition, body, [i])
  
        return e

input_a = tf.keras.layers.Input(shape=(1), dtype=tf.float32)
input_b = tf.keras.layers.Input(shape=(1), dtype=tf.float32)
input_c = tf.keras.layers.Input(shape=(1), dtype=tf.int32)

tl = TrialLayer()(input_a, input_b, input_c)

model = tf.keras.models.Model(inputs=[input_a,input_b,input_c], outputs=tl)

tf.print(model([2.0,3.0, 4]))
# 35

您也可以将条件更改为 tf.math.less(i, c) 并得到 28 作为您的输出。