Difference between tensorflow Model subclassing

Viewed 42

Let's say I am building my neural network architecture by subclassing tensorflow's Model class. A standard way to do this would be to:

class IdentityBlock(tf.keras.Model):
    def __init__(self, filters, kernel_size, **kwargs):
        super().__init__(**kwargs)
        
        self.conv1 = tf.keras.layers.Conv2D(filters, kernel_size, padding="same")
        self.bn1 = tf.keras.layers.BatchNormalization()
        
        self.act = tf.keras.layers.Activation("relu")
        self.add = tf.keras.layers.Add()
        
    def call(self, input_tensor):
        
        x = self.conv1(input_tensor)
        x = self.bn1(x)
        x = self.act(x)
        x = self.add([x, input_tensor])
        
        return x

Which I understand, we subclass the Model class and any argument not specified has the default value in **kwargs. Now I also saw this option which is confusing me:

class IdentityBlock(tf.keras.Model):
    def __init__(self, filters, kernel_size):
        super(IdentityBlock, self).__init__(name='')
        
        self.conv1 = tf.keras.layers.Conv2D(filters, kernel_size, padding="same")
        self.bn1 = tf.keras.layers.BatchNormalization()
        
        self.act = tf.keras.layers.Activation("relu")
        self.add = tf.keras.layers.Add()
        
    def call(self, input_tensor):
        
        x = self.conv1(input_tensor)
        x = self.bn1(x)
        x = self.act(x)
        x = self.add([x, input_tensor])
        
        return x

Why would we pass IdentityBlock and self in super(), and also not put **kwargs in the __init__ parameters? What is the difference between these two subclassing methods, and if there isn't one, how are these the equivalent?

0 Answers
Related