Upload model
Browse files- config.json +15 -0
- configuration.py +18 -0
- projector_residual.py +25 -0
- pytorch_model.bin +3 -0
- th_encoder.py +18 -0
- wangchanberta_cross_clip.py +29 -0
config.json
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"WangchanbertaEncoderModel"
|
4 |
+
],
|
5 |
+
"auto_map": {
|
6 |
+
"AutoConfig": "configuration.WangchanbertaEncoderConfig",
|
7 |
+
"AutoModel": "wangchanberta_cross_clip.WangchanbertaEncoderModel"
|
8 |
+
},
|
9 |
+
"dropout": 0.2,
|
10 |
+
"input_text_embedding_dim": 768,
|
11 |
+
"output_embedding_dim": 512,
|
12 |
+
"th_model_base": "airesearch/wangchanberta-base-att-spm-uncased",
|
13 |
+
"torch_dtype": "float32",
|
14 |
+
"transformers_version": "4.29.1"
|
15 |
+
}
|
configuration.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List
|
2 |
+
|
3 |
+
from transformers import PretrainedConfig
|
4 |
+
|
5 |
+
|
6 |
+
class WangchanbertaEncoderConfig(PretrainedConfig):
|
7 |
+
def __init__(
|
8 |
+
self,
|
9 |
+
th_model_base: str = "airesearch/wangchanberta-base-att-spm-uncased",
|
10 |
+
input_text_embedding_dim: int = 768,
|
11 |
+
output_embedding_dim: int = 512,
|
12 |
+
dropout: float = 0.2
|
13 |
+
):
|
14 |
+
super().__init__()
|
15 |
+
self.th_model_base = th_model_base
|
16 |
+
self.input_text_embedding_dim = input_text_embedding_dim
|
17 |
+
self.output_embedding_dim = output_embedding_dim
|
18 |
+
self.dropout = dropout
|
projector_residual.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch.nn as nn
|
2 |
+
|
3 |
+
|
4 |
+
class HeadProjectorResidual(nn.Module):
|
5 |
+
def __init__(
|
6 |
+
self,
|
7 |
+
input_embedding_dim: int = 1000,
|
8 |
+
output_embedding_dim: int = 512,
|
9 |
+
dropout: float = 0.4
|
10 |
+
):
|
11 |
+
super().__init__()
|
12 |
+
self.projection = nn.Linear(input_embedding_dim, output_embedding_dim)
|
13 |
+
self.gelu = nn.GELU()
|
14 |
+
self.fc = nn.Linear(output_embedding_dim, output_embedding_dim)
|
15 |
+
self.dropout = nn.Dropout(dropout)
|
16 |
+
self.layer_norm = nn.LayerNorm(output_embedding_dim)
|
17 |
+
|
18 |
+
def forward(self, x):
|
19 |
+
projected = self.projection(x)
|
20 |
+
x = self.gelu(projected)
|
21 |
+
x = self.fc(x)
|
22 |
+
x = self.dropout(x)
|
23 |
+
x = x + projected
|
24 |
+
x = self.layer_norm(x)
|
25 |
+
return x
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9389d57d4a8469bae436267b2147144f7ce9c2c7d5be063381db212eac2f729a
|
3 |
+
size 423681349
|
th_encoder.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch.nn as nn
|
2 |
+
from transformers import AutoModel
|
3 |
+
|
4 |
+
|
5 |
+
class ThaiEncoder(nn.Module):
|
6 |
+
def __init__(self, model_name: str, trainable: bool = False) -> None:
|
7 |
+
super().__init__()
|
8 |
+
self.model = AutoModel.from_pretrained(model_name)
|
9 |
+
|
10 |
+
for p in self.model.parameters():
|
11 |
+
p.requires_grad = trainable
|
12 |
+
|
13 |
+
self.target_token_idx = 0
|
14 |
+
|
15 |
+
def forward(self, input_ids, attention_mask):
|
16 |
+
output = self.model(input_ids=input_ids, attention_mask=attention_mask)
|
17 |
+
last_hidden_state = output.last_hidden_state
|
18 |
+
return last_hidden_state[:, self.target_token_idx, :]
|
wangchanberta_cross_clip.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
|
3 |
+
from transformers import PreTrainedModel, AutoTokenizer
|
4 |
+
from transformers.configuration_utils import PretrainedConfig
|
5 |
+
|
6 |
+
from .th_encoder import ThaiEncoder
|
7 |
+
from .projector_residual import HeadProjectorResidual
|
8 |
+
|
9 |
+
|
10 |
+
class WangchanbertaEncoderModel(PreTrainedModel):
|
11 |
+
def __init__(self, config: PretrainedConfig):
|
12 |
+
super().__init__(config)
|
13 |
+
self.text_tokenizer = AutoTokenizer.from_pretrained(config.th_model_base)
|
14 |
+
self.text_encoder = ThaiEncoder(model_name=config.th_model_base)
|
15 |
+
self.text_projector = HeadProjectorResidual(
|
16 |
+
input_embedding_dim=config.input_text_embedding_dim,
|
17 |
+
output_embedding_dim=config.output_embedding_dim,
|
18 |
+
dropout=config.dropout
|
19 |
+
)
|
20 |
+
self.max_length = 200
|
21 |
+
|
22 |
+
def forward(self, text: str):
|
23 |
+
tokened_word = self.text_tokenizer(text, padding='max_length', truncation=True, max_length=self.max_length)
|
24 |
+
text_vector = self.text_encoder(
|
25 |
+
input_ids=torch.tensor([tokened_word["input_ids"]]),
|
26 |
+
attention_mask=torch.tensor([tokened_word["attention_mask"]])
|
27 |
+
)
|
28 |
+
text_projected = self.text_projector(text_vector)
|
29 |
+
return text_projected
|