def test_set_custom_classifier(self): all_data = get_ssts_dataset('train') model_config = RNN_MODEL_CONFIG preprocessor = GlovePreprocessor(TOKEN_COUNT_THRESHOLD, MAX_SENT_COUNT) preprocessor.build_vocab(all_data[TEXT_COL]) explainer = IntrospectiveRationaleExplainer( classifier_type=CLASSIFIER_TYPE_CUSTOM, cuda=CUDA) explainer.build_model_config(model_config) # set custom classifier classifier = ClassifierModule(explainer.get_model_config(), preprocessor.word_vocab) explainer.set_preprocessor(preprocessor) explainer.set_classifier(classifier) assert explainer.classifier_type is CLASSIFIER_TYPE_CUSTOM
def set_classifier(self, classifier_type=CLASSIFIER_TYPE_RNN, classifier=None): """ Set the classifier from prepackaged option or provide custom classifier :param classifier_type: One of ['BERT', 'RNN', 'BERT_RNN'] :type: str :param classifier: Custom provided classifier :type: Any """ if classifier_type == CLASSIFIER_TYPE_RNN or classifier_type == CLASSIFIER_TYPE_BERT_RNN: self.classifier = ClassifierModule(self.model_config, self.preprocessor.word_vocab) elif classifier_type == CLASSIFIER_TYPE_BERT: self.classifier = BertForSequenceClassification.from_pretrained( "bert-base-uncased", num_labels=self.model_config.num_labels, output_hidden_states=False, output_attentions=False, ) else: self.classifier = classifier
def set_generator_classifier(self, classifier_type=CLASSIFIER_TYPE_RNN, generator_classifier=None): """ Set classifier for the Generator :param classifier_type: One of ['BERT', 'RNN', 'BERT_RNN'] :type classifier_type: str :param generator_classifier: Custom provided classifier for generator :type generator_classifier: Any :return: Any """ if classifier_type == CLASSIFIER_TYPE_RNN: self.generator_classifier = ClassifierModule( self.model_config, self.preprocessor.word_vocab) elif classifier_type == CLASSIFIER_TYPE_BERT or classifier_type == CLASSIFIER_TYPE_BERT_RNN: self.generator_classifier = BertForSequenceClassification.from_pretrained( "bert-base-uncased", num_labels=self.model_config.num_labels, output_hidden_states=True, output_attentions=True, ) else: self.generator_classifier = generator_classifier