首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用BERT对相似句子进行聚类

如何使用BERT对相似句子进行聚类
EN

Stack Overflow用户
提问于 2019-04-11 02:31:21
回答 5查看 18.8K关注 0票数 23

对于ElMo,FastText和Word2Vec,我平均句子中的单词嵌入,并使用HDBSCAN/KMeans聚类来对相似的句子进行分组。

在这篇简短的文章中可以看到一个很好的实现示例:http://ai.intelligentonlinetools.com/ml/text-clustering-word-embedding-machine-learning/

我想用BERT做同样的事情(使用hugging face中的BERT python包),但是我不太熟悉如何提取原始的单词/句子向量,以便将它们输入到聚类算法中。我知道BERT可以输出句子表示-那么我如何真正从句子中提取原始向量呢?

任何信息都会有帮助。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2021-08-10 14:28:09

作为Subham Kumar mentioned,用户可以使用以下Python3库来计算句子相似度:https://github.com/UKPLab/sentence-transformers

该库有几个用于执行集群的code examples

fast_clustering.py

代码语言:javascript
复制
"""
This is a more complex example on performing clustering on large scale dataset.

This examples find in a large set of sentences local communities, i.e., groups of sentences that are highly
similar. You can freely configure the threshold what is considered as similar. A high threshold will
only find extremely similar sentences, a lower threshold will find more sentence that are less similar.

A second parameter is 'min_community_size': Only communities with at least a certain number of sentences will be returned.

The method for finding the communities is extremely fast, for clustering 50k sentences it requires only 5 seconds (plus embedding comuptation).

In this example, we download a large set of questions from Quora and then find similar questions in this set.
"""
from sentence_transformers import SentenceTransformer, util
import os
import csv
import time


# Model for computing sentence embeddings. We use one trained for similar questions detection
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')

# We donwload the Quora Duplicate Questions Dataset (https://www.quora.com/q/quoradata/First-Quora-Dataset-Release-Question-Pairs)
# and find similar question in it
url = "http://qim.fs.quoracdn.net/quora_duplicate_questions.tsv"
dataset_path = "quora_duplicate_questions.tsv"
max_corpus_size = 50000 # We limit our corpus to only the first 50k questions


# Check if the dataset exists. If not, download and extract
# Download dataset if needed
if not os.path.exists(dataset_path):
    print("Download dataset")
    util.http_get(url, dataset_path)

# Get all unique sentences from the file
corpus_sentences = set()
with open(dataset_path, encoding='utf8') as fIn:
    reader = csv.DictReader(fIn, delimiter='\t', quoting=csv.QUOTE_MINIMAL)
    for row in reader:
        corpus_sentences.add(row['question1'])
        corpus_sentences.add(row['question2'])
        if len(corpus_sentences) >= max_corpus_size:
            break

corpus_sentences = list(corpus_sentences)
print("Encode the corpus. This might take a while")
corpus_embeddings = model.encode(corpus_sentences, batch_size=64, show_progress_bar=True, convert_to_tensor=True)


print("Start clustering")
start_time = time.time()

#Two parameters to tune:
#min_cluster_size: Only consider cluster that have at least 25 elements
#threshold: Consider sentence pairs with a cosine-similarity larger than threshold as similar
clusters = util.community_detection(corpus_embeddings, min_community_size=25, threshold=0.75)

print("Clustering done after {:.2f} sec".format(time.time() - start_time))

#Print for all clusters the top 3 and bottom 3 elements
for i, cluster in enumerate(clusters):
    print("\nCluster {}, #{} Elements ".format(i+1, len(cluster)))
    for sentence_id in cluster[0:3]:
        print("\t", corpus_sentences[sentence_id])
    print("\t", "...")
    for sentence_id in cluster[-3:]:
        print("\t", corpus_sentences[sentence_id])

kmeans.py

代码语言:javascript
复制
"""
This is a simple application for sentence embeddings: clustering

Sentences are mapped to sentence embeddings and then k-mean clustering is applied.
"""
from sentence_transformers import SentenceTransformer
from sklearn.cluster import KMeans

embedder = SentenceTransformer('paraphrase-MiniLM-L6-v2')

# Corpus with example sentences
corpus = ['A man is eating food.',
          'A man is eating a piece of bread.',
          'A man is eating pasta.',
          'The girl is carrying a baby.',
          'The baby is carried by the woman',
          'A man is riding a horse.',
          'A man is riding a white horse on an enclosed ground.',
          'A monkey is playing drums.',
          'Someone in a gorilla costume is playing a set of drums.',
          'A cheetah is running behind its prey.',
          'A cheetah chases prey on across a field.'
          ]
corpus_embeddings = embedder.encode(corpus)

# Perform kmean clustering
num_clusters = 5
clustering_model = KMeans(n_clusters=num_clusters)
clustering_model.fit(corpus_embeddings)
cluster_assignment = clustering_model.labels_

clustered_sentences = [[] for i in range(num_clusters)]
for sentence_id, cluster_id in enumerate(cluster_assignment):
    clustered_sentences[cluster_id].append(corpus[sentence_id])

for i, cluster in enumerate(clustered_sentences):
    print("Cluster ", i+1)
    print(cluster)
    print("")

agglomerative.py

代码语言:javascript
复制
"""
This is a simple application for sentence embeddings: clustering

Sentences are mapped to sentence embeddings and then agglomerative clustering with a threshold is applied.
"""
from sentence_transformers import SentenceTransformer
from sklearn.cluster import AgglomerativeClustering
import numpy as np

embedder = SentenceTransformer('paraphrase-MiniLM-L6-v2')

# Corpus with example sentences
corpus = ['A man is eating food.',
          'A man is eating a piece of bread.',
          'A man is eating pasta.',
          'The girl is carrying a baby.',
          'The baby is carried by the woman',
          'A man is riding a horse.',
          'A man is riding a white horse on an enclosed ground.',
          'A monkey is playing drums.',
          'Someone in a gorilla costume is playing a set of drums.',
          'A cheetah is running behind its prey.',
          'A cheetah chases prey on across a field.'
          ]
corpus_embeddings = embedder.encode(corpus)

# Normalize the embeddings to unit length
corpus_embeddings = corpus_embeddings /  np.linalg.norm(corpus_embeddings, axis=1, keepdims=True)

# Perform kmean clustering
clustering_model = AgglomerativeClustering(n_clusters=None, distance_threshold=1.5) #, affinity='cosine', linkage='average', distance_threshold=0.4)
clustering_model.fit(corpus_embeddings)
cluster_assignment = clustering_model.labels_

clustered_sentences = {}
for sentence_id, cluster_id in enumerate(cluster_assignment):
    if cluster_id not in clustered_sentences:
        clustered_sentences[cluster_id] = []

    clustered_sentences[cluster_id].append(corpus[sentence_id])

for i, cluster in clustered_sentences.items():
    print("Cluster ", i+1)
    print(cluster)
    print("")
票数 1
EN

Stack Overflow用户

发布于 2020-07-12 16:48:01

您可以使用Sentence Transformers生成句子嵌入。与从bert- as -service获得的嵌入相比,这些嵌入更有意义,因为它们经过了微调,使得语义相似的句子具有更高的相似性得分。如果要聚类的句子数在数百万或更多,则可以使用基于FAISS的聚类算法,因为vanilla K-means聚类算法需要二次时间。

票数 15
EN

Stack Overflow用户

发布于 2019-06-27 02:28:29

您需要首先为句子生成bert embeddidngs。bert-as-service提供了一种非常简单的方法来生成句子的嵌入。

这就是你如何对bert向量进行geberate,以获得你需要聚类的句子列表。它在bert-as-service存储库中得到了很好的解释:https://github.com/hanxiao/bert-as-service

安装:

代码语言:javascript
复制
pip install bert-serving-server  # server
pip install bert-serving-client  # client, independent of `bert-serving-server`

https://github.com/google-research/bert下载一个预先训练好的模型

启动服务:

代码语言:javascript
复制
bert-serving-start -model_dir /your_model_directory/ -num_worker=4 

生成句子列表的向量:

代码语言:javascript
复制
from bert_serving.client import BertClient
bc = BertClient()
vectors=bc.encode(your_list_of_sentences)

这将为您提供一个向量列表,您可以将它们写入csv并使用任何聚类算法,因为句子被缩减为数字。

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55619176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档