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

pip install glove==1.0.0

参考地址

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]

猜你喜欢

转载自blog.csdn.net/ming20101204/article/details/84950061