BERTopic(V0.9.0)主題模擬技術

1 引言

BERTopic(最新版本0.9.0,8/7/2021)是一種主題建模技術(BERTopic performs topic Modeling with state-of-the-art transformer models),它利用Transformers和c-TF-IDF來創建密集的集群(clusters),使主題易于解釋,同時在主題描述中保留重要的詞匯。它甚至支持與LDAvis類似的可視化。

主題模擬的藝術(The Art of Topic Modeling)---以Step-Path Failure為例

GeotechSet數據集主題模擬(Topic Modeling)

LDA Topic Modeling(主題建模): 以Rocscience 2021用戶會議為例

2 安裝BERTopic

由于BERTopic依賴大量的庫,為了避免發生沖突,建立了虛擬環境bertpoic, 使用默認安裝方式: pip install bertopic;  BERTopic也支持Flair嵌入,包括gensim,spacy, 同時需要安裝Tensorflow, 使用pip install bertopic[all]命令可以安裝全部的后臺庫,不過,因為Tensorflow(大約425M)的下載速度太慢,所以目前沒有全部安裝。在默認的安裝狀態下,主要依賴的庫:transformers(V4.9.2), torch(V1.9.0), sentence-transformers(2.0.0).

3 BERTopic的算法

總的來說,BERTopic使用了三個步驟:

(1) 嵌入文檔(Embed documents)

用BERT或任何其他嵌入技術提取文檔嵌入。BERTopic默認的嵌入模型是sentence-transformers,也可以使用Spacy, Flair, Gensim, USE等嵌入模型。

(2) 聚類文檔(Cluster Documents)

用UMAP來降低嵌入的維度; 用HDBSCAN對減少的嵌入進行聚類,并創建語義相似的文件群。

(3) 創建主題表征(Create topic representation)

用c-TF-IDF提取和減少主題; 提高具有最大邊際相關性的詞匯的連貫性.

4 產生主題

為了便于比較,使用《主題模擬的藝術(The Art of Topic Modeling)---以Step-Path Failure為例》中的數據集,這是一個基于關鍵詞step-path failure的數據集,在那篇筆記中,采用Top2Vec庫進行了主題模擬,結果產生出兩個非常近似的主題, 其中最核心的關鍵詞是rockslides和fracture。

BERTopic默認的模型是paraphrase-MiniLM-L6-v2, 以前曾經使用過這個模型(聯合6種Transformers預訓練模型). 同樣的數據使用BERTopic產生出如下19個主題。

0_bridges_rock_bridge_slope

1_mass_strength_srm_joint

2_steppath_verification_problem_characterisation

3_steppath_sets_park_lake

4_mechanics step path failure discontinuity_egeote...

5_stress_fracture_elfen_coalescence

6_slopes_brittle_simulation_unstable

7_brittle_fractures_fracture_slope

8_flaws_crack_cracks_coalescence

9_bridges_failure_joints_rock

10_jointed_blocks_mechanical_joint

11_strength_tests_anisotropic_dip

12_al_discontinuities_slope_rock

13_steppath_slopes_fig_visualizations

14_slope_shear_failure_structures

15_slope_brittle_displacement_pit

16_professor_modelling_brittle_fracture

17_cement_granular_particles_particle

18_rockslide_terrestrial_photogrammetry_surface

BERTopic(V0.9.0)主題模擬技術的圖1

5 代表性段落

對產生的每個主題,默認情況下可以列出該主題的三個對應性文檔(即數據集內的段落)。例如,0_bridges_rock_bridge_slope對應的主題詞匯: 

bridges, rock, bridge, slope, analysis, 

stability, failure, fracture, equilibrium, slopes

三個代表性段落如下所示(略)。

6 主題聚類

相似的主題可以聚合在一起,如下圖所示。

BERTopic(V0.9.0)主題模擬技術的圖2

7 相似詞匯

BERTopic能夠在數據集內產生類似Word2Vec的相似詞匯,下面試驗一些例子:

(1) rock joints---rock, failure, fracture, bridge, joint, slopes, analysis

(2) toppling---discontinuities, slope, rock, mechanisms, failure, stability

(3) rockslide---terrestrial, rockfall, photogrammetry, topography,

 geometry, rocky, mountains, reconstruction, debris


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

TOP