graphinvent/Workflow.py
def evaluate_model(self, model_to_evaluate : torch.nn.Module,
                       label : str="") -> Union[float, None]:
        """
        Evaluates model.

        For regular training jobs, evaluates the model every `sample_every`
        epochs by calculating the UC-JSD from generated structures. Saves model
        scores in `validation.log` and then saves model state.

        For fine-tuning jobs, evaluates the model every time function is called
        (i.e., every fine-tuning step) by computing the score of molecules
        generated by the specified model.

        Args:
        ----
            model_to_evaluate (torch.nn.Module) : Specific model to evaluate
                                                  (e.g. SummationMPNN).
            label (str)                         : Label to use for saving generated
                                                  structures from a specific graph
                                                  generation step.

        Returns:
        -------
            score (float) : Model score for fine-tuning job, otherwise simply None.
        """
        if self.constants.job_type == "fine-tune":
            model_to_evaluate.eval()  # sets layers to eval mode (e.g. norm, dropout)
            with torch.no_grad():     # deactivates autograd engine

                _, score = self.generate_graphs_rl(
                    model_a=model_to_evaluate,
                    model_b=self.prior_model,
                    is_agent=True,
                    model_a_label=label
                )

                print(f"* Saving model state at Epoch {self.current_epoch}.",
                      flush=True)
                # `pickle.HIGHEST_PROTOCOL` good for large objects
                model_path = (f"{self.constants.job_dir}"
                              f"model_restart_{self.current_epoch}.pth")
                torch.save(obj=model_to_evaluate.state_dict(),
                           f=model_path,
                           pickle_protocol=pickle.HIGHEST_PROTOCOL)

        elif self.current_epoch % self.constants.sample_every == 0:
            model_to_evaluate.eval()  # sets layers to eval mode (e.g. norm, dropout)
            with torch.no_grad():     # deactivates autograd engine
                self.generate_graphs(n_samples=self.constants.n_samples,
                                     evaluation=True)
                score = None

                print(f"* Saving model state at Epoch {self.current_epoch}.",
                      flush=True)
                # `pickle.HIGHEST_PROTOCOL` good for large objects
                model_path = (f"{self.constants.job_dir}"
                              f"model_restart_{self.current_epoch}.pth")
                torch.save(obj=model_to_evaluate.state_dict(),
                           f=model_path,
                           pickle_protocol=pickle.HIGHEST_PROTOCOL)

                print("* Evaluating model.", flush=True)
                self.analyzer.model = model_to_evaluate
                self.analyzer.evaluate_model(
                    likelihood_per_action=self.likelihood_per_action
                )

        else:
            # score not computer, so use placeholder
            util.write_training_status(score="NA")
            score = None

        return score

        Evaluates model.

        For regular training jobs, evaluates the model every `sample_every`
        epochs by calculating the UC-JSD from generated structures. Saves model
        scores in `validation.log` and then saves model state.

        For fine-tuning jobs, evaluates the model every time function is called
        (i.e., every fine-tuning step) by computing the score of molecules
        generated by the specified model.

        Args:
        ----
            model_to_evaluate (torch.nn.Module) : Specific model to evaluate
                                                  (e.g. SummationMPNN).
            label (str)                         : Label to use for saving generated
                                                  structures from a specific graph
                                                  generation step.

        Returns:
        -------
            score (float) : Model score for fine-tuning job, otherwise simply None.
        
Evaluates model.
For regular training jobs, evaluates the model every `sample_every`
epochs by calculating the UC-JSD from generated structures. Saves model
scores in `validation.log` and then saves model state.

For fine-tuning jobs, evaluates the model every time function is called
by computing the score of molecules
generated by the specified model.
Evaluates model . For regular training jobs evaluates the model every ` sample_every ` epochs by calculating the UC - JSD from generated structures . Saves model scores in ` validation . log ` and then saves model state . For fine - tuning jobs evaluates the model every time function is called by computing the score of molecules generated by the specified model .
def evaluate_model(self, model_to_evaluate : torch.nn.Module,
                       label : str="") -> Union[float, None]:
        if self.constants.job_type == "fine-tune":
            model_to_evaluate.eval()  
            with torch.no_grad():     
                _, score = self.generate_graphs_rl(
                    model_a=model_to_evaluate,
                    model_b=self.prior_model,
                    is_agent=True,
                    model_a_label=label
                )
                print(f"* Saving model state at Epoch {self.current_epoch}.",
                      flush=True)
                model_path = (f"{self.constants.job_dir}"
                              f"model_restart_{self.current_epoch}.pth")
                torch.save(obj=model_to_evaluate.state_dict(),
                           f=model_path,
                           pickle_protocol=pickle.HIGHEST_PROTOCOL)
        elif self.current_epoch % self.constants.sample_every == 0:
            model_to_evaluate.eval()  
            with torch.no_grad():     
                self.generate_graphs(n_samples=self.constants.n_samples,
                                     evaluation=True)
                score = None
                print(f"* Saving model state at Epoch {self.current_epoch}.",
                      flush=True)
                model_path = (f"{self.constants.job_dir}"
                              f"model_restart_{self.current_epoch}.pth")
                torch.save(obj=model_to_evaluate.state_dict(),
                           f=model_path,
                           pickle_protocol=pickle.HIGHEST_PROTOCOL)
                print("* Evaluating model.", flush=True)
                self.analyzer.model = model_to_evaluate
                self.analyzer.evaluate_model(
                    likelihood_per_action=self.likelihood_per_action
                )
        else:
            util.write_training_status(score="NA")
            score = None
        return score
def evaluate_model ( self , model_to_evaluate : torch . nn . Module , label : str = "" ) -> Union [ float , None ] : if self . constants . job_type == "fine-tune" : model_to_evaluate . eval ( ) with torch . no_grad ( ) : _ , score = self . generate_graphs_rl ( model_a = model_to_evaluate , model_b = self . prior_model , is_agent = True , model_a_label = label ) print ( f"* Saving model state at Epoch {self.current_epoch}." , flush = True ) model_path = ( f"{self.constants.job_dir}" f"model_restart_{self.current_epoch}.pth" ) torch . save ( obj = model_to_evaluate . state_dict ( ) , f = model_path , pickle_protocol = pickle . HIGHEST_PROTOCOL ) elif self . current_epoch % self . constants . sample_every == 0 : model_to_evaluate . eval ( ) with torch . no_grad ( ) : self . generate_graphs ( n_samples = self . constants . n_samples , evaluation = True ) score = None print ( f"* Saving model state at Epoch {self.current_epoch}." , flush = True ) model_path = ( f"{self.constants.job_dir}" f"model_restart_{self.current_epoch}.pth" ) torch . save ( obj = model_to_evaluate . state_dict ( ) , f = model_path , pickle_protocol = pickle . HIGHEST_PROTOCOL ) print ( "* Evaluating model." , flush = True ) self . analyzer . model = model_to_evaluate self . analyzer . evaluate_model ( likelihood_per_action = self . likelihood_per_action ) else : util . write_training_status ( score = "NA" ) score = None return score
Evaluates model.
Evaluates model .
"""
        Evaluates model.

        For regular training jobs, evaluates the model every `sample_every`
        epochs by calculating the UC-JSD from generated structures. Saves model
        scores in `validation.log` and then saves model state.

        For fine-tuning jobs, evaluates the model every time function is called
        (i.e., every fine-tuning step) by computing the score of molecules
        generated by the specified model.

        Args:
        ----
            model_to_evaluate (torch.nn.Module) : Specific model to evaluate
                                                  (e.g. SummationMPNN).
            label (str)                         : Label to use for saving generated
                                                  structures from a specific graph
                                                  generation step.

        Returns:
        -------
            score (float) : Model score for fine-tuning job, otherwise simply None.
        """ # sets layers to eval mode (e.g. norm, dropout) # deactivates autograd engine # `pickle.HIGHEST_PROTOCOL` good for large objects # sets layers to eval mode (e.g. norm, dropout) # deactivates autograd engine # `pickle.HIGHEST_PROTOCOL` good for large objects # score not computer, so use placeholder
score (float) : Model score for fine-tuning job, otherwise simply None.
score ( float ) : Model score for fine - tuning job otherwise simply None .
self
model_to_evaluate
torch.nn.Module
Specific model to evaluate
.
Specific model to evaluate .
label
str
Label to use for saving generated
structures from a specific graph
generation step.
Label to use for saving generated structures from a specific graph generation step .