Glove算法安装出错,解决办法

本文介绍如何使用GloVe库进行词嵌入训练,包括预处理文本、创建语料库、设置参数并保存模型。通过实例展示了GloVe算法的工作原理,如窗口大小、学习率和组件数量的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pip install glove

pip install fasttext  安装fasttext包
 

 

参考地址

glove 使用

#importing the glove library
from glove import Corpus, Glove
# creating a corpus object
corpus = Corpus() 
#training the corpus to generate the co occurence matrix which is used in GloVe
corpus.fit(lines, window=10)
#creating a Glove object which will use the matrix created in the above lines to create embeddings
#We can set the learning rate as it uses Gradient Descent and number of components
glove = Glove(no_components=5, learning_rate=0.05)
 
glove.fit(corpus.matrix, epochs=30, no_threads=4, verbose=True)
glove.add_dictionary(corpus.dictionary)
glove.save('glove.model')

The corpus.fit takes two arguments:

  • lines — this is the 2D array we created after the pre-processing
  • window — this is the distance between two words algo should consider to find some relationship between them

Parameters of Glove:

  • no_of_components — This is the dimension of the output vector generated by the GloVe
  • learning_rate — Algo uses gradient descent so learning rate defines the rate at which the algo reaches towards the minima (lower the rate more time it takes to learn but reaches the minimum value)

Parameters of glove.fit :

  • cooccurence_matrix: the matrix of word-word co-occurrences
  • epochs: this defines the number of passes algo makes through the data set
  • no_of_threads: number of threads used by the algo to run

After the training glove object has the word vectors for the lines we have provided. But the dictionary still resides in the corpus object. We need to add the dictionary to the glove object to make it complete.

glove.add_dictionary(corpus.dictionary)

This line does the dictionary addition in the glove object. After this, the object is ready to provide you with the embeddings.

print glove.word_vectors[glove.dictionary['samsung']]
OUTPUT:
[ 0.04521741  0.02455266 -0.06374787 -0.07107575  0.04608054]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值