Search code examples
pythongensimlatent-semantic-indexing

Gensim: ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (0,)


I am trying to emulate streaming for some documents and update the LSI on additional documents streamed-in. I find this error:

Traceback (most recent call last):
  File "gensimStreamGen_tutorial5.py", line 57, in <module>
    for vector in corpus_memory_friendly: # load one vector into memory at a time
  File "gensimStreamGen_tutorial5.py", line 44, in __iter__
    lsi = models.LsiModel(corpus, num_topics=10) # initialize an LSI transformation
  File "/Users/Desktop/gensim-0.12.0/gensim/models/lsimodel.py", line 331, in __init__
    self.add_documents(corpus)
  File "/Users/Desktop/gensim-0.12.0/gensim/models/lsimodel.py", line 388, in add_documents
    update = Projection(self.num_terms, self.num_topics, job, extra_dims=self.extra_samples, power_iters=self.power_iters)
  File "/Users/Desktop/gensim-0.12.0/gensim/models/lsimodel.py", line 126, in __init__
    extra_dims=self.extra_dims)
  File "/Users/Desktop/gensim-0.12.0/gensim/models/lsimodel.py", line 677, in stochastic_svd
    q, _ = matutils.qr_destroy(y) # orthonormalize the range
  File "/Users/Desktop/gensim-0.12.0/gensim/matutils.py", line 398, in qr_destroy
    qr, tau, work, info = geqrf(a, lwork=-1, overwrite_a=True)
ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (0,)

The code for streaming documents and updating LSI model:

class MyCorpus(object):
    def __iter__(self):
        for document in documents:
            # Stream-in documents and build TF-IDF model to construct new_vec
            yield new_vec
            corpus.append(new_vec)
            tfidf = models.TfidfModel(corpus)
            corpus_tfidf = tfidf[corpus]
            lsi = models.LsiModel(corpus_tfidf,  num_topics=2)
            corpus_lsi = lsi[corpus_tfidf]
            lsi.print_topics(2)
            for doc in corpus_lsi:
                print(doc)

corpus_memory_friendly = MyCorpus()
for vector in corpus_memory_friendly:
    print(vector)

The corpus gets a new new_vec every iteration. The new_vec on each yield for different iterations:

[]
[(0, 1)]
[(1, 1), (2, 1), (3, 1)]
[(3, 2), (4, 1), (5, 1)]
[(2, 1), (6, 1), (7, 1)]
[]
[(8, 1)]
[(8, 1), (9, 1)]
[(9, 1), (10, 1), (11, 1)]

The error appears on the first iteration (first line in expected new_vec). The rest is the expected output from new_vec.


Solution

  • I think because of your data in documents have blank try add

    if(document!=[]and document!=[[]])