simpletransformers/seq2seq/seq2seq_model.py
def predict(self, pred_data, output_dir=None, suffix=None, verbose=True, silent=False):
        """
        Performs predictions on a list of text.
        Args:
            pred_data: Pandas DataFrame containing the 2 columns - `input_text`, `target_text`.
                        - `input_text`: The input text sequence.
                        - `target_text`: The target text sequence.            
            output_dir: The directory where predictition results files will be saved. If not given, self.args.output_dir will be used.
            suffix: The supplementary suffix of prediction results name.
        Returns:
            preds: A python list of the generated sequences.
        """  # noqa: ignore flake8"

        to_predict = pred_data["input_text"].tolist()
        target_predict = pred_data["target_text"].tolist()

        assert len(to_predict)==len(target_predict)

        self._move_model_to_device()

        if not output_dir:
            output_dir = self.args.output_dir

        self._move_model_to_device()

        os.makedirs(output_dir, exist_ok=True)


        all_outputs = []
        # Batching
        for batch in tqdm([to_predict[i : i + self.args.eval_batch_size] for i in range(0, len(to_predict), self.args.eval_batch_size)],
                          desc='Predicting', disable=self.args.silent, mininterval=0,):
            if self.args.model_type == "marian":
                input_ids = self.encoder_tokenizer.prepare_translation_batch(
                    batch,
                    max_length=self.args.max_seq_length,
                    padding=True,
                    return_tensors="pt",
                    truncation=True,
                )["input_ids"]
            else:
                input_ids = self.encoder_tokenizer.batch_encode_plus(
                    batch,
                    max_length=self.args.max_seq_length,
                    padding=True,
                    return_tensors="pt",
                    truncation=True,
                )["input_ids"]
            input_ids = input_ids.to(self.device)

            if self.args.model_type in ["bart", "marian"]:
                outputs = self.model.generate(
                    input_ids=input_ids,
                    num_beams=self.args.num_beams,
                    max_length=self.args.max_length,
                    length_penalty=self.args.length_penalty,
                    early_stopping=self.args.early_stopping,
                    repetition_penalty=self.args.repetition_penalty,
                    do_sample=self.args.do_sample,
                    top_k=self.args.top_k,
                    top_p=self.args.top_p,
                    num_return_sequences=self.args.num_return_sequences,
                )
            else:
                outputs = self.model.generate(
                    input_ids=input_ids,
                    decoder_start_token_id=self.model.config.decoder.pad_token_id,
                    num_beams=self.args.num_beams,
                    max_length=self.args.max_length,
                    length_penalty=self.args.length_penalty,
                    early_stopping=self.args.early_stopping,
                    repetition_penalty=self.args.repetition_penalty,
                    do_sample=self.args.do_sample,
                    top_k=self.args.top_k,
                    top_p=self.args.top_p,
                    num_return_sequences=self.args.num_return_sequences,
                )

            all_outputs.extend(outputs.cpu().numpy())

        if self.args.use_multiprocessed_decoding:
            self.model.to("cpu")
            with Pool(self.args.process_count) as p:
                outputs = list(
                    tqdm(
                        p.imap(self._decode, all_outputs, chunksize=self.args.multiprocessing_chunksize),
                        total=len(all_outputs),
                        desc="Decoding outputs",
                        disable=self.args.silent,
                    )
                )
            self._move_model_to_device()
        else:
            outputs = [
                self.decoder_tokenizer.decode(output_id, skip_special_tokens=True, clean_up_tokenization_spaces=True)
                for output_id in all_outputs
            ]

        output_predication_file = os.path.join(output_dir, "predictions_{}.txt".format(suffix))
        correct_num = 0
        accuracy_list = []
        with open(output_predication_file, "w", encoding="utf8", errors="ignore") as writer:
            writer.write("to_predict\n\toutput\n\ttarget\n")
            for i in range(len(outputs)):
                outputs[i] = outputs[i].strip()
                writer.write(to_predict[i]+"\t"+outputs[i]+"\n\t"+target_predict[i])
                if self.args.evaluation_metric == 'passage':
                    ith_accuracy = self.passage_predict_accuray(to_predict[i], outputs[i], target_predict[i], 10)
                    ith_bleu = corpus_bleu(sys_stream=outputs[i], ref_streams=target_predict[i]).score
                    if ith_accuracy < 0:
                        pass
                    else:
                        accuracy_list.append(ith_accuracy)
                    print("number :{} accuracy:{} bleu :{}".format(i, ith_accuracy, ith_bleu))
                    writer.write("number :{} accuracy:{} bleu :{} \n".format(i, ith_accuracy, ith_bleu))
                elif self.args.evaluation_metric == 'qa':
                    if outputs[i].strip().lower() in target_predict[i].strip().lower().split('\t'):
                        print(outputs[i].strip().lower()+'\n'+str(target_predict[i].strip().lower().split('\t')))
                        correct_num += 1
        if self.args.evaluation_metric == 'passage':
            os.rename(output_predication_file,os.path.join(output_dir,"predictions_{}_{}.txt".format(suffix,np.mean(accuracy_list))))
            print("mean accuracy: {}".format(np.mean(accuracy_list)))
        if self.args.evaluation_metric == 'qa':
            print("correct number: {}, correct ratio: {}".format(correct_num, correct_num/float(len(outputs))))


        if self.args.num_return_sequences > 1:
            return [
                outputs[i : i + self.args.num_return_sequences]
                for i in range(0, len(outputs), self.args.num_return_sequences)
            ]
        else:
            return outputs

        Performs predictions on a list of text.
        Args:
            pred_data: Pandas DataFrame containing the 2 columns - `input_text`, `target_text`.
                        - `input_text`: The input text sequence.
                        - `target_text`: The target text sequence.            
            output_dir: The directory where predictition results files will be saved. If not given, self.args.output_dir will be used.
            suffix: The supplementary suffix of prediction results name.
        Returns:
            preds: A python list of the generated sequences.
        
Performs predictions on a list of text.
Performs predictions on a list of text .
def predict(self, pred_data, output_dir=None, suffix=None, verbose=True, silent=False):
        to_predict = pred_data["input_text"].tolist()
        target_predict = pred_data["target_text"].tolist()
        assert len(to_predict)==len(target_predict)
        self._move_model_to_device()
        if not output_dir:
            output_dir = self.args.output_dir
        self._move_model_to_device()
        os.makedirs(output_dir, exist_ok=True)
        all_outputs = []
        for batch in tqdm([to_predict[i : i + self.args.eval_batch_size] for i in range(0, len(to_predict), self.args.eval_batch_size)],
                          desc='Predicting', disable=self.args.silent, mininterval=0,):
            if self.args.model_type == "marian":
                input_ids = self.encoder_tokenizer.prepare_translation_batch(
                    batch,
                    max_length=self.args.max_seq_length,
                    padding=True,
                    return_tensors="pt",
                    truncation=True,
                )["input_ids"]
            else:
                input_ids = self.encoder_tokenizer.batch_encode_plus(
                    batch,
                    max_length=self.args.max_seq_length,
                    padding=True,
                    return_tensors="pt",
                    truncation=True,
                )["input_ids"]
            input_ids = input_ids.to(self.device)
            if self.args.model_type in ["bart", "marian"]:
                outputs = self.model.generate(
                    input_ids=input_ids,
                    num_beams=self.args.num_beams,
                    max_length=self.args.max_length,
                    length_penalty=self.args.length_penalty,
                    early_stopping=self.args.early_stopping,
                    repetition_penalty=self.args.repetition_penalty,
                    do_sample=self.args.do_sample,
                    top_k=self.args.top_k,
                    top_p=self.args.top_p,
                    num_return_sequences=self.args.num_return_sequences,
                )
            else:
                outputs = self.model.generate(
                    input_ids=input_ids,
                    decoder_start_token_id=self.model.config.decoder.pad_token_id,
                    num_beams=self.args.num_beams,
                    max_length=self.args.max_length,
                    length_penalty=self.args.length_penalty,
                    early_stopping=self.args.early_stopping,
                    repetition_penalty=self.args.repetition_penalty,
                    do_sample=self.args.do_sample,
                    top_k=self.args.top_k,
                    top_p=self.args.top_p,
                    num_return_sequences=self.args.num_return_sequences,
                )
            all_outputs.extend(outputs.cpu().numpy())
        if self.args.use_multiprocessed_decoding:
            self.model.to("cpu")
            with Pool(self.args.process_count) as p:
                outputs = list(
                    tqdm(
                        p.imap(self._decode, all_outputs, chunksize=self.args.multiprocessing_chunksize),
                        total=len(all_outputs),
                        desc="Decoding outputs",
                        disable=self.args.silent,
                    )
                )
            self._move_model_to_device()
        else:
            outputs = [
                self.decoder_tokenizer.decode(output_id, skip_special_tokens=True, clean_up_tokenization_spaces=True)
                for output_id in all_outputs
            ]
        output_predication_file = os.path.join(output_dir, "predictions_{}.txt".format(suffix))
        correct_num = 0
        accuracy_list = []
        with open(output_predication_file, "w", encoding="utf8", errors="ignore") as writer:
            writer.write("to_predict\n\toutput\n\ttarget\n")
            for i in range(len(outputs)):
                outputs[i] = outputs[i].strip()
                writer.write(to_predict[i]+"\t"+outputs[i]+"\n\t"+target_predict[i])
                if self.args.evaluation_metric == 'passage':
                    ith_accuracy = self.passage_predict_accuray(to_predict[i], outputs[i], target_predict[i], 10)
                    ith_bleu = corpus_bleu(sys_stream=outputs[i], ref_streams=target_predict[i]).score
                    if ith_accuracy < 0:
                        pass
                    else:
                        accuracy_list.append(ith_accuracy)
                    print("number :{} accuracy:{} bleu :{}".format(i, ith_accuracy, ith_bleu))
                    writer.write("number :{} accuracy:{} bleu :{} \n".format(i, ith_accuracy, ith_bleu))
                elif self.args.evaluation_metric == 'qa':
                    if outputs[i].strip().lower() in target_predict[i].strip().lower().split('\t'):
                        print(outputs[i].strip().lower()+'\n'+str(target_predict[i].strip().lower().split('\t')))
                        correct_num += 1
        if self.args.evaluation_metric == 'passage':
            os.rename(output_predication_file,os.path.join(output_dir,"predictions_{}_{}.txt".format(suffix,np.mean(accuracy_list))))
            print("mean accuracy: {}".format(np.mean(accuracy_list)))
        if self.args.evaluation_metric == 'qa':
            print("correct number: {}, correct ratio: {}".format(correct_num, correct_num/float(len(outputs))))
        if self.args.num_return_sequences > 1:
            return [
                outputs[i : i + self.args.num_return_sequences]
                for i in range(0, len(outputs), self.args.num_return_sequences)
            ]
        else:
            return outputs
def predict ( self , pred_data , output_dir = None , suffix = None , verbose = True , silent = False ) : to_predict = pred_data [ "input_text" ] . tolist ( ) target_predict = pred_data [ "target_text" ] . tolist ( ) assert len ( to_predict ) == len ( target_predict ) self . _move_model_to_device ( ) if not output_dir : output_dir = self . args . output_dir self . _move_model_to_device ( ) os . makedirs ( output_dir , exist_ok = True ) all_outputs = [ ] for batch in tqdm ( [ to_predict [ i : i + self . args . eval_batch_size ] for i in range ( 0 , len ( to_predict ) , self . args . eval_batch_size ) ] , desc = 'Predicting' , disable = self . args . silent , mininterval = 0 , ) : if self . args . model_type == "marian" : input_ids = self . encoder_tokenizer . prepare_translation_batch ( batch , max_length = self . args . max_seq_length , padding = True , return_tensors = "pt" , truncation = True , ) [ "input_ids" ] else : input_ids = self . encoder_tokenizer . batch_encode_plus ( batch , max_length = self . args . max_seq_length , padding = True , return_tensors = "pt" , truncation = True , ) [ "input_ids" ] input_ids = input_ids . to ( self . device ) if self . args . model_type in [ "bart" , "marian" ] : outputs = self . model . generate ( input_ids = input_ids , num_beams = self . args . num_beams , max_length = self . args . max_length , length_penalty = self . args . length_penalty , early_stopping = self . args . early_stopping , repetition_penalty = self . args . repetition_penalty , do_sample = self . args . do_sample , top_k = self . args . top_k , top_p = self . args . top_p , num_return_sequences = self . args . num_return_sequences , ) else : outputs = self . model . generate ( input_ids = input_ids , decoder_start_token_id = self . model . config . decoder . pad_token_id , num_beams = self . args . num_beams , max_length = self . args . max_length , length_penalty = self . args . length_penalty , early_stopping = self . args . early_stopping , repetition_penalty = self . args . repetition_penalty , do_sample = self . args . do_sample , top_k = self . args . top_k , top_p = self . args . top_p , num_return_sequences = self . args . num_return_sequences , ) all_outputs . extend ( outputs . cpu ( ) . numpy ( ) ) if self . args . use_multiprocessed_decoding : self . model . to ( "cpu" ) with Pool ( self . args . process_count ) as p : outputs = list ( tqdm ( p . imap ( self . _decode , all_outputs , chunksize = self . args . multiprocessing_chunksize ) , total = len ( all_outputs ) , desc = "Decoding outputs" , disable = self . args . silent , ) ) self . _move_model_to_device ( ) else : outputs = [ self . decoder_tokenizer . decode ( output_id , skip_special_tokens = True , clean_up_tokenization_spaces = True ) for output_id in all_outputs ] output_predication_file = os . path . join ( output_dir , "predictions_{}.txt" . format ( suffix ) ) correct_num = 0 accuracy_list = [ ] with open ( output_predication_file , "w" , encoding = "utf8" , errors = "ignore" ) as writer : writer . write ( "to_predict\n\toutput\n\ttarget\n" ) for i in range ( len ( outputs ) ) : outputs [ i ] = outputs [ i ] . strip ( ) writer . write ( to_predict [ i ] + "\t" + outputs [ i ] + "\n\t" + target_predict [ i ] ) if self . args . evaluation_metric == 'passage' : ith_accuracy = self . passage_predict_accuray ( to_predict [ i ] , outputs [ i ] , target_predict [ i ] , 10 ) ith_bleu = corpus_bleu ( sys_stream = outputs [ i ] , ref_streams = target_predict [ i ] ) . score if ith_accuracy < 0 : pass else : accuracy_list . append ( ith_accuracy ) print ( "number :{} accuracy:{} bleu :{}" . format ( i , ith_accuracy , ith_bleu ) ) writer . write ( "number :{} accuracy:{} bleu :{} \n" . format ( i , ith_accuracy , ith_bleu ) ) elif self . args . evaluation_metric == 'qa' : if outputs [ i ] . strip ( ) . lower ( ) in target_predict [ i ] . strip ( ) . lower ( ) . split ( '\t' ) : print ( outputs [ i ] . strip ( ) . lower ( ) + '\n' + str ( target_predict [ i ] . strip ( ) . lower ( ) . split ( '\t' ) ) ) correct_num += 1 if self . args . evaluation_metric == 'passage' : os . rename ( output_predication_file , os . path . join ( output_dir , "predictions_{}_{}.txt" . format ( suffix , np . mean ( accuracy_list ) ) ) ) print ( "mean accuracy: {}" . format ( np . mean ( accuracy_list ) ) ) if self . args . evaluation_metric == 'qa' : print ( "correct number: {}, correct ratio: {}" . format ( correct_num , correct_num / float ( len ( outputs ) ) ) ) if self . args . num_return_sequences > 1 : return [ outputs [ i : i + self . args . num_return_sequences ] for i in range ( 0 , len ( outputs ) , self . args . num_return_sequences ) ] else : return outputs
Performs predictions on a list of text.
Performs predictions on a list of text .
"""
        Performs predictions on a list of text.
        Args:
            pred_data: Pandas DataFrame containing the 2 columns - `input_text`, `target_text`.
                        - `input_text`: The input text sequence.
                        - `target_text`: The target text sequence.            
            output_dir: The directory where predictition results files will be saved. If not given, self.args.output_dir will be used.
            suffix: The supplementary suffix of prediction results name.
        Returns:
            preds: A python list of the generated sequences.
        """ # noqa: ignore flake8" # Batching
A python list of the generated sequences.
A python list of the generated sequences .
preds
self
pred_data
Pandas DataFrame containing the 2 columns - `input_text`, `target_text`.
`input_text`: The input text sequence.
`target_text`: The target text sequence.
Pandas DataFrame containing the 2 columns - ` input_text ` ` target_text ` . ` input_text ` : The input text sequence . ` target_text ` : The target text sequence .
output_dir
The directory where predictition results files will be saved. If not given, self.args.output_dir will be used.
The directory where predictition results files will be saved . If not given self . args . output_dir will be used .
suffix
The supplementary suffix of prediction results name.
The supplementary suffix of prediction results name .
verbose
silent