Giter Site home page Giter Site logo

Comments (4)

bjornamr avatar bjornamr commented on July 27, 2024

did anyone solve this?

from glove-python.

bjornamr avatar bjornamr commented on July 27, 2024

@thomasj02 I solved it. This is not a clean solution, but it worked on python 3.
It will use a lot of ram, so I will advice not running anything RAM heavy on the side.

` @classmethod
def load_stanford(cls, filename):
"""
Load model from the output files generated by
the C code from http://nlp.stanford.edu/projects/glove/.

    The entries of the word dictionary will be of type
    unicode in Python 2 and str in Python 3.
    """

    dct = {}
    #vectors = array.array('d')
    vectors = []
    # Read in the data.
    temp_array = []
    vector_size = 0
    with io.open(filename, 'r', encoding='utf-8') as savefile:
        for i, line in enumerate(savefile):
            tokens = line.split(' ')
            word = tokens[0]
            entries = tokens[1:]
            vector_size = len(entries)
            dct[word] = i
            #vectors.extend(float(x) for x in entries)
            vectors.append([float(x) for x in entries])
            #temp_array.append([float(x) for x in entries])
            print("temp_array", len(temp_array))

    # Infer word vectors dimensions.
    print("dct keys",len(dct.keys()))
    no_components = len(vectors)


    # Set up the model instance.
    instance = Glove()
    instance.no_components = no_components
    word_vec = np.memmap("word_vec", dtype=np.float32, mode="w+", shape=(len(vectors),vector_size))
    word_vec[:] = vectors[:]
    instance.word_vectors = word_vec
    #instance.word_vectors[:] = np.array(vectors).reshape(no_vectors,no_components)
    print("word_vec_new", instance.word_vectors.shape)
    instance.word_biases = np.memmap("word_biases", dtype=np.float32, mode="w+", shape=len(vectors))
    print("word_biases", instance.word_biases.shape)

    instance.add_dictionary(dct)

    return instance`

from glove-python.

sp4ghet avatar sp4ghet commented on July 27, 2024

image

It looks like there are some unknowns in the original corpus, which means the total size of vectors is different from num_words * dimensions and reshape won't work.

Adding a little catch for <unk> in the case of twitter corpus helped for me.
This is the glove.py file for the Glove class.
https://github.com/maciejkula/glove-python/blob/master/glove/glove.py#L235

    @classmethod
    def load_stanford(cls, filename):
        """
        Load model from the output files generated by
        the C code from http://nlp.stanford.edu/projects/glove/.

        The entries of the word dictionary will be of type
        unicode in Python 2 and str in Python 3.
        """

        dct = {}
        vectors = array.array('d')

        # Read in the data.
        with io.open(filename, 'r', encoding='utf-8') as savefile:
            for i, line in enumerate(savefile):
                tokens = line.split(' ')

                word = tokens[0]
                entries = tokens[1:]
################# This part
                if word == '<unk>':
                    continue
#################
                dct[word] = i
                vectors.extend([float(x) for x in entries])

        # Infer word vectors dimensions.
        no_components = len(entries)
        no_vectors = len(dct)

        # Set up the model instance.
        instance = Glove()
        instance.no_components = no_components
        instance.word_vectors = (np.array(vectors)
                                 .reshape(no_vectors,
                                          no_components))
        instance.word_biases = np.zeros(no_vectors)
        instance.add_dictionary(dct)

        return instance

from glove-python.

sp4ghet avatar sp4ghet commented on July 27, 2024

Actually, on second thought it should probably be less hardcoded and be more like

if word in dct.keys():

from glove-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.