【问题标题】:如何将`for loop`的迭代器范围作为TensorFlow中的keras输入层传递?
【发布时间】:2022-01-23 08:17:45
【问题描述】:

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

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

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

【问题讨论】:

    标签: python tensorflow keras tensorflow2.0


    【解决方案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 作为您的输出。

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 2019-09-15
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2019-07-31
      • 2021-06-18
      • 1970-01-01
      • 2011-04-28
      相关资源
      最近更新 更多