【问题标题】:Expected Randomness does not occur in Tensorflow LayerTensorflow 层中不会出现预期的随机性
【发布时间】:2021-11-06 05:26:38
【问题描述】:

我编写了一个自定义层来打乱输入。当我尝试测试图层时,不会发生所说的洗牌。下面是我的最小噪声层:

class ShuffleLayer(tf.keras.layers.Layer):
    def __init__(self, **kwargs):
        super(ShuffleLayer, self).__init__(**kwargs)
        
    def call(self, inputs, training=None):
        if training:
            shuffled = tf.stop_gradient(tf.random.shuffle(inputs))
            return shuffled
        return inputs

当我测试图层时,图层不会随机播放

SL = ShuffleLayer()
x = tf.reshape(tf.range(0,10, dtype=tf.float32), (5,2))
y = SL(x)
print(x.numpy())
print(y.numpy())

[[0. 1.] [2. 3.] [4. 5.] [6. 7.] [8. 9.]]

[[0. 1.] [2. 3.] [4. 5.] [6. 7.][8. 9.]]

为什么不会发生预期的行为?

【问题讨论】:

    标签: tensorflow2.x


    【解决方案1】:

    查看层call,我们看到如果trainingNone,层什么都不做。当层被称为y = SL(x) 时,它会看到trainingNone 并返回输入。通过打开训练参数来获得混洗输出:

    y = SL(x, training=True)
    print(x.numpy())
    print(y.numpy())
    

    [[0. 1.][2. 3.][4. 5.][6. 7.][8. 9.]]

    [[0. 1.][6. 7.][2. 3.][8. 9.][4. 5.]]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 2018-02-07
      • 2021-08-20
      相关资源
      最近更新 更多