File size: 5,155 Bytes
ae56469 3084243 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# %%
import torch
from transformers import (
BertTokenizer,
BertForMaskedLM,
AutoModelForMaskedLM,
AutoTokenizer,
BertModel,
)
import numpy as np
import random
from itertools import islice
from torch.utils.data import Dataset, DataLoader
from torch.optim import AdamW, SGD
from tqdm import tqdm
import os
def index_to_onehot(l, length):
# l=[1, 5], len=6 -> [0,1,0,0,0,1]
return [1 if i in l else 0 for i in range(length)]
def get_punctuation_position(tokenized_text, tokenizer):
# adjust comma_pos and period_pos
count = 0
comma_pos = []
period_pos = []
punctuation_removed_text = []
comma_id = tokenizer.convert_tokens_to_ids("、")
period_id = tokenizer.convert_tokens_to_ids("。")
for i, c in enumerate(tokenized_text):
if c == comma_id:
comma_pos.append(i - count - 1)
count += 1
elif c == period_id:
period_pos.append(i - count - 1)
count += 1
else:
punctuation_removed_text.append(c)
if len(punctuation_removed_text) < 512:
punctuation_removed_text += [tokenizer.pad_token_id] * (
512 - len(punctuation_removed_text)
)
return (
torch.tensor(punctuation_removed_text),
[
index_to_onehot(comma_pos, 512),
index_to_onehot(period_pos, 512),
],
)
# %%
# get_punctuation_position("今日は、いい天気です。")
# # %%
# index_to_onehot([1, 2, 3, 4, 5], 7)
# tokenizer = BertTokenizer.from_pretrained("cl-tohoku/bert-base-japanese-char")
# tokenized_text = tokenizer(
# "今 日 は 、 い い 天 気 で す 。",
# max_length=512,
# padding="max_length",
# truncation=True,
# return_tensors="pt",
# )
# inputs, label = get_punctuation_position(tokenized_text["input_ids"][0], tokenizer)
# print(inputs) # ->tensor([ 2, 732, 48, 12, 19, 19, 411, 343, 17, 46, 3, 0, 0, 0, ...])
# print(label) # -> [[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, ...], # 点の位置(最初に[SOS]が入るため、1つずれる)
# -> [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, ...]] # 丸の位置
# %%
class PunctuationPositionDataset(torch.utils.data.Dataset):
def __init__(self, data, tokenizer):
self.data = data
self.tokenizer = tokenizer
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
text = self.data[idx]
text = " ".join(list(text))
inputs = self.tokenizer(
text,
max_length=512,
padding="max_length",
truncation=True,
return_tensors="pt",
)
# if idx % 100 == 0:
# print(masked_text, label)
input_ids, label = get_punctuation_position(
inputs["input_ids"][0], self.tokenizer
)
label = torch.tensor(label, dtype=torch.float32).transpose(0, 1)
return (input_ids, inputs.attention_mask.squeeze(), label.squeeze(), text)
# %%
model_name = "tohoku-nlp/bert-base-japanese-char-v3"
tokenizer = BertTokenizer.from_pretrained(model_name)
base_model = BertModel.from_pretrained(model_name)
# %%
class punctuation_predictor(torch.nn.Module):
def __init__(self, base_model):
super().__init__()
self.base_model = base_model
self.dropout = torch.nn.Dropout(0.2)
self.linear = torch.nn.Linear(768, 2)
def forward(self, input_ids, attention_mask):
last_hidden_state = self.base_model(
input_ids=input_ids, attention_mask=attention_mask
).last_hidden_state
# get last hidden state token by token and apply linear layer
return self.linear(self.dropout(last_hidden_state))
model = punctuation_predictor(base_model)
# %%
# a = tokenizer("今 日 は い い 天 気 で す 。",max_length=512,
# padding="max_length",
# truncation=True,
# return_tensors="pt",)
# %%
with open("data/train.txt", "r") as f:
texts = f.readlines()
dataset = PunctuationPositionDataset(texts, tokenizer)
# %%
data_loader = DataLoader(
dataset,
batch_size=16,
shuffle=True,
num_workers=8,
)
# %%
# set lr to 5e-5 to base model
optimizer = AdamW(
[
{"params": model.base_model.parameters(), "lr": 5e-5},
{"params": model.linear.parameters(), "lr": 1e-3},
],
)
criteria = torch.nn.BCEWithLogitsLoss()
# %%
model.train()
model.to("cuda")
for epoch in range(10):
epoch_loss = 0.0
progress_bar = tqdm(data_loader, desc=f"Epoch {epoch+1}")
for batch in progress_bar:
input_ids, attention_masks, labels, text = batch
input_ids = input_ids.to("cuda")
attention_masks = attention_masks.to("cuda")
labels = labels.to("cuda")
outputs = model(input_ids=input_ids, attention_mask=attention_masks)
loss = criteria(outputs, labels)
loss.backward()
optimizer.step()
optimizer.zero_grad()
epoch_loss += loss.item()
progress_bar.set_postfix({"loss": epoch_loss / len(data_loader)})
# %%
torch.save(model.state_dict(), "weight/punctuation_position_model.pth")
|