(self)
Setup ASR model and optimizer
Setup ASR model and optimizer
Setup ASR model and optimizer
def set_model(self):
        ''' Setup ASR model and optimizer '''
        # Model
        init_adadelta = self.config['hparas']['optimizer'] == 'Adadelta'
        self.model = ASR(self.feat_dim, self.vocab_size, init_adadelta, **
                         self.config['model']).to(self.device)
        self.verbose(self.model.create_msg())
        model_paras = [{'params': self.model.parameters()}]

        # Losses
        self.seq_loss = torch.nn.CrossEntropyLoss(ignore_index=0)
        # Note: zero_infinity=False is unstable?
        self.ctc_loss = torch.nn.CTCLoss(blank=0, zero_infinity=False)

        # Plug-ins
        self.emb_fuse = False
        self.emb_reg = ('emb' in self.config) and (
            self.config['emb']['enable'])
        if self.emb_reg:
            from src.plugin import EmbeddingRegularizer
            self.emb_decoder = EmbeddingRegularizer(
                self.tokenizer, self.model.dec_dim, **self.config['emb']).to(self.device)
            model_paras.append({'params': self.emb_decoder.parameters()})
            self.emb_fuse = self.emb_decoder.apply_fuse
            if self.emb_fuse:
                self.seq_loss = torch.nn.NLLLoss(ignore_index=0)
            self.verbose(self.emb_decoder.create_msg())

        # Optimizer
        self.optimizer = Optimizer(model_paras, **self.config['hparas'])
        self.verbose(self.optimizer.create_msg())

        # Enable AMP if needed
        self.enable_apex()

        # Automatically load pre-trained model if self.paras.load is given
        self.load_ckpt()
def set_model ( self ) : # Model init_adadelta = self . config [ 'hparas' ] [ 'optimizer' ] == 'Adadelta' self . model = ASR ( self . feat_dim , self . vocab_size , init_adadelta , * * self . config [ 'model' ] ) . to ( self . device ) self . verbose ( self . model . create_msg ( ) ) model_paras = [ { 'params' : self . model . parameters ( ) } ] # Losses self . seq_loss = torch . nn . CrossEntropyLoss ( ignore_index = 0 ) # Note: zero_infinity=False is unstable? self . ctc_loss = torch . nn . CTCLoss ( blank = 0 , zero_infinity = False ) # Plug-ins self . emb_fuse = False self . emb_reg = ( 'emb' in self . config ) and ( self . config [ 'emb' ] [ 'enable' ] ) if self . emb_reg : from src . plugin import EmbeddingRegularizer self . emb_decoder = EmbeddingRegularizer ( self . tokenizer , self . model . dec_dim , * * self . config [ 'emb' ] ) . to ( self . device ) model_paras . append ( { 'params' : self . emb_decoder . parameters ( ) } ) self . emb_fuse = self . emb_decoder . apply_fuse if self . emb_fuse : self . seq_loss = torch . nn . NLLLoss ( ignore_index = 0 ) self . verbose ( self . emb_decoder . create_msg ( ) ) # Optimizer self . optimizer = Optimizer ( model_paras , * * self . config [ 'hparas' ] ) self . verbose ( self . optimizer . create_msg ( ) ) # Enable AMP if needed self . enable_apex ( ) # Automatically load pre-trained model if self.paras.load is given self . load_ckpt ( )