AlanRobotics commited on
Commit
ebfd652
1 Parent(s): b6a4691

Upload model

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