Skip to content

Commit

Permalink
Update transformer version in bert_ranker
Browse files Browse the repository at this point in the history
  • Loading branch information
Ihab-Asaad committed Mar 3, 2022
1 parent ea68f89 commit 5ef5bbb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"class_name": "torch_bert_ranker",
"one_hot_labels": false,
"pretrained_bert": "bert-base-uncased",
"bert_config_file": "{MODEL_PATH}/model/config.json",

This comment has been minimized.

Copy link
@vaskonov

vaskonov Mar 15, 2022

Contributor

try eliminating this row

This comment has been minimized.

Copy link
@Ihab-Asaad

Ihab-Asaad Mar 15, 2022

Author Contributor

Ok, I tested without it.

"save_path": "{MODEL_PATH}/model",
"load_path": "{MODEL_PATH}/model",
"optimizer": "AdamW",
Expand Down Expand Up @@ -86,7 +87,7 @@
"download": [
{
"url": "http://files.deeppavlov.ai/datasets/ubuntu_v2_data.tar.gz",
"subdir": "{DOWNLOADS_PATH}/ubuntu_v2_data"
"subdir": "{DOWNLOADS_PATH}/ubuntu_v2_data_"
},
{
"url": "http://files.deeppavlov.ai/deeppavlov_data/ubuntu_v2_uncased_torch_bert_model_v0.tar.gz",
Expand Down
4 changes: 2 additions & 2 deletions deeppavlov/core/common/requirements_registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
],
"torch_bert_ranker": [
"{DEEPPAVLOV_PATH}/requirements/pytorch16.txt",
"{DEEPPAVLOV_PATH}/requirements/transformers28.txt"
"{DEEPPAVLOV_PATH}/requirements/transformers.txt"
],
"torch_transformers_classifier": [
"{DEEPPAVLOV_PATH}/requirements/pytorch16.txt",
Expand All @@ -226,7 +226,7 @@
],
"torch_bert_ranker_preprocessor": [
"{DEEPPAVLOV_PATH}/requirements/pytorch16.txt",
"{DEEPPAVLOV_PATH}/requirements/transformers28.txt"
"{DEEPPAVLOV_PATH}/requirements/transformers.txt"
],
"transformers_bert_preprocessor": [
"{DEEPPAVLOV_PATH}/requirements/transformers.txt"
Expand Down
30 changes: 17 additions & 13 deletions deeppavlov/models/torch_bert/torch_bert_ranker.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TorchBertRankerModel(TorchModel):
e.g. {'lr': 0.1, 'weight_decay': 0.001, 'momentum': 0.9}
"""

def __init__(self, pretrained_bert: str,
def __init__(self, pretrained_bert: str = None,
bert_config_file: Optional[str] = None,
n_classes: int = 2,
return_probas: bool = True,
Expand Down Expand Up @@ -97,7 +97,7 @@ def train_on_batch(self, features_li: List[List[InputFeatures]], y: Union[List[i
self.optimizer.zero_grad()

loss, logits = self.model(b_input_ids, token_type_ids=None, attention_mask=b_input_masks,
labels=b_labels)
labels=b_labels, return_dict=False)
loss.backward()
# Clip the norm of the gradients to 1.0.
# This is to help prevent the "exploding gradients" problem.
Expand Down Expand Up @@ -162,13 +162,20 @@ def load(self, fname=None):

if self.pretrained_bert:
log.info(f"From pretrained {self.pretrained_bert}.")
config = AutoConfig.from_pretrained(self.pretrained_bert,
# num_labels=self.n_classes,
output_attentions=False,
output_hidden_states=False)
if Path(expand_path(self.pretrained_bert)).exists():
self.pretrained_bert = str(expand_path(self.pretrained_bert))

This comment has been minimized.

Copy link
@vaskonov

vaskonov Mar 15, 2022

Contributor

what's the point?

This comment has been minimized.

Copy link
@Ihab-Asaad

Ihab-Asaad Mar 15, 2022

Author Contributor

in case the pretrained model name exists as folder name

config = AutoConfig.from_pretrained(self.pretrained_bert,
# num_labels=self.n_classes,
output_attentions=False,
output_hidden_states=False)

else:
config = AutoConfig.from_pretrained(self.pretrained_bert,
# num_labels=self.n_classes,
output_attentions=False,
output_hidden_states=False)

self.model = AutoModelForSequenceClassification.from_pretrained(self.pretrained_bert, config=config)

try:
hidden_size = self.model.classifier.out_proj.in_features

Expand All @@ -188,13 +195,10 @@ def load(self, fname=None):
self.model.num_labels = self.n_classes


elif self.bert_config_file and Path(self.bert_config_file).is_file():
self.bert_config = AutoConfig.from_json_file(str(expand_path(self.bert_config_file)))
if self.attention_probs_keep_prob is not None:
self.bert_config.attention_probs_dropout_prob = 1.0 - self.attention_probs_keep_prob
if self.hidden_keep_prob is not None:
self.bert_config.hidden_dropout_prob = 1.0 - self.hidden_keep_prob
elif self.bert_config_file and expand_path(self.bert_config_file).is_file():
self.bert_config = AutoConfig.from_pretrained(str(expand_path(self.bert_config_file)))
self.model = AutoModelForSequenceClassification.from_config(config=self.bert_config)

else:
raise ConfigError("No pre-trained BERT model is given.")

Expand Down

0 comments on commit 5ef5bbb

Please sign in to comment.