Bert模型微調---產生自己的訓練數據模型

1 引言

盡管已經發展出許多預訓練模型,但正如過去試驗看到的一樣,這些預訓練模型還不能真正滿足我們巖土工程專業的需要,為了真正達到我們的目的,必須在預訓練模型的基礎上微調出我們自己的模型GeotechSet,之所以長時間沒有這樣做,其中一個主要原因是考慮到時間問題,以我目前的硬件配置,訓練出一個新的模型需要好幾個小時(下面例子的模型訓練用了大約50分鐘,訓練數據1.3M)。這個筆記簡要總結了微調模型的過程,檢驗了訓練出來的模型是否可用。


2 訓練模型

微調代碼保存在training_stsbenchmark.py中,訓練數據集保存在datasets文件夾內。

預訓練模型可以選擇任意的Transformers模型,例如Bert,RoBERTa,XLNet, XLM-R,DistilBERT等(bert-base-uncased, roberta-base, xlm-roberta-base,bert-base-cased)。

(1) 首先embedding預訓練模型

word_embedding_model = models.Transformer(model_name)

(2) 應用均值集合,得到一個固定大小的句子向量

pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension(),pooling_mode_mean_tokens=True,pooling_mode_cls_token=False,pooling_mode_max_tokens=False)

(3) 產生SentenceTransformer模型

model = SentenceTransformer(modules=[word_embedding_model, pooling_model])

(4) 轉換訓練數據,把訓練數據分成三個數據集,并加載數據:

train_samples = []

dev_samples = []

test_samples = []

train_samples的使用:

train_dataloader = DataLoader(train_samples, shuffle=True, batch_size=train_batch_size)train_loss = losses.CosineSimilarityLoss(model=model)

dev_samples的使用:

evaluator = EmbeddingSimilarityEvaluator.from_input_examples(dev_samples, name='sts-dev')

(5) 訓練模型

model.fit(train_objectives=[(train_dataloader, train_loss)],evaluator=evaluator,epochs=num_epochs,evaluation_steps=1000,warmup_steps=warmup_steps,output_path=model_save_path)

(6) 評價模型

model = SentenceTransformer(model_save_path)test_evaluator = EmbeddingSimilarityEvaluator.from_input_examples(test_samples, name='sts-test')test_evaluator(model, output_path=model_save_path)

訓練后的模型保存在output文件夾內。


3 驗證模型

訓練完成后,需要檢驗這個模型是否正常工作。使用sentence-transformers-similarity.py進行檢驗,命名為stb,下圖中的最后一個。

Bert模型微調---產生自己的訓練數據模型的圖1

從test.txt內查找與“step-path failure models of large open pit slopes(大型露天礦邊坡的階梯式破壞模型)”句子最相似的三個句子,結果如下:


[1] Based on the results of the laboratory simulations step-path failure models of large open pit slopes are presented and the influence of intact rock bridge length, step-path overlap and fracture spacing discussed. (基于實驗室模擬的結果,提出了大型露天礦邊坡的階梯式破壞模型,并討論了完整巖橋長度、階梯式重疊和斷裂間距的影響。)

[2]  {FracMan} ==>Simulations are presented based on DFN models of a large conceptual rock slope and incorporating varied failure mechanisms, demonstrating the importance of both considering realistic fracture mechanisms and the ability to model complex failure paths involving sliding along discontinuities, dilation, and intact rock fracture. (基于大型巖石邊坡的DFN模型并結合不同的破壞機制進行了模擬,證明了考慮真實斷裂機制和模擬復雜破壞路徑能力的重要性,其中包括沿不連續體的滑動、擴張和完整巖石斷裂。)

[3] {Characterisation of High Rock Slopes using an Integrated Numerical Modelling} ==>3. a) Photogrammetry derived point cloud of open pit slope face b) 3D mesh generated from 3D point cloud c) 3D mesh imported into 3D numerical code (Slope Model) B-2. Characterization of brittle fracture in surface mines using conceptual simulation Failure of large rock slopes is often a combination of slip and/or opening of preexissting non-persistent discontinuities and intact rock failure. (3. a) 攝影測量得出的露天礦坡面的點云 b) 從三維點云生成的三維網格 c) 三維網格導入三維數值代碼(Slope Model) B-2. 利用概念模擬對露天礦的脆性斷裂進行表征,顯示大型巖石邊坡的破壞往往是滑移和/或預先存在的非貫通不連續體的張開與完整巖石破壞的結合。)


4 結束語

這個筆記使用一個小樣本數據集微調出新的訓練模型,并檢驗了新的模型是否可用。結果顯示目前使用的微調過程在本機上可以使用。以后我們將逐漸訓練出自己的GeotechSet模型。 


登錄后免費查看全文
立即登錄
App下載
技術鄰APP
工程師必備
  • 項目客服
  • 培訓客服
  • 平臺客服

TOP