无法向Keras层内的权重添加随机噪声。

huangapple go评论59阅读模式
英文:

Can't add random noise to weights inside a Keras Layer

问题

我正在尝试在Keras的卷积层的前向传播中添加随机噪声。我编写了一个包装类,它会在计算卷积之前向权重添加噪声。但是对self.weights的任何添加或修改对最终值没有影响,也没有错误。有人可以帮忙吗?

class Conv2D_New(tf.keras.layers.Conv2D):       
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        
    def call(self, inputs):
        random_noise = tf.random.normal(stddev=0.01, shape=self.weights[0].shape)
        self.weights[0] = self.weights[0] + random_noise
        tf.print(self.weights[0]) ########### 没有变化??? ##################
        return super().call(inputs)
英文:

I am trying to add a random noise during forward pass to a convolutional layer in Keras. I wrote a wrapper class where it would add noise to the weights before computing convolution. Any addition or modifications to self.weights has no effect to the final value. There is no error either. Can someone help ?

class Conv2D_New(tf.keras.layers.Conv2D):       
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        
    def call(self, inputs):
        random_noise = tf.random.normal(stddev=0.01, shape=self.weights[0].shape)
        self.weights[0] = self.weights[0] + random_noise
        tf.print(self.weights[0]) ########### NO CHANGE ???? ##################
        return super().call(inputs)

答案1

得分: 0

如果您查看文档中的示例,您会注意到它使用了Tensor方法,即assign_add。尝试在您的情况下使用相同的方法:

self.weights[0].assign_add(random_noise)
英文:

If you look at the example in the docs you'll notice it uses a Tensor method, assign_add. Try using the same method in your case:

self.weights[0].assign_add(random_noise)

huangapple
  • 本文由 发表于 2023年4月7日 02:47:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定