Help us understand the problem. What is going on with this article?

How to visualize a word2vec vector trained by fastText on a tensorboard

More than 1 year has passed since last update.
import os
import tensorflow as tf
import numpy as np
import fastText
from tensorflow.contrib.tensorboard.plugins import projector

# load model                                                                                                                                                   
word2vec = fastText.load_model('./browse_history.bin')

# create a list of vectors                                                                                                         
dim = len(word2vec.get_words())
embedding = np.empty((len(word2vec.get_words()), word2vec.get_dimension()), dtype=np.float32)
for i, word in enumerate(word2vec.get_words()):
    embedding[i] = word2vec.get_word_vector(word)

# setup a TensorFlow session                                                                                                       
sess = tf.InteractiveSession()
X = tf.Variable([0.0], name='browse_history_embedding')

place = tf.placeholder(tf.float32, shape=embedding.shape)
set_x = tf.assign(X, place, validate_shape=False), feed_dict={place: embedding})

# write labels                                                                                                                     
with open(os.path.join('log', 'metadata.tsv'), 'w') as f:
    for word in word2vec.get_words():
        f.write(word + '\n')

# create a TensorFlow summary writer                                                                                               
summary_writer = tf.summary.FileWriter('log', sess.graph)
config = projector.ProjectorConfig()
embedding_conf = config.embeddings.add()
embedding_conf.tensor_name = 'browse_history_embedding:0'
embedding_conf.metadata_path = os.path.join('log', 'metadata.tsv')
projector.visualize_embeddings(summary_writer, config)

# save the model
saver = tf.train.Saver(), os.path.join('log', "model.ckpt"))
  • we can execute above code on jupyter notebook

  • python binding for fastText is one included in fastText original git repository, is not one which is install by command "pip install fasttext"

how to create sprite image file

from PIL import Image
mport math

master_width = 124 * 90   # x thumbnail width
master_height = 124 * 90  # x thumbnail height

master =
    size=(master_width, master_height),
    color=(0,0,0)) # fully transparent

for index, label in enumerate(word2vec.get_words()):
    img = jpgs.get(label) #  jpgs: images file set
    if img:
        i = (index % 124) * 90
        j = int(index / 124) * 90
        print(index, i, j)
        master.paste(img,(i,j)) # paste sprite master image'master.png')
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away