在其__init__方法中引用类时出错

时间:2019-07-11 16:51:04

标签: python python-3.x oop tensorflow machine-translation

我正在将(NMT Tensorflow code)放在主类中。代码库有两个类-“编码器”和“解码器”。它们在各自的“ init”方法中被引用。但是,这会引发错误-“未定义的命名编码器”。

class TranslationModel(ModelBase):



    pathToZip = tf.keras.utils.get_file('spa-eng.zip', origin='http://download.tensorflow.org/data/spa-eng.zip', extract=True)

    pathToFile = os.path.dirname(pathToZip)+"/spa-eng/spa.txt"


    def __init__(self,
                 batchSize = 64,
                 bufferSize = None,
                 numberOfBatches = None,
                 units = 1024,
                 vocabInputSize = None,
                 vocabTargetSize = None,
                 optimizer = tf.train.AdamOptimizer(),
                 dataSetPath = None,
                 inputTensor = None,
                 targetTensor = None,
                 inputLanguage = None,
                 targetLanguage = None,
                 maxLengthInput = None,
                 maxLengthTarget = None,
                 embeddingDimension  = 256, *arg, **kwargs):
        self.batchSize          = 64
        self.bufferSize         = None
        self.numberOfBatches    = None
        self.units              = units
        self.vocabInputSize     = None
        self.vocabTargetSize    = None
        self.optimizer          = optimizer
        self.dataSetPath        = dataSetPath
        self.targetTensor       = targetTensor
        self.inputTensor        = inputTensor
        self.inputLanguage      = inputLanguage
        self.targetLanguage     = targetLanguage
        self.maxLengthInput     = maxLengthInput
        self.maxLengthTarget    = maxLengthTarget
        self.embeddingDimension = embeddingDimension
        super().__init__(*arg, **kwargs)

        #OTHER FUNCTIONS HERE

    class Encoder(tf.keras.Model):
        def __init__(self, vocabSize, embeddingDimension, encoderUnits, batchSize):
            super(Encoder, self).__init__() # Raises error - 'Undefined named Encoder'
        #Other code here



    class Decoder(tf.keras.Model):
        def __init__(self, vocabSize, embeddingDimension, dec_units, batchSize):
            super('Decoder', self).__init__() # Raises error - 'Undefined named Decoder'
        ## Other code

1 个答案:

答案 0 :(得分:0)

这是因为当您在另一个类中有一个类并且要标识它时,您应该采用这种方式:OutterClass.InnerClass

如果仅使用InnerClass则不起作用

在您的情况下为TranslationModel.Encoder