super() 为何在同一类中使用

huangapple go评论59阅读模式
英文:

why is super() used with in same class

问题

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
英文:
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

答案1

得分: 1

"Super()函数用于访问父类或兄弟类的方法和属性。所以在你的情况下,你从TensorFlow Keras API的Model中继承了AnomalyDetector,它将初始化模型的重要属性,如输入和输出形状。此外,MODEL是Keras API中包含所有模型类的父类。所以通过使用父类model类的super(AnomalyDetector, self).init()方法,它调用了AnomalyDetector子类的所有属性,这使得AnomalyDetector可以继承并使用父类Model类的功能和属性。"

英文:

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.

huangapple
  • 本文由 发表于 2023年4月17日 19:25:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定