utils/trainer.py
def _initialize_models(self, learning_rate, weight_decay, load_from, resume_from, device):
        """Initialize models and optimizer(s), and load state dictionaries, if
        possible."""
        # Get model class
        model_class = self.config["model"].get("model_class", None)
        if model_class is None:
            model_class = "BertForDBpediaDocumentClassification"  # default model class
        model_init = model_classes[model_class]
        # Initialize backbone model
        logger.info("Initializing model...")
        from_pretrained = load_from is not None or resume_from is not None
        self.device = torch.device(device)
        self.model = model_init(self.config, from_pretrained=from_pretrained).to(self.device)
        self.tokenizer = self.model.tokenizer

        # Initialize optimizer
        if isinstance(learning_rate, str):
            learning_rate = eval(learning_rate)
        self.optimizer = AdamW(
            [params for params in self.model.parameters()
             if params.requires_grad],
            lr=learning_rate, weight_decay=weight_decay)

        # Load from a pretrained model
        self.start_epoch = 0

        if resume_from is not None:
            # Ensure that the two configs match (with some exclusions)
            with open(os.path.join(self.save_dir, "config.yaml"), "r") as conf:
                resume_config = yaml.load(conf, Loader=yaml.FullLoader)

            # Load the most recent saved model
            model_list = Path(self.save_dir).glob("checkpoint*.pth")
            resume_from = max(
                model_list, key=os.path.getctime)  # last saved model
            logger.info(f"Loading most recent saved model at {resume_from}")
            # Get some more info for resuming training
            _, last_name = os.path.split(resume_from)
            last_name, _ = os.path.splitext(last_name)
            self.start_epoch = int(last_name.split("_")[-1]) + 1

            compare_config = ConfigComparer(self.config, resume_config)
            compare_config.compare()
        if load_from is not None:
            logger.info(f"Loading pretrained model from {load_from}")

        if from_pretrained:
            load_from_path = resume_from if resume_from is not None else load_from

            checkpoint = torch.load(load_from_path, map_location=self.device)
            self.model.load_state_dict(
                checkpoint["model"])
            self.optimizer.load_state_dict(checkpoint["optimizer"])

        self.resume_from = resume_from
        self.load_from = load_from
Initialize models and optimizer(s), and load state dictionaries, if
        possible.
Initialize models and optimizer(s), and load state dictionaries, if
possible.
Initialize models and optimizer ( s ) and load state dictionaries if possible .
def _initialize_models(self, learning_rate, weight_decay, load_from, resume_from, device):
        model_class = self.config["model"].get("model_class", None)
        if model_class is None:
            model_class = "BertForDBpediaDocumentClassification"  
        model_init = model_classes[model_class]
        logger.info("Initializing model...")
        from_pretrained = load_from is not None or resume_from is not None
        self.device = torch.device(device)
        self.model = model_init(self.config, from_pretrained=from_pretrained).to(self.device)
        self.tokenizer = self.model.tokenizer
        if isinstance(learning_rate, str):
            learning_rate = eval(learning_rate)
        self.optimizer = AdamW(
            [params for params in self.model.parameters()
             if params.requires_grad],
            lr=learning_rate, weight_decay=weight_decay)
        self.start_epoch = 0
        if resume_from is not None:
            with open(os.path.join(self.save_dir, "config.yaml"), "r") as conf:
                resume_config = yaml.load(conf, Loader=yaml.FullLoader)
            model_list = Path(self.save_dir).glob("checkpoint*.pth")
            resume_from = max(
                model_list, key=os.path.getctime)  
            logger.info(f"Loading most recent saved model at {resume_from}")
            _, last_name = os.path.split(resume_from)
            last_name, _ = os.path.splitext(last_name)
            self.start_epoch = int(last_name.split("_")[-1]) + 1
            compare_config = ConfigComparer(self.config, resume_config)
            compare_config.compare()
        if load_from is not None:
            logger.info(f"Loading pretrained model from {load_from}")
        if from_pretrained:
            load_from_path = resume_from if resume_from is not None else load_from
            checkpoint = torch.load(load_from_path, map_location=self.device)
            self.model.load_state_dict(
                checkpoint["model"])
            self.optimizer.load_state_dict(checkpoint["optimizer"])
        self.resume_from = resume_from
        self.load_from = load_from
def _initialize_models ( self , learning_rate , weight_decay , load_from , resume_from , device ) : model_class = self . config [ "model" ] . get ( "model_class" , None ) if model_class is None : model_class = "BertForDBpediaDocumentClassification" model_init = model_classes [ model_class ] logger . info ( "Initializing model..." ) from_pretrained = load_from is not None or resume_from is not None self . device = torch . device ( device ) self . model = model_init ( self . config , from_pretrained = from_pretrained ) . to ( self . device ) self . tokenizer = self . model . tokenizer if isinstance ( learning_rate , str ) : learning_rate = eval ( learning_rate ) self . optimizer = AdamW ( [ params for params in self . model . parameters ( ) if params . requires_grad ] , lr = learning_rate , weight_decay = weight_decay ) self . start_epoch = 0 if resume_from is not None : with open ( os . path . join ( self . save_dir , "config.yaml" ) , "r" ) as conf : resume_config = yaml . load ( conf , Loader = yaml . FullLoader ) model_list = Path ( self . save_dir ) . glob ( "checkpoint*.pth" ) resume_from = max ( model_list , key = os . path . getctime ) logger . info ( f"Loading most recent saved model at {resume_from}" ) _ , last_name = os . path . split ( resume_from ) last_name , _ = os . path . splitext ( last_name ) self . start_epoch = int ( last_name . split ( "_" ) [ - 1 ] ) + 1 compare_config = ConfigComparer ( self . config , resume_config ) compare_config . compare ( ) if load_from is not None : logger . info ( f"Loading pretrained model from {load_from}" ) if from_pretrained : load_from_path = resume_from if resume_from is not None else load_from checkpoint = torch . load ( load_from_path , map_location = self . device ) self . model . load_state_dict ( checkpoint [ "model" ] ) self . optimizer . load_state_dict ( checkpoint [ "optimizer" ] ) self . resume_from = resume_from self . load_from = load_from
Initialize models and optimizer(s), and load state dictionaries, if
possible.
Initialize models and optimizer ( s ) and load state dictionaries if possible .
"""Initialize models and optimizer(s), and load state dictionaries, if
        possible.""" # Get model class # default model class # Initialize backbone model # Initialize optimizer # Load from a pretrained model # Ensure that the two configs match (with some exclusions) # Load the most recent saved model # last saved model # Get some more info for resuming training
self
learning_rate
weight_decay
load_from
resume_from
device