secilozksen commited on
Commit
0627ca8
·
1 Parent(s): bcd30a8

Upload model

Browse files
Files changed (4) hide show
  1. config.json +13 -0
  2. configuration_dpr.py +7 -0
  3. modeling_dpr.py +66 -0
  4. pytorch_model.bin +3 -0
config.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "/home/secilsen/PycharmProjects/NeuroscienceQA/DPR-model/checkpoint-374",
3
+ "architectures": [
4
+ "OBSSDPRModel"
5
+ ],
6
+ "auto_map": {
7
+ "AutoConfig": "configuration_dpr.CustomDPRConfig",
8
+ "AutoModel": "modeling_dpr.OBSSDPRModel"
9
+ },
10
+ "model_type": "dpr",
11
+ "torch_dtype": "float32",
12
+ "transformers_version": "4.24.0"
13
+ }
configuration_dpr.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+
3
+
4
+ class CustomDPRConfig(PretrainedConfig):
5
+ model_type = 'dpr'
6
+ def __init__(self, **kwargs):
7
+ super().__init__(**kwargs)
modeling_dpr.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PreTrainedModel, DPRQuestionEncoder, DPRContextEncoder
2
+ import torch
3
+ import torch.nn as nn
4
+ from .configuration_dpr import CustomDPRConfig
5
+ from typing import Union, List, Dict
6
+
7
+
8
+ class OBSSDPRModel(PreTrainedModel):
9
+ config_class = CustomDPRConfig
10
+
11
+ def __init__(self, config):
12
+ super().__init__(config)
13
+ self.config = config
14
+ self.model = DPRModel()
15
+
16
+ def forward(self, input):
17
+ return self.model(input)
18
+
19
+
20
+ class DPRModel(nn.Module):
21
+ def __init__(self,
22
+ question_model_name='facebook/dpr-question_encoder-single-nq-base',
23
+ context_model_name='facebook/dpr-ctx_encoder-single-nq-base',
24
+ freeze_params=12.0):
25
+ super(DPRModel, self).__init__()
26
+ self.freeze_params = freeze_params
27
+ self.question_model = DPRQuestionEncoder.from_pretrained(question_model_name)
28
+ self.context_model = DPRContextEncoder.from_pretrained(context_model_name)
29
+ # self.freeze_layers(freeze_params)
30
+
31
+ def freeze_layers(self, freeze_params):
32
+ num_layers_context = sum(1 for _ in self.context_model.parameters())
33
+ num_layers_question = sum(1 for _ in self.question_model.parameters())
34
+
35
+ for parameters in list(self.context_model.parameters())[:int(freeze_params * num_layers_context)]:
36
+ parameters.requires_grad = False
37
+
38
+ for parameters in list(self.context_model.parameters())[int(freeze_params * num_layers_context):]:
39
+ parameters.requires_grad = True
40
+
41
+ for parameters in list(self.question_model.parameters())[:int(freeze_params * num_layers_question)]:
42
+ parameters.requires_grad = False
43
+
44
+ for parameters in list(self.question_model.parameters())[int(freeze_params * num_layers_question):]:
45
+ parameters.requires_grad = True
46
+
47
+ def batch_dot_product(self, context_output, question_output):
48
+ mat1 = torch.unsqueeze(question_output, dim=1)
49
+ mat2 = torch.unsqueeze(context_output, dim=2)
50
+ result = torch.bmm(mat1, mat2)
51
+ result = torch.squeeze(result, dim=1)
52
+ result = torch.squeeze(result, dim=1)
53
+ return result
54
+
55
+ def forward(self, batch: Union[List[Dict], Dict]):
56
+ context_tensor = batch['context_tensor']
57
+ question_tensor = batch['question_tensor']
58
+ context_model_output = self.context_model(input_ids=context_tensor['input_ids'],
59
+ attention_mask=context_tensor['attention_mask']) # (bsz, hdim)
60
+ question_model_output = self.question_model(input_ids = question_tensor['input_ids'],
61
+ attention_mask=question_tensor['attention_mask'])
62
+ embeddings_context = context_model_output['pooler_output']
63
+ embeddings_question = question_model_output['pooler_output']
64
+
65
+ scores = self.batch_dot_product(embeddings_context, embeddings_question) # self.scale
66
+ return scores
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:133eaf09746322ca5e9f81a7b3f27063ba8b9de11e4a569eca5e700ece969f4b
3
+ size 871284457