当前位置:   article > 正文

sentence_transformers 微调模型_sentence_transformers微调模型

sentence_transformers微调模型

模型使用的是:uer/sbert-base-chinese-nli · Hugging Face

使用sentencetransformer微调模型

sentence_transformers 官网:SentenceTransformers Documentation — Sentence-Transformers documentation

使用自己的数据微调模型

sentence_transformer 使用自己训练数据微调模型的代码如下所示。(模型为计算句子相似度)

  1. from sentence_transformers import SentenceTransformer,SentenceDataset,InputExample,evaluation,losses,util
  2. class sbert():
  3. def build_train_data(self,o1,o2,n1,n2,train_size):
  4. train_data = []
  5. for i in range(train_size):
  6. train_data.append(InputExample(tests=[o1[i],o2[i]],label=1.0))
  7. train_data.append(InputExample(tests=[n1[i],n2[i]],label=1.0))
  8. return train_data
  9. def build_evaluation_data(o1,o2,n1,n2,train_size,eval_size):
  10. s1 = o1[train_size:]
  11. s2 = o2[train_size:]
  12. s1.extend(list(n1[train_size:]))
  13. s2.extend(list(n2[train_size:]))
  14. score = [1.0]*eval_size + [0.0]*eval_size
  15. evaluator = evaluation.EmbeddingSimilarityEvaluator(s1,s2,score)
  16. return evaluator
  17. def callback(self,score,epoch,steps)
  18. print('score:{},epoch:{},steps:{}'.format(score,epoch,steps))
  19. def train(self):
  20. #1.获取正、负样本,o1是标准问,O2是相似问
  21. o1,o2 = self.get_act_data()
  22. n1,n2 = self.get_neg_data()
  23. #2.定义训练集、测试集大小 + 构造训练数据
  24. train_size = int(len(o1)*0.8)
  25. eval_size = len(o1) - train_size
  26. train_data = self.build_train_data(o1,o2,n1,n2,train_size)
  27. #3.定义测试数据
  28. evaluator = self.build_evaluation_data(o1,o2,n1,n2,train_size,eval_size)
  29. #4.需要训练的模型
  30. mode = SentenceTransformer('模型地址')
  31. #5
  32. train_dataset = SentenceDataset(train_data,model)
  33. train_dataloader = DataLoader(train_dataset,shuffle =true, batch_size = 8)
  34. train_loss = losses.CosineSimilarityLoss(model)
  35. #6.调试模型
  36. model.fit(train_objectives = [(train_dataloader,train_loss)],epochs = 1,warmup_steps = 100,evaluator = evaluator,evaluation_steps = 100,output_path = '存调试后模型的地址',save_best_model = True,callback = self.callback)

sentence_transformer使用自己微调后的模型的代码如下所示:

  1. #1. 定义模型
  2. model = SentenceTransformer('模型地址')
  3. #2.编码向量
  4. o1_emb = model.encode(['数据list','求求一定要好运啊'])
  5. o2_emb = model.encode(['一定要是列表','我绝对可以好运']
  6. #计算相似度
  7. cosine_score0 = util.cos_sim(o1_emb,o2_emb)
  8. cosine_score = []
  9. for i in range(len(cosine_score0)):
  10. cosine_score.append(cosine_score0[i][i].numpy().tolist()

增加模型层数

  1. from sentence_transformers import SentenceTransformer, models
  2. from torch import nn
  3. word_embedding_model = models.Transformer('bert-base-uncased', max_seq_length=256)
  4. pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension())
  5. dense_model = models.Dense(in_features=pooling_model.get_sentence_embedding_dimension(), out_features=256, activation_function=nn.Tanh())
  6. model = SentenceTransformer(modules=[word_embedding_model, pooling_model, dense_model])

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/367597?site
推荐阅读
相关标签
  

闽ICP备14008679号