英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论