The Scaling block represents a simple multiplication of each input by the same user-defined scalar value. You can also add an offset.

\[output = input * scale + offset\]


Use, e.g., when scaling the residual branch of residual connections as in the network ResNet.

Example code when downloading an .h5-model

If your model includes a Scaling block and you want to download an .h5-model, you can use the additional code below. You need to do this since the .h5-format comes from Keras, and Keras doesn’t support the Scaling block, so some extra code is needed.

import tensorflow as tf

class Scaling(tf.keras.layers.Layer):

    def __init__(self, scale: float, offset: float, **kwargs):
        self._scale = scale
        self._offset = offset

    def call(self, inputs):
        return inputs * self._scale + self._offset

    def get_config(self):
        config = super().get_config()
        config['scale'] = self._scale
        config['offset'] = self._offset
        return config

model = tf.keras.models.load_model('my_model.h5', custom_objects={'Scaling': Scaling})


Scale: The real number used to multiply each input.
Default: 0.5.

Offset: The offset value is added to each input.
Default: 0.

Was this page helpful?
Yes No