0

I am trying to train my model using glove. My code is as below:

#!/usr/bin/env python3

from __future__ import print_function
import argparse
import pprint
import gensim

from glove import Glove 
from tensorflow.python.keras.utils.data_utils import Sequence
def read_corpus(filename):

delchars = [chr(c) for c in range(256)]
delchars = [x for x in delchars if not x.isalnum()]
delchars.remove(' ')
delchars = ''.join(delchars)

with open(filename, 'r') as datafile:
    for line in datafile:
        yield line.lower().translate(None, delchars).split(' ')


if __name__ == '__main__':


base_path = "/home/hunzala_awan/vocab.pubmed1.txt"

get_data = read_corpus(base_path)

glove = Glove(no_components=100, learning_rate=0.05)
glove.fit(get_data, epochs=10, verbose=True) 

pprint.pprint(glove.most_similar("cancer", number=10))

When I try to run this code, I get the following error:

Traceback (most recent call last): File "mytest3.py", line 36, in glove.fit(get_data, epochs=10, verbose=True) File "/usr/local/lib/python3.5/dist-packages/glove/glove.py", line 86, in fit shape = matrix.shape AttributeError: 'generator' object has no attribute 'shape'

What am I missing? Any help in this issue will be highly appreciated.

Thanks in advance

gojomo
  • 52,260
  • 14
  • 86
  • 115
Hunzla
  • 3
  • 3

1 Answers1

0

I'm not familiar with Glove, but it seems that it can't fit from genberator function. You can try yield it ahead-of-time and convert to list (it will be more memory-consuming):

glove.fit(list(get_data), epochs=10, verbose=True)
Vladimir Sotnikov
  • 1,399
  • 11
  • 13
  • Thanks for the reply, I did this but now I got new error: `Traceback (most recent call last): File "mytest3.py", line 36, in glove.fit(list(get_data), epochs=10, verbose=True) File "mytest3.py", line 25, in read_corpus yield line.lower().translate(None, delchars).split(' ') TypeError: translate() takes exactly one argument (2 given) ` – Hunzla Feb 09 '20 at 17:59