-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added Word2vec to Tensorflow 2D tensor file #1051
Changes from 6 commits
3215ea4
23fac40
d24406a
77a96c0
103d0d6
fa7dbad
0bf3b85
96d3a58
15ab0e5
6007c29
cc9fb70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2016 Loreto Parisi <loretoparisi@gmail.com> | ||
# Copyright (C) 2016 Silvio Olivastri <silvio.olivastri@gmail.com> | ||
# Copyright (C) 2016 Radim Rehurek <radim@rare-technologies.com> | ||
|
||
""" | ||
USAGE: $ python -m gensim.scripts.word2vec2tensor --input <Word2Vec model file> --output <TSV tensor filename prefix> | ||
Where: | ||
<Word2Vec model file>: Input Word2Vec model | ||
<TSV tensor filename prefix>: 2D tensor TSV output file name prefix | ||
Output: | ||
The script will create two TSV files. A 2d tensor format file, and a Word Embedding metadata file. Both files will | ||
us the --output file name as prefix | ||
This script is used to convert the word2vec format to Tensorflow 2D tensor and metadata formats for Embedding Visualization | ||
To use the generated TSV 2D tensor and metadata file in the Projector Visualizer, please | ||
1) Open http://projector.tensorflow.org/. | ||
2) Choose "Load Data" from the left menu. | ||
3) Select "Choose file" in "Load a TSV file of vectors." and choose you local "_tensor.tsv" file | ||
4) Select "Choose file" in "Load a TSV file of metadata." and choose you local "_metadata.tsv" file | ||
|
||
For more information about TensorBoard TSV format please visit: | ||
https://www.tensorflow.org/versions/master/how_tos/embedding_viz/ | ||
|
||
""" | ||
|
||
import os | ||
import sys | ||
import random | ||
import logging | ||
import argparse | ||
|
||
import gensim | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
def word2vec2tensor(word2vec_model_path,tensor_filename): | ||
''' | ||
Convert Word2Vec mode to 2D tensor TSV file and metadata file | ||
@word2vec_model_path word2vec model | ||
@tensor_filename tensor filename prefix | ||
''' | ||
model = gensim.models.Word2Vec.load_word2vec_format(word2vec_model_path, binary=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better use text format here, or make it optional atleast There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can ask for format as optional like, keeping text format as default There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All done thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, forgot this earlier, space before |
||
outfiletsv = tensor_filename + '_tensor.tsv' | ||
outfiletsvmeta = tensor_filename + '_metadata.tsv' | ||
|
||
with open(outfiletsv, 'w+') as file_vector: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
with open(outfiletsvmeta, 'w+') as file_metadata: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dtto. |
||
for word in model.index2word: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use model.wv.index2word There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did this change get overwritten? |
||
file_metadata.write(word.encode('utf-8') + '\n') | ||
vector_row = '\t'.join(map(str, model[word])) | ||
file_vector.write(vector_row + '\n') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please log the location and name of the written files. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I have added further instructions and logging. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had an error occurring at line 54. TypeError: can't concat str to bytes |
||
|
||
logger.info("2D tensor file saved to %s" % outfiletsv) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tmylk little nitpick, but for the future, prefer |
||
logger.info("Tensor metadata file saved to %s" % outfiletsvmeta) | ||
|
||
if __name__ == "__main__": | ||
logging.basicConfig(format='%(asctime)s : %(threadName)s : %(levelname)s : %(message)s', level=logging.INFO) | ||
logging.root.setLevel(level=logging.INFO) | ||
logger.info("running %s", ' '.join(sys.argv)) | ||
|
||
# check and process cmdline input | ||
program = os.path.basename(sys.argv[0]) | ||
if len(sys.argv) < 2: | ||
print(globals()['__doc__'] % locals()) | ||
sys.exit(1) | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"-i", "--input", required=True, | ||
help="Input word2vec model") | ||
parser.add_argument( | ||
"-o", "--output", required=True, | ||
help="Output tensor file name prefix") | ||
args = parser.parse_args() | ||
|
||
word2vec2tensor(args.input, args.output) | ||
|
||
logger.info("finished running %s", program) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing license (LGPL, like the rest of gensim). @tmylk @loretoparisi