Commit
·
54dcf0f
1
Parent(s):
040947a
Upload model
Browse files- config.json +4 -0
- configuration_siamese.py +14 -0
- modeling_siamese.py +50 -0
config.json
CHANGED
|
@@ -2,6 +2,10 @@
|
|
| 2 |
"architectures": [
|
| 3 |
"SiamseNNModel"
|
| 4 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"model_type": "AutoModel",
|
| 6 |
"torch_dtype": "float32",
|
| 7 |
"transformers_version": "4.24.0"
|
|
|
|
| 2 |
"architectures": [
|
| 3 |
"SiamseNNModel"
|
| 4 |
],
|
| 5 |
+
"auto_map": {
|
| 6 |
+
"AutoConfig": "configuration_siamese.SiameseConfig",
|
| 7 |
+
"AutoModel": "modeling_siamese.SiamseNNModel"
|
| 8 |
+
},
|
| 9 |
"model_type": "AutoModel",
|
| 10 |
"torch_dtype": "float32",
|
| 11 |
"transformers_version": "4.24.0"
|
configuration_siamese.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class SiameseConfig(PretrainedConfig):
|
| 5 |
+
model_type = "AutoModel"
|
| 6 |
+
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
**kwargs):
|
| 10 |
+
super().__init__()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
siamese_config = SiameseConfig()
|
| 14 |
+
siamese_config.save_pretrained('siamse_nn')
|
modeling_siamese.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PreTrainedModel, BertModel
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
checkpoint = 'cointegrated/rubert-tiny'
|
| 5 |
+
|
| 6 |
+
class Lambda(torch.nn.Module):
|
| 7 |
+
def __init__(self, lambd):
|
| 8 |
+
super().__init__()
|
| 9 |
+
self.lambd = lambd
|
| 10 |
+
|
| 11 |
+
def forward(self, x):
|
| 12 |
+
return self.lambd(x)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class SiameseNN(torch.nn.Module):
|
| 16 |
+
def __init__(self):
|
| 17 |
+
super(SiameseNN, self).__init__()
|
| 18 |
+
l1_norm = lambda x: 1 - torch.abs(x[0] - x[1])
|
| 19 |
+
self.encoder = BertModel.from_pretrained(checkpoint)
|
| 20 |
+
self.merged = Lambda(l1_norm)
|
| 21 |
+
self.fc1 = torch.nn.Linear(312, 2)
|
| 22 |
+
self.softmax = torch.nn.Softmax()
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def forward(self, x):
|
| 26 |
+
first_encoded = self.encoder(**x[0]).pooler_output
|
| 27 |
+
#print("First: ", first_encoded)
|
| 28 |
+
second_encoded = self.encoder(**x[1]).pooler_output
|
| 29 |
+
l1_distance = self.merged([first_encoded, second_encoded])
|
| 30 |
+
#print(l1_distance.shape)
|
| 31 |
+
fc1 = self.fc1(l1_distance)
|
| 32 |
+
fc1 = self.softmax(fc1)
|
| 33 |
+
return fc1
|
| 34 |
+
|
| 35 |
+
second_model = SiameseNN()
|
| 36 |
+
second_model.load_state_dict(torch.load('siamese_state'))
|
| 37 |
+
|
| 38 |
+
class SiamseNNModel(PreTrainedModel):
|
| 39 |
+
def __init__(self, config):
|
| 40 |
+
super().__init__(config)
|
| 41 |
+
self.model = second_model
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def forward(self, tensor, labels=None):
|
| 45 |
+
logits = self.model(tensor)
|
| 46 |
+
if labels is not None:
|
| 47 |
+
loss_fn = torch.nn.CrossEntropyLoss()
|
| 48 |
+
loss = loss_fn(logits, labels)
|
| 49 |
+
return {'loss': loss, 'logits': logits}
|
| 50 |
+
return {'logits': logits}
|