使用tf.keras,我有一个Conv2D层,内核大小为5x5。是否可以在将每个5x5补丁送入Conv2D层之前,通过L1范数(或该补丁上的任何其他变换)对其进行归一化?
tf.keras
Conv2D
5x5
7y4bm7vi1#
你可以通过创建一个自定义图层来实现这一点:
class L1NormalizationLayer(tf.keras.layers.Layer): def __init__(self, **kwargs): super(L1NormalizationLayer, self).__init__(**kwargs) def build(self, input_shape): # No trainable weights for this layer super(L1NormalizationLayer, self).build(input_shape) def call(self, inputs): # Calculate L1 norm along the last axis (assuming channels_last data format) l1_norm = tf.reduce_sum(tf.abs(inputs), axis=-1, keepdims=True) # Normalize each patch by dividing by the L1 norm normalized_inputs = inputs / l1_norm return normalized_inputs def compute_output_shape(self, input_shape): return input_shape
字符串
1条答案
按热度按时间7y4bm7vi1#
你可以通过创建一个自定义图层来实现这一点:
字符串