亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當我嘗試使用 python networkx 總結文本文檔時出現錯誤

當我嘗試使用 python networkx 總結文本文檔時出現錯誤

慕絲7291255 2023-03-22 16:02:05
當我嘗試使用 python networkx 總結文本文檔時,我得到了 PowerIterationFailedConvergence:(PowerIterationFailedConvergence(...), 'power iteration failed to converge within 100 iterations') 如下面的代碼所示。代碼“scores = nx.pagerank(sentence_similarity_graph)”中顯示的錯誤def read_article(file_name):    file = open(file_name, "r",encoding="utf8")    filedata = file.readlines()    text=""    for s in filedata:        text=text+s.replace("\n","")        text=re.sub(' +', ' ', text) #remove space        text=re.sub('—',' ',text)        article = text.split(". ")     sentences = []    for sentence in article:#         print(sentence)        sentences.append(sentence.replace("[^a-zA-Z]", "").split(" "))    sentences.pop()    new_sent=[]    for lst in sentences:        newlst=[]        for i in range(len(lst)):            if lst[i].lower()!=lst[i-1].lower():                newlst.append(lst[i])            else:                newlst=newlst        new_sent.append(newlst)    return new_sentdef sentence_similarity(sent1, sent2, stopwords=None):    if stopwords is None:        stopwords = []     sent1 = [w.lower() for w in sent1]    sent2 = [w.lower() for w in sent2]     all_words = list(set(sent1 + sent2))     vector1 = [0] * len(all_words)    vector2 = [0] * len(all_words)     # build the vector for the first sentence    for w in sent1:        if w in stopwords:            continue        vector1[all_words.index(w)] += 1     # build the vector for the second sentence    for w in sent2:        if w in stopwords:            continue        vector2[all_words.index(w)] += 1     return 1 - cosine_distance(vector1, vector2)def build_similarity_matrix(sentences, stop_words):    # Create an empty similarity matrix    similarity_matrix = np.zeros((len(sentences), len(sentences)))
查看完整描述

3 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

也許你現在已經解決了。

問題是您使用向量的時間太長了。您的向量是使用整個詞匯表構建的,這對于模型來說可能太長而無法在僅 100 個周期內收斂(這是 pagerank 的默認值)。

您可以減少詞匯表的長度(您是否檢查過它是否正確刪除了停用詞?)或使用任何其他技術,例如減少不常用的單詞,或使用 TF-IDF。

就我而言,我遇到了同樣的問題,但使用的是 Glove 詞嵌入。300 維無法收斂,使用 100 維模型很容易解決。

您可以嘗試的另一件事是在調用 nx.pagerank 時擴展 max_iter 參數:

nx.pagerank(nx_graph, max_iter=600) # Or any number that will work for you.

默認值為 100 個周期。


查看完整回答
反對 回復 2023-03-22
?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

當算法未能在冪迭代方法的指定迭代次數內收斂到指定的容差時,會發生此錯誤。因此,您可以像這樣在 nx.pagerank() 中增加錯誤容忍度:

nx.pagerank(sentence_similarity_graph,?tol=1.0e-3)

默認值為 1.0e-6。

查看完整回答
反對 回復 2023-03-22
?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

我有同樣的問題。原來sentence_similarity_graph具有“nan”值。怎么解決?


在 def sentence_similarity中:


if np.isnan(1 - cosine_distance(vector1, vector2)):

  return 0

return 1 - cosine_distance(vector1, vector2)


查看完整回答
反對 回復 2023-03-22
  • 3 回答
  • 0 關注
  • 565 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號