Update metrics_finetuning_teacher.py
Browse files- metrics_finetuning_teacher.py +19 -36
metrics_finetuning_teacher.py
CHANGED
|
@@ -1,10 +1,5 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
-
"""
|
| 4 |
-
Created on Thu Aug 17 14:12:16 2023
|
| 5 |
-
|
| 6 |
-
@author: fujidai
|
| 7 |
-
"""
|
| 8 |
|
| 9 |
|
| 10 |
import torch
|
|
@@ -20,15 +15,14 @@ import numpy as np
|
|
| 20 |
from sentence_transformers import SentenceTransformer, util
|
| 21 |
|
| 22 |
|
| 23 |
-
word_embedding_model = models.Transformer('paraphrase-mpnet-base-v2', max_seq_length=512)# modelの指定をする
|
| 24 |
|
| 25 |
pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension())
|
| 26 |
-
#dense_model = models.Dense(in_features=pooling_model.get_sentence_embedding_dimension(),out_features=16)
|
| 27 |
model = SentenceTransformer(modules=[word_embedding_model, pooling_model],device='mps')
|
| 28 |
print(model)
|
| 29 |
|
| 30 |
|
| 31 |
-
with open('/da_seikika.txt', 'r') as f:#
|
| 32 |
|
| 33 |
raberu = f.read()
|
| 34 |
raberu_lines = raberu.splitlines()#改行コードごとにリストに入れている
|
|
@@ -39,12 +33,16 @@ for i in range(len(raberu_lines)):
|
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
-
with open('/ref.txt', 'r') as f
|
| 43 |
left = f.read()
|
| 44 |
left_lines = left.splitlines()
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
with open('/trg.txt', 'r') as f
|
| 48 |
right = f.read()
|
| 49 |
right_lines = right.splitlines()#改行コードごとにリストに入れている
|
| 50 |
|
|
@@ -53,13 +51,14 @@ train_examples = []
|
|
| 53 |
for i in range(len(left_lines)):
|
| 54 |
pair=[]
|
| 55 |
pair.append(left_lines[i])#left_lines側のi行目をtextsに追加している
|
|
|
|
| 56 |
pair.append(right_lines[i])#right_lines側のi行目をtextsに追加している
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
#if aq>=0.25:
|
| 62 |
train_examples.append(example)#学習として入れるものに入れている
|
|
|
|
| 63 |
print(len(train_examples))
|
| 64 |
|
| 65 |
|
|
@@ -68,18 +67,16 @@ device = torch.device('mps')
|
|
| 68 |
|
| 69 |
import torch.nn.functional as F
|
| 70 |
|
| 71 |
-
|
| 72 |
train_dataloader = DataLoader(train_examples, shuffle=True, batch_size=8)
|
| 73 |
-
|
| 74 |
-
train_loss = losses.CosineSimilarityLoss(model)
|
| 75 |
|
| 76 |
|
| 77 |
#Tune the model
|
| 78 |
model.fit(train_objectives=[(train_dataloader, train_loss)], epochs=100, warmup_steps=100,show_progress_bar=True,
|
| 79 |
-
|
| 80 |
-
checkpoint_path='checkpoint-savename',
|
| 81 |
-
|
| 82 |
-
|
| 83 |
|
| 84 |
)
|
| 85 |
model.save("savename")
|
|
@@ -88,18 +85,4 @@ model.save("savename")
|
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
#
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
import torch
|
|
|
|
| 15 |
from sentence_transformers import SentenceTransformer, util
|
| 16 |
|
| 17 |
|
| 18 |
+
word_embedding_model = models.Transformer('/paraphrase-mpnet-base-v2', max_seq_length=512)# modelの指定をする
|
| 19 |
|
| 20 |
pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension())
|
|
|
|
| 21 |
model = SentenceTransformer(modules=[word_embedding_model, pooling_model],device='mps')
|
| 22 |
print(model)
|
| 23 |
|
| 24 |
|
| 25 |
+
with open('/da_seikika.txt', 'r') as f:#dascoreを正規化したもの
|
| 26 |
|
| 27 |
raberu = f.read()
|
| 28 |
raberu_lines = raberu.splitlines()#改行コードごとにリストに入れている
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
+
with open('/ref.txt', 'r') as f:#リファレンス
|
| 37 |
left = f.read()
|
| 38 |
left_lines = left.splitlines()
|
| 39 |
|
| 40 |
+
with open('/ref.txt', 'r') as f:#リファレンス
|
| 41 |
+
senter = f.read()
|
| 42 |
+
senter_lines = senter.splitlines()
|
| 43 |
+
|
| 44 |
|
| 45 |
+
with open('/trg.txt', 'r') as f:#ターゲット
|
| 46 |
right = f.read()
|
| 47 |
right_lines = right.splitlines()#改行コードごとにリストに入れている
|
| 48 |
|
|
|
|
| 51 |
for i in range(len(left_lines)):
|
| 52 |
pair=[]
|
| 53 |
pair.append(left_lines[i])#left_lines側のi行目をtextsに追加している
|
| 54 |
+
pair.append(senter_lines[i])
|
| 55 |
pair.append(right_lines[i])#right_lines側のi行目をtextsに追加している
|
| 56 |
+
|
| 57 |
+
absolutely=abs(1-data[i])# 1ーdascoreのす値を絶対値を付けている
|
| 58 |
+
example = InputExample(texts=pair, label=absolutely)#textsをラベル付きで追加している
|
| 59 |
+
|
|
|
|
| 60 |
train_examples.append(example)#学習として入れるものに入れている
|
| 61 |
+
|
| 62 |
print(len(train_examples))
|
| 63 |
|
| 64 |
|
|
|
|
| 67 |
|
| 68 |
import torch.nn.functional as F
|
| 69 |
|
|
|
|
| 70 |
train_dataloader = DataLoader(train_examples, shuffle=True, batch_size=8)
|
| 71 |
+
train_loss = losses.MarginMSELoss(model=model,similarity_fct=F.cosine_similarity)
|
|
|
|
| 72 |
|
| 73 |
|
| 74 |
#Tune the model
|
| 75 |
model.fit(train_objectives=[(train_dataloader, train_loss)], epochs=100, warmup_steps=100,show_progress_bar=True,
|
| 76 |
+
|
| 77 |
+
checkpoint_path='checkpoint-savename',
|
| 78 |
+
checkpoint_save_steps=6699,#どのくらいのイテレーションごとに保存するか
|
| 79 |
+
save_best_model=True
|
| 80 |
|
| 81 |
)
|
| 82 |
model.save("savename")
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
#
|