英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论