-
-
Save nikopartanen/3ba498ca39b55d483755498371ae77a3 to your computer and use it in GitHub Desktop.
Convert gensim word2vec to tensorboard visualized model, detail: https://eliyar.biz/using-pre-trained-gensim-word2vector-in-a-keras-model-and-visualizing/ This version contains small edits done by Niko Partanen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # encoding: utf-8 | |
| """ | |
| @author: BrikerMan | |
| @contact: eliyar917@gmail.com | |
| @blog: https://eliyar.biz | |
| @version: 1.0 | |
| @license: Apache Licence | |
| @file: w2v_visualizer.py | |
| @time: 2017/7/30 上午9:37 | |
| @comment: Modified by Niko Partanen in 30.11.2017 | |
| """ | |
| import sys, os | |
| from gensim.models import Word2Vec | |
| import tensorflow as tf | |
| import numpy as np | |
| from tensorflow.contrib.tensorboard.plugins import projector | |
| import gensim | |
| def visualize(model, output_path): | |
| meta_file = "w2x_metadata.tsv" | |
| placeholder = np.zeros((len(model.wv.index2word), 300)) | |
| with open(os.path.join(output_path,meta_file), 'wb') as file_metadata: | |
| for i, word in enumerate(model.wv.index2word): | |
| placeholder[i] = model[word] | |
| # temporary solution for https://github.com/tensorflow/tensorflow/issues/9094 | |
| if word == '': | |
| print("Emply Line, should replecaed by any thing else, or will cause a bug of tensorboard") | |
| file_metadata.write("{0}".format('<Empty Line>').encode('utf-8') + b'\n') | |
| else: | |
| file_metadata.write("{0}".format(word).encode('utf-8') + b'\n') | |
| # define the model without training | |
| sess = tf.InteractiveSession() | |
| embedding = tf.Variable(placeholder, trainable = False, name = 'w2x_metadata') | |
| tf.global_variables_initializer().run() | |
| saver = tf.train.Saver() | |
| writer = tf.summary.FileWriter(output_path, sess.graph) | |
| # adding into projector | |
| config = projector.ProjectorConfig() | |
| embed = config.embeddings.add() | |
| embed.tensor_name = 'w2x_metadata' | |
| embed.metadata_path = meta_file | |
| # Specify the width and height of a single thumbnail. | |
| projector.visualize_embeddings(writer, config) | |
| saver.save(sess, os.path.join(output_path,'w2x_metadata.ckpt')) | |
| print('Run `tensorboard --logdir={0}` to run visualize result on tensorboard'.format(output_path)) | |
| if __name__ == "__main__": | |
| """ | |
| Just run `python w2v_visualizer.py word2vec.model visualize_result` | |
| """ | |
| try: | |
| model_path = sys.argv[1] | |
| output_path = sys.argv[2] | |
| except: | |
| print("Please provice model path and output path") | |
| model = gensim.models.KeyedVectors.load_word2vec_format(model_path) | |
| visualize(model, output_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment