patomp commited on
Commit
1e99f6a
1 Parent(s): a35d4d7

Upload model

Browse files
Files changed (5) hide show
  1. config.json +15 -0
  2. configuration.py +19 -0
  3. encoder.py +26 -0
  4. projector.py +29 -0
  5. pytorch_model.bin +3 -0
config.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "ThaiLightWeightEncoderModel"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "configuration.ThaiLightWeightEncoderConfig",
7
+ "AutoModel": "encoder.ThaiLightWeightEncoderModel"
8
+ },
9
+ "dropout": 0.2,
10
+ "final_embedding_dim": 512,
11
+ "input_embedding_dim": 300,
12
+ "torch_dtype": "float32",
13
+ "transformers_version": "4.28.1",
14
+ "word_vector_model_name": "thai2fit_wv"
15
+ }
configuration.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+ from typing import List
3
+
4
+
5
+ class ThaiLightWeightEncoderConfig(PretrainedConfig):
6
+
7
+ def __init__(
8
+ self,
9
+ input_embedding_dim: int = 300,
10
+ final_embedding_dim: int = 512,
11
+ dropout: float = 0.2,
12
+ word_vector_model_name: str = "thai2fit_wv",
13
+ **kwargs,
14
+ ):
15
+ self.input_embedding_dim = input_embedding_dim
16
+ self.final_embedding_dim = final_embedding_dim
17
+ self.word_vector_model_name = word_vector_model_name
18
+ self.dropout = dropout
19
+ super().__init__(**kwargs)
encoder.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PreTrainedModel
2
+
3
+ from pythainlp import word_vector
4
+ import torch
5
+
6
+ from .configuration import ThaiLightWeightEncoderConfig
7
+ from .projector import Projector
8
+
9
+
10
+ class ThaiLightWeightEncoderModel(PreTrainedModel):
11
+ config_class = ThaiLightWeightEncoderConfig
12
+
13
+ def __init__(self, config):
14
+ super().__init__(config)
15
+ self.wv = word_vector.WordVector(model_name=config.word_vector_model_name)
16
+ self.projector = Projector(
17
+ input_embedding_dim=config.input_embedding_dim,
18
+ final_embedding_dim=config.final_embedding_dim,
19
+ dropout=config.dropout
20
+ )
21
+
22
+ def forward(self, text: str):
23
+ embed = self.wv.sentence_vectorizer(text, use_mean=True)[0]
24
+ proj_embed = self.projector(torch.from_numpy(embed).float())
25
+ proj_embed = proj_embed.to("cpu").detach().numpy()
26
+ return proj_embed
projector.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch import nn
3
+ import numpy as np
4
+
5
+
6
+ class Projector(nn.Module):
7
+ def __init__(
8
+ self,
9
+ input_embedding_dim: int = 300,
10
+ final_embedding_dim: int = 512,
11
+ dropout: float = 0.2
12
+ ):
13
+ super().__init__()
14
+ self.fc1 = nn.Linear(input_embedding_dim, 512)
15
+ self.fn1 = nn.LeakyReLU()
16
+ self.fc2 = nn.Linear(512, final_embedding_dim)
17
+ self.fn2 = nn.LeakyReLU()
18
+ self.dropout = nn.Dropout(dropout)
19
+ self.layer_norm = nn.LayerNorm(final_embedding_dim)
20
+ self.temperature = nn.Parameter(torch.ones([]) * np.log(1 / 0.07))
21
+
22
+ def forward(self, x):
23
+ x = self.fc1(x)
24
+ x = self.fn1(x)
25
+ x = self.dropout(x)
26
+ x = self.fc2(x)
27
+ x = self.fn2(x)
28
+ x = self.layer_norm(x)
29
+ return x
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ba76acb7fec448de261d0b0af611fe61f1a7a2b49be1b2cecfd0828d90d8d628
3
+ size 1673609