这里只是记录一下这个问题。最近又用Gensim+LSI做了一些分类的事情,但是发现如果将LSI中的topic number设置过高,则很有可能会报错,一些示例性的代码如下:
dictionary = corpora.Dictionary(line.lower().split() for line in open(FILE_STRING))
corpus = [dictionary.doc2bow(line.lower().split()) for line in open(FILE_STRING)]
tfidf_model = models.TfidfModel(corpus)
corpus_tfidf = tfidf_model[corpus]
lsi_model = models.LsiModel(corpus_tfidf,id2word=dictionary,num_topics=NUM_TOPICS)
corpus_lsi = lsi_model[corpus_tfidf]
this_corpus=dictionary.doc2bow(this_line.lower().split())#例如这里要对this_line来生成其LSI向量
this_corpus_tfidf=tfidf_model[this_corpus]
this_lsi=lsi_model[this_corpus_tfidf]
print(this_lsi)
vec_list=[]
for each_topic in this_lsi:
vec_list.append(each_topic[1])
print(len(vec_list))#生成之后发现如果NUM_TOPICS设置得太高,则这里的vector会少一个维度,真的太奇怪了。
如我设置NUM_TOPICS==1024,则很容易报下面的错:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 1024 and the array at index 1 has size 1023
所以经验是使用LSI时维度不能设置得太高,否则很多训练数据会不可用。也实在不知道原因在哪里,只是觉得很奇怪!