-1
class AnomalyDetector(Model):
  def __init__(self):
    super(AnomalyDetector, self).__init__()
    self.encoder = tf.keras.Sequential([
      layers.Dense(64, activation="relu"),
      layers.Dense(32, activation="relu"),
      layers.Dense(16, activation="relu"),
      layers.Dense(8, activation="relu")]) 
    
    self.decoder = tf.keras.Sequential([
      layers.Dense(16, activation="relu"),
      layers.Dense(32, activation="relu"),
      layers.Dense(64, activation="relu"),
      layers.Dense(140, activation="sigmoid")])
    
  def call(self, x):
    encoded = self.encoder(x)
    decoded = self.decoder(encoded)
    return decoded

i learnt that we use super() to call a parent class method inside a child class . but in this case there is nothing like parent and child , its only one class . please help me understand this code entirely.

i was not able to understand why is super() used in this

hasan
  • 11
  • 2
    Well, there's `AnomalyDetector` and its parent `Model`. – Klaus D. Apr 17 '23 at 11:27
  • i don't understand , can you please elaborate . the arguement " model " in class autoencoder( model ) is nothing its just a parameter , it can be replace with x or y or z or any – hasan Apr 17 '23 at 11:29
  • 1
    The very first line of your code says `class AnomalyDetector(Model)`. That means you're creating a child class named AnomalyDetector, whose parent is named Model. – slothrop Apr 17 '23 at 11:31
  • brother , this the only class in the model , there is not any other class , " Model " is just an arguement it can also be X or Y or Z – hasan Apr 17 '23 at 11:37
  • 3
    You seem to have a misconception on how inheritance works in Python. The "argument" to a class is its parent class. There can be multiple. – Klaus D. Apr 17 '23 at 11:38
  • @hasan check out the docs: https://docs.python.org/3/tutorial/classes.html#inheritance. Note the syntax `class DerivedClassName(BaseClassName):` ... "derived" means "child class" and "base" means "parent class". – slothrop Apr 17 '23 at 11:43
  • brother thanks , i appreciate the help , i now understod it . i thought "Model" is just a random arguement which can be X or Y or Z but its a tensorflow API – hasan Apr 17 '23 at 11:43

1 Answers1

1

The Super() function is used to give access to the methods and properties of a parent or sibling class.

So in your case, you are inheriting the AnomalyDetector from the Model from TensorFlow Keras API that will initialize important attributes such as the input and output shapes of the model.

Further MODEL is a parent class in Keras API that contains all the model classes. So by using the super(AnomalyDetector, self).init() method of the parent model class it calls all the attributes from the AnomalyDetector subclass and this allows AnomalyDetector to inherit and use the functionality and attributes of the parent Model class.