F-Haru commited on
Commit
1bc94fa
·
1 Parent(s): f31434a

Update metrics_finetuning_student.py

Browse files
Files changed (1) hide show
  1. metrics_finetuning_student.py +22 -28
metrics_finetuning_student.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
@@ -23,12 +18,11 @@ from sentence_transformers import SentenceTransformer, util
23
  word_embedding_model = models.Transformer('/paraphrase-multilingual-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:#Negative en-ja cos_sim
32
 
33
  raberu = f.read()
34
  raberu_lines = raberu.splitlines()#改行コードごとにリストに入れている
@@ -39,47 +33,51 @@ for i in range(len(raberu_lines)):
39
 
40
 
41
 
42
- with open('/src.txt', 'r') as f:#TEDのenglish
43
  left = f.read()
44
  left_lines = left.splitlines()
45
 
 
 
 
 
46
 
47
- with open('/trg.txt', 'r') as f:#pseudo japanese (TEDのenglishをgoogle翻訳に入れた疑似コパス)
48
  right = f.read()
49
  right_lines = right.splitlines()#改行コードごとにリストに入れている
50
 
51
 
 
52
  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
- example = InputExample(texts=pair, label=data[i])#textsラベルきで追加している
59
- #print(example)#
60
- #label=1-data[i]の1は positive cos_sim
61
- #if aq>=0.25:
62
  train_examples.append(example)#学習として入れるものに入れている
 
63
  print(len(train_examples))
64
 
65
 
 
66
  device = torch.device('mps')
67
- #print(device)
68
 
69
  import torch.nn.functional as F
70
 
71
-
72
  train_dataloader = DataLoader(train_examples, shuffle=True, batch_size=8)
73
- #train_loss = losses.MarginMSELoss(model=model,similarity_fct=F.cosine_similarity)
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
- #output_path='完成2best-6-30',
80
- checkpoint_path='checkpoint-savename',checkpoint_save_steps=6699,#どのくらいのイテレーションごとに保存するか
81
- save_best_model=True#,#,#checkpoint_save_total_limit=5
82
- #optimizer_params= {'lr': 2e-6}#
83
 
84
  )
85
  model.save("savename")
@@ -87,16 +85,12 @@ model.save("savename")
87
 
88
 
89
 
 
90
 
91
 
92
 
93
 
94
-
95
-
96
-
97
-
98
-
99
-
100
 
101
 
102
 
 
1
  #!/usr/bin/env python3
2
  # -*- coding: utf-8 -*-
 
 
 
 
 
3
 
4
 
5
  import torch
 
18
  word_embedding_model = models.Transformer('/paraphrase-multilingual-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('/src.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
 
49
 
50
+
51
  train_examples = []
52
  for i in range(len(left_lines)):
53
  pair=[]
54
  pair.append(left_lines[i])#left_lines側のi行目をtextsに追加している
55
+ pair.append(senter_lines[i])
56
  pair.append(right_lines[i])#right_lines側のi行目をtextsに追加している
57
+
58
+ absolutely=abs(1-data[i])# 1ーdascoreの数値絶対値をている
59
+ example = InputExample(texts=pair, label=absolutely)#textsをラベル付きで追加している
 
 
60
  train_examples.append(example)#学習として入れるものに入れている
61
+
62
  print(len(train_examples))
63
 
64
 
65
+
66
  device = torch.device('mps')
 
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
 
75
  #Tune the model
76
  model.fit(train_objectives=[(train_dataloader, train_loss)], epochs=100, warmup_steps=100,show_progress_bar=True,
77
+
78
+ checkpoint_path='checkpoint-savename',
79
+ checkpoint_save_steps=6699,#どのくらいのイテレーションごとに保存するか
80
+ save_best_model=True,
81
 
82
  )
83
  model.save("savename")
 
85
 
86
 
87
 
88
+ '''
89
 
90
 
91
 
92
 
93
+ '''
 
 
 
 
 
94
 
95
 
96