Git Product home page Git Product logo

Comments (3)

bakrianoo avatar bakrianoo commented on May 24, 2024 2

Hi. You can simply write all the embeddings into a txt file to use anywhere. I do not know if this could help you or not ?

import gensim
import json

model = gensim.models.Word2Vec.load('model/tweets_sg_100')

# ----- Write to txt File

all_tokens_vecs = {}
for i in range(0,len(model.wv.index2word)):
    all_tokens_vecs[ model.wv.index2word[i] ] = model.wv[model.wv.index2word[i]]

res = open("model.txt", 'w', encoding='utf8')
res.write(json.dumps(all_tokens_vecs))
res.close()

# ---- Read
with open('model.txt', 'r', encoding='utf8') as content_file:
    all_tokens_vecs = json.loads(content_file.read())

print(all_tokens_vecs['محمد'])

from aravec.

waadth avatar waadth commented on May 24, 2024 1

thank you for your effort
I prefer to convert it to .bin extension
however,
I try to do same code for both "full_grams_sg_300_twitter.mdl" and "full_grams_cbow_300_twitter.mdl"
and I had this error

TypeError Traceback (most recent call last)
in ()
12
13 res = open("model.txt", 'w', encoding='utf8')
---> 14 res.write(json.dumps(all_tokens_vecs))
15 res.close()
16

/anaconda3/lib/python3.7/json/init.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
229 cls is None and indent is None and separators is None and
230 default is None and not sort_keys and not kw):
--> 231 return _default_encoder.encode(obj)
232 if cls is None:
233 cls = JSONEncoder

/anaconda3/lib/python3.7/json/encoder.py in encode(self, o)
197 # exceptions aren't as detailed. The list call should be roughly
198 # equivalent to the PySequence_Fast that ''.join() would do.
--> 199 chunks = self.iterencode(o, _one_shot=True)
200 if not isinstance(chunks, (list, tuple)):
201 chunks = list(chunks)

/anaconda3/lib/python3.7/json/encoder.py in iterencode(self, o, _one_shot)
255 self.key_separator, self.item_separator, self.sort_keys,
256 self.skipkeys, _one_shot)
--> 257 return _iterencode(o, 0)
258
259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

/anaconda3/lib/python3.7/json/encoder.py in default(self, o)
177
178 """
--> 179 raise TypeError(f'Object of type {o.class.name} '
180 f'is not JSON serializable')
181

TypeError: Object of type ndarray is not JSON serializable

any help to convert that .mdl file to .bin, please?
I would be very thankful?

from aravec.

abeermohamed1 avatar abeermohamed1 commented on May 24, 2024

thank you for your effort
I prefer to convert it to .bin extension
however,
I try to do same code for both "full_grams_sg_300_twitter.mdl" and "full_grams_cbow_300_twitter.mdl"
and I had this error

TypeError Traceback (most recent call last)
in ()
12
13 res = open("model.txt", 'w', encoding='utf8')
---> 14 res.write(json.dumps(all_tokens_vecs))
15 res.close()
16

/anaconda3/lib/python3.7/json/init.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
229 cls is None and indent is None and separators is None and
230 default is None and not sort_keys and not kw):
--> 231 return _default_encoder.encode(obj)
232 if cls is None:
233 cls = JSONEncoder

/anaconda3/lib/python3.7/json/encoder.py in encode(self, o)
197 # exceptions aren't as detailed. The list call should be roughly
198 # equivalent to the PySequence_Fast that ''.join() would do.
--> 199 chunks = self.iterencode(o, _one_shot=True)
200 if not isinstance(chunks, (list, tuple)):
201 chunks = list(chunks)

/anaconda3/lib/python3.7/json/encoder.py in iterencode(self, o, _one_shot)
255 self.key_separator, self.item_separator, self.sort_keys,
256 self.skipkeys, _one_shot)
--> 257 return _iterencode(o, 0)
258
259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

/anaconda3/lib/python3.7/json/encoder.py in default(self, o)
177
178 """
--> 179 raise TypeError(f'Object of type {o.class.name} '
180 f'is not JSON serializable')
181

TypeError: Object of type ndarray is not JSON serializable

any help to convert that .mdl file to .bin, please?
I would be very thankful?

thank you for your effort
I prefer to convert it to .bin extension
however,
I try to do same code for both "full_grams_sg_300_twitter.mdl" and "full_grams_cbow_300_twitter.mdl"
and I had this error

TypeError Traceback (most recent call last)
in ()
12
13 res = open("model.txt", 'w', encoding='utf8')
---> 14 res.write(json.dumps(all_tokens_vecs))
15 res.close()
16

/anaconda3/lib/python3.7/json/init.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
229 cls is None and indent is None and separators is None and
230 default is None and not sort_keys and not kw):
--> 231 return _default_encoder.encode(obj)
232 if cls is None:
233 cls = JSONEncoder

/anaconda3/lib/python3.7/json/encoder.py in encode(self, o)
197 # exceptions aren't as detailed. The list call should be roughly
198 # equivalent to the PySequence_Fast that ''.join() would do.
--> 199 chunks = self.iterencode(o, _one_shot=True)
200 if not isinstance(chunks, (list, tuple)):
201 chunks = list(chunks)

/anaconda3/lib/python3.7/json/encoder.py in iterencode(self, o, _one_shot)
255 self.key_separator, self.item_separator, self.sort_keys,
256 self.skipkeys, _one_shot)
--> 257 return _iterencode(o, 0)
258
259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

/anaconda3/lib/python3.7/json/encoder.py in default(self, o)
177
178 """
--> 179 raise TypeError(f'Object of type {o.class.name} '
180 f'is not JSON serializable')
181

TypeError: Object of type ndarray is not JSON serializable

any help to convert that .mdl file to .bin, please?
I would be very thankful?

@waadth Hi did you find a fix for the below error appreciated

TypeError: Object of type ndarray is not JSON serializable

from aravec.

Related Issues (15)

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.