Comments (4)
did anyone solve this?
from glove-python.
@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.
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.
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)
- 美女bnh/m
- Support for python 3.7.4 HOT 5
- install glove-python on linux HOT 3
- Glove on multipe documents
- Does Glove have this function ?
- Compile error in Linux Anaconda installation HOT 7
- Unable to install glove_python in virtual environment HOT 1
- Failed installing Glove_python on linux HOT 5
- Fail to use it in Google Colab HOT 3
- Bug in binary search
- unable install on win10 HOT 13
- import error on OSX
- Failed installing on win10 use “pip install glove-python-binary” HOT 1
- Installation in Google Colab HOT 3
- AttributeError: module 'glove.metrics' has no attribute 'read_analogy_file'
- Missing import in example analogy_tasks_evaluation.py
- How to fine-tune the pretrained GloVe vectors on a custom corpus using this library HOT 1
- installation failed on Windows 10 HOT 2
- Can't install glove_python HOT 8
- Can't install using "pip -v install glove_python" or "python .\setup.py install" on Windows 10 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from glove-python.