Upload folder using huggingface_hub
Browse files- config.json +30 -0
- model.safetensors +3 -0
- modeling_simcse.py +48 -0
- special_tokens_map.json +7 -0
- tokenizer_config.json +64 -0
- vocab.txt +0 -0
config.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"DualEncoderSimCSEModel"
|
| 4 |
+
],
|
| 5 |
+
"attention_probs_dropout_prob": 0.1,
|
| 6 |
+
"classifier_dropout": null,
|
| 7 |
+
"hidden_act": "gelu",
|
| 8 |
+
"hidden_dropout_prob": 0.1,
|
| 9 |
+
"hidden_size": 768,
|
| 10 |
+
"initializer_range": 0.02,
|
| 11 |
+
"input_encoder_name": "tohoku-nlp/bert-base-japanese-v3",
|
| 12 |
+
"intermediate_size": 3072,
|
| 13 |
+
"layer_norm_eps": 1e-12,
|
| 14 |
+
"max_position_embeddings": 512,
|
| 15 |
+
"model_type": "bert",
|
| 16 |
+
"num_attention_heads": 12,
|
| 17 |
+
"num_hidden_layers": 12,
|
| 18 |
+
"output_encoder_name": "tohoku-nlp/bert-base-japanese-v3",
|
| 19 |
+
"pad_token_id": 0,
|
| 20 |
+
"position_embedding_type": "absolute",
|
| 21 |
+
"simcse_temperature": 0.05,
|
| 22 |
+
"torch_dtype": "float32",
|
| 23 |
+
"transformers_version": "4.51.3",
|
| 24 |
+
"type_vocab_size": 2,
|
| 25 |
+
"use_cache": true,
|
| 26 |
+
"vocab_size": 32768,
|
| 27 |
+
"auto_map": {
|
| 28 |
+
"AutoModel": "modeling_simcse.SimCSEInferenceModel"
|
| 29 |
+
}
|
| 30 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:63fecadbccad33f96a05ca8f8eb94e50e6a8f397d8ae2b83fc2d2fa9257be24a
|
| 3 |
+
size 894432952
|
modeling_simcse.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
from transformers import (
|
| 3 |
+
BertModel,
|
| 4 |
+
BertConfig,
|
| 5 |
+
PreTrainedModel,
|
| 6 |
+
)
|
| 7 |
+
from transformers.tokenization_utils_base import BatchEncoding
|
| 8 |
+
import torch, torch.nn as nn, torch.nn.functional as F
|
| 9 |
+
|
| 10 |
+
class SimCSEInferenceModel(PreTrainedModel):
|
| 11 |
+
config_class = BertConfig # 推論時は BERT Config と合わせる
|
| 12 |
+
|
| 13 |
+
def __init__(self, config):
|
| 14 |
+
super().__init__(config)
|
| 15 |
+
# 追加ダウンロードを避けるため from_config で空モデルを組み立てる
|
| 16 |
+
base_cfg = BertConfig(**config.to_dict())
|
| 17 |
+
self.encoder_input = BertModel(base_cfg)
|
| 18 |
+
self.encoder_output = BertModel(base_cfg)
|
| 19 |
+
|
| 20 |
+
hidden = self.encoder_input.config.hidden_size
|
| 21 |
+
self.dense_input = nn.Linear(hidden, hidden)
|
| 22 |
+
self.dense_output = nn.Linear(hidden, hidden)
|
| 23 |
+
self.activation = nn.Tanh()
|
| 24 |
+
self.temperature = getattr(config, "simcse_temperature", 0.05)
|
| 25 |
+
|
| 26 |
+
@torch.no_grad()
|
| 27 |
+
def encode_input(self, tok: BatchEncoding) -> torch.Tensor:
|
| 28 |
+
h = self.encoder_input(**tok).last_hidden_state[:, 0]
|
| 29 |
+
return self.activation(self.dense_input(h))
|
| 30 |
+
|
| 31 |
+
@torch.no_grad()
|
| 32 |
+
def encode_output(self, tok: BatchEncoding) -> torch.Tensor:
|
| 33 |
+
h = self.encoder_output(**tok).last_hidden_state[:, 0]
|
| 34 |
+
return self.activation(self.dense_output(h))
|
| 35 |
+
|
| 36 |
+
def forward(
|
| 37 |
+
self,
|
| 38 |
+
tokenized_texts_1: BatchEncoding,
|
| 39 |
+
tokenized_texts_2: BatchEncoding,
|
| 40 |
+
labels: torch.Tensor,
|
| 41 |
+
**_
|
| 42 |
+
):
|
| 43 |
+
device = next(self.parameters()).device
|
| 44 |
+
z1 = F.normalize(self.encode_input(tokenized_texts_1.to(device)), dim=-1)
|
| 45 |
+
z2 = F.normalize(self.encode_output(tokenized_texts_2.to(device)), dim=-1)
|
| 46 |
+
sim = torch.matmul(z1, z2.T)
|
| 47 |
+
loss = F.cross_entropy(sim / self.temperature, labels.to(device))
|
| 48 |
+
return {"loss": loss, "logits": sim}
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": "[CLS]",
|
| 3 |
+
"mask_token": "[MASK]",
|
| 4 |
+
"pad_token": "[PAD]",
|
| 5 |
+
"sep_token": "[SEP]",
|
| 6 |
+
"unk_token": "[UNK]"
|
| 7 |
+
}
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "[PAD]",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"1": {
|
| 12 |
+
"content": "[UNK]",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"2": {
|
| 20 |
+
"content": "[CLS]",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"3": {
|
| 28 |
+
"content": "[SEP]",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"4": {
|
| 36 |
+
"content": "[MASK]",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"clean_up_tokenization_spaces": false,
|
| 45 |
+
"cls_token": "[CLS]",
|
| 46 |
+
"do_lower_case": false,
|
| 47 |
+
"do_subword_tokenize": true,
|
| 48 |
+
"do_word_tokenize": true,
|
| 49 |
+
"extra_special_tokens": {},
|
| 50 |
+
"jumanpp_kwargs": null,
|
| 51 |
+
"mask_token": "[MASK]",
|
| 52 |
+
"mecab_kwargs": {
|
| 53 |
+
"mecab_dic": "unidic_lite"
|
| 54 |
+
},
|
| 55 |
+
"model_max_length": 512,
|
| 56 |
+
"never_split": null,
|
| 57 |
+
"pad_token": "[PAD]",
|
| 58 |
+
"sep_token": "[SEP]",
|
| 59 |
+
"subword_tokenizer_type": "wordpiece",
|
| 60 |
+
"sudachi_kwargs": null,
|
| 61 |
+
"tokenizer_class": "BertJapaneseTokenizer",
|
| 62 |
+
"unk_token": "[UNK]",
|
| 63 |
+
"word_tokenizer_type": "mecab"
|
| 64 |
+
}
|
vocab.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|