def run(self, focused=False, focused_model=None, training_epochs=5):
    if (focused == True):
        self.model_list = [focused_model]
    else:
        pass
    for model_name in self.model_list:
        training_args = TrainingArguments(output_dir=('./results/' + model_name), num_train_epochs=training_epochs, per_device_train_batch_size=16, per_device_eval_batch_size=64, warmup_steps=500, weight_decay=0.01, logging_dir=('./logs/' + model_name))
        model = None
        tokenizer = None
        print((('Training on a dataset with ' + str(self.num_labels)) + ' labels'))
        if (model_name == 'bert-base-uncased'):
            model = BertForSequenceClassification.from_pretrained(model_name, num_labels=self.num_labels)
            tokenizer = BertTokenizerFast.from_pretrained(model_name)
        elif (model_name == 'albert-base-v2'):
            tokenizer = transformers.AlbertTokenizer.from_pretrained('albert-base-v2')
            model = transformers.AlbertForSequenceClassification.from_pretrained('albert-base-v2', return_dict=True, num_labels=self.num_labels)
        elif (model_name == 'roberta-base'):
            tokenizer = transformers.RobertaTokenizer.from_pretrained('roberta-base')
            model = transformers.RobertaForSequenceClassification.from_pretrained('roberta-base', return_dict=True, num_labels=self.num_labels)
        elif (model_name == 'linear_SVM'):
            tokenizer = None
            model = 'linear_SVM'
            parameters = {'vect__ngram_range': [(1, 1), (1, 2)], 'tfidf__use_idf': (True, False), 'clf__alpha': (0.05, 0.01, 0.005, 0.001, 0.005), 'clf__penalty': ('l2', 'l1', 'elasticnet')}
            classifier = SGDClassifier(loss='hinge', random_state=42, max_iter=5, tol=None)
        elif (model_name == 'multinomial_naive_bayesian'):
            tokenizer = None
            model = 'multinomial_naive_bayesian'
            parameters = {'vect__ngram_range': [(1, 1), (1, 2)], 'tfidf__use_idf': (True, False), 'clf__alpha': (1, 0.1, 0.01, 0.001, 0.0001), 'clf__fit_prior': (True, False)}
            classifier = MultinomialNB()
        if ((not model) or (not tokenizer)):
            print('ERROR')

        def tokenize(batch):
            return tokenizer(batch['text'], padding='max_length', truncation=True)
        if (tokenizer is not None):
            train_dataset = self.train_dataset_raw.map(tokenize, batched=True, batch_size=len(self.train_dataset_raw))
            test_dataset = self.test_dataset_raw.map(tokenize, batched=True, batch_size=len(self.train_dataset_raw))
            train_dataset.set_format('torch', columns=['input_ids', 'attention_mask', 'labels'])
            test_dataset.set_format('torch', columns=['input_ids', 'attention_mask', 'labels'])
        else:
            train_dataset = self.train_dataset_raw
            test_dataset = self.test_dataset_raw
        if ((model_name == 'linear_SVM') or (model_name == 'multinomial_naive_bayesian')):
            trainer = None
            pipeline = Pipeline([('vect', CountVectorizer()), ('tfidf', TfidfTransformer()), ('clf', classifier)])
            gs_clf = GridSearchCV(pipeline, parameters, cv=5, n_jobs=(- 1))
            if (len(train_dataset['labels']) < 25):
                print('not enough data to use a count vectorizer, sorry!')
            else:
                gs_ind = int((len(train_dataset['labels']) / 10))
                gs_clf = gs_clf.fit(train_dataset['text'][:gs_ind], train_dataset['labels'][:gs_ind])
                best_params = gs_clf.best_params_
                pipeline.set_params(**best_params)
                pipeline.fit(train_dataset['text'], train_dataset['labels'])
                prediction = pipeline.predict(test_dataset['text'])
                (precision, recall, f1, _) = precision_recall_fscore_support(test_dataset['labels'], prediction, average=None)
                full_report = classification_report(test_dataset['labels'], prediction)
                acc = accuracy_score(test_dataset['labels'], prediction)
                loss = hamming_loss(test_dataset['labels'], prediction)
                curr_metrics = {'eval_loss': loss, 'eval_accuracy': np.mean(acc), 'eval_f1': np.mean(f1), 'eval_precision': np.mean(precision), 'eval_recall': np.mean(recall), 'eval_full_report': full_report}
                dump(pipeline, (model_name + '_model.joblib'))
                print('best parameters are:')
                print(best_params)
        else:
            trainer = Trainer(model=model, args=training_args, compute_metrics=self.compute_metrics, train_dataset=train_dataset, eval_dataset=test_dataset)
            trainer.train()
            curr_metrics = trainer.evaluate()
            trainer.save_model((model_name + '_model'))
        self.all_metrics[model_name] = curr_metrics
        print(curr_metrics)
        del model, tokenizer, trainer
        gc.collect()
        torch.cuda.empty_cache()
def run(self, focused=False, focused_model=None, training_epochs=5):
    if (focused == True):
        self.model_list = [focused_model]
    else:
        pass
    for model_name in self.model_list:
        training_args = TrainingArguments(output_dir=('./results/' + model_name), num_train_epochs=training_epochs, per_device_train_batch_size=16, per_device_eval_batch_size=64, warmup_steps=500, weight_decay=0.01, logging_dir=('./logs/' + model_name))
        model = None
        tokenizer = None
        print((('Training on a dataset with ' + str(self.num_labels)) + ' labels'))
        if (model_name == 'bert-base-uncased'):
            model = BertForSequenceClassification.from_pretrained(model_name, num_labels=self.num_labels)
            tokenizer = BertTokenizerFast.from_pretrained(model_name)
        elif (model_name == 'albert-base-v2'):
            tokenizer = transformers.AlbertTokenizer.from_pretrained('albert-base-v2')
            model = transformers.AlbertForSequenceClassification.from_pretrained('albert-base-v2', return_dict=True, num_labels=self.num_labels)
        elif (model_name == 'roberta-base'):
            tokenizer = transformers.RobertaTokenizer.from_pretrained('roberta-base')
            model = transformers.RobertaForSequenceClassification.from_pretrained('roberta-base', return_dict=True, num_labels=self.num_labels)
        elif (model_name == 'linear_SVM'):
            tokenizer = None
            model = 'linear_SVM'
            parameters = {'vect__ngram_range': [(1, 1), (1, 2)], 'tfidf__use_idf': (True, False), 'clf__alpha': (0.05, 0.01, 0.005, 0.001, 0.005), 'clf__penalty': ('l2', 'l1', 'elasticnet')}
            classifier = SGDClassifier(loss='hinge', random_state=42, max_iter=5, tol=None)
        elif (model_name == 'multinomial_naive_bayesian'):
            tokenizer = None
            model = 'multinomial_naive_bayesian'
            parameters = {'vect__ngram_range': [(1, 1), (1, 2)], 'tfidf__use_idf': (True, False), 'clf__alpha': (1, 0.1, 0.01, 0.001, 0.0001), 'clf__fit_prior': (True, False)}
            classifier = MultinomialNB()
        if ((not model) or (not tokenizer)):
            print('ERROR')

        def tokenize(batch):
            return tokenizer(batch['text'], padding='max_length', truncation=True)
        if (tokenizer is not None):
            train_dataset = self.train_dataset_raw.map(tokenize, batched=True, batch_size=len(self.train_dataset_raw))
            test_dataset = self.test_dataset_raw.map(tokenize, batched=True, batch_size=len(self.train_dataset_raw))
            train_dataset.set_format('torch', columns=['input_ids', 'attention_mask', 'labels'])
            test_dataset.set_format('torch', columns=['input_ids', 'attention_mask', 'labels'])
        else:
            train_dataset = self.train_dataset_raw
            test_dataset = self.test_dataset_raw
        if ((model_name == 'linear_SVM') or (model_name == 'multinomial_naive_bayesian')):
            trainer = None
            pipeline = Pipeline([('vect', CountVectorizer()), ('tfidf', TfidfTransformer()), ('clf', classifier)])
            gs_clf = GridSearchCV(pipeline, parameters, cv=5, n_jobs=(- 1))
            if (len(train_dataset['labels']) < 25):
                print('not enough data to use a count vectorizer, sorry!')
            else:
                gs_ind = int((len(train_dataset['labels']) / 10))
                gs_clf = gs_clf.fit(train_dataset['text'][:gs_ind], train_dataset['labels'][:gs_ind])
                best_params = gs_clf.best_params_
                pipeline.set_params(**best_params)
                pipeline.fit(train_dataset['text'], train_dataset['labels'])
                prediction = pipeline.predict(test_dataset['text'])
                (precision, recall, f1, _) = precision_recall_fscore_support(test_dataset['labels'], prediction, average=None)
                full_report = classification_report(test_dataset['labels'], prediction)
                acc = accuracy_score(test_dataset['labels'], prediction)
                loss = hamming_loss(test_dataset['labels'], prediction)
                curr_metrics = {'eval_loss': loss, 'eval_accuracy': np.mean(acc), 'eval_f1': np.mean(f1), 'eval_precision': np.mean(precision), 'eval_recall': np.mean(recall), 'eval_full_report': full_report}
                dump(pipeline, (model_name + '_model.joblib'))
                print('best parameters are:')
                print(best_params)
        else:
            trainer = Trainer(model=model, args=training_args, compute_metrics=self.compute_metrics, train_dataset=train_dataset, eval_dataset=test_dataset)
            trainer.train()
            curr_metrics = trainer.evaluate()
            trainer.save_model((model_name + '_model'))
        self.all_metrics[model_name] = curr_metrics
        print(curr_metrics)
        del model, tokenizer, trainer
        gc.collect()
        torch.cuda.empty_cache()