Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to visualize keras embeddings in tensorboard?

keras has the ability to export some of it's training data in a tensorboard comaptible format using keras.callbacks.TensorBoard

However, it doesn't support the embedding visualisation in tensorboard.

Is there a way around this?

like image 847
Ophir Yoktan Avatar asked Jan 15 '17 19:01

Ophir Yoktan


People also ask

How do you visualize embeds in TensorBoard?

In order to visualize the embeddings, we select PROJECTOR from the dropdown menu on the top right of the TensorBoard dashboard: Image by author.

Can I use TensorBoard with keras?

Using TensorBoard with deep learning frameworksYou can also use it with other frameworks such as Keras, PyTorch and XGBoost, just to mention a few.

How do I visualize a word embed?

To visualize the word embedding, we are going to use common dimensionality reduction techniques such as PCA and t-SNE. To map the words into their vector representations in embedding space, the pre-trained word embedding GloVe will be implemented.

What is embedding projector?

The Embedding Projector offers three commonly used methods of data dimensionality reduction, which allow easier visualization of complex data: PCA, t-SNE and custom linear projections. PCA is often effective at exploring the internal structure of the embeddings, revealing the most influential dimensions in the data.


2 Answers

Found a solution:

import os

import keras
import tensorflow

ROOT_DIR = '/tmp/tfboard'

os.makedirs(ROOT_DIR, exist_ok=True)


OUTPUT_MODEL_FILE_NAME = os.path.join(ROOT_DIR,'tf.ckpt')

# get the keras model
model = get_model()
# get the tensor name from the embedding layer
tensor_name = next(filter(lambda x: x.name == 'embedding', model.layers)).W.name

# the vocabulary
metadata_file_name = os.path.join(ROOT_DIR,tensor_name)

embedding_df = get_embedding()
embedding_df.to_csv(metadata_file_name, header=False, columns=[])

saver = tensorflow.train.Saver()
saver.save(keras.backend.get_session(), OUTPUT_MODEL_FILE_NAME)

summary_writer = tensorflow.train.SummaryWriter(ROOT_DIR)

config = tensorflow.contrib.tensorboard.plugins.projector.ProjectorConfig()
embedding = config.embeddings.add()
embedding.tensor_name = tensor_name
embedding.metadata_path = metadata_file_name
tensorflow.contrib.tensorboard.plugins.projector.visualize_embeddings(summary_writer, config)
like image 59
Ophir Yoktan Avatar answered Oct 01 '22 01:10

Ophir Yoktan


There is pull request with this functionality - https://github.com/fchollet/keras/pull/5247 callback is extended to create visualization for specific embedding layers.

like image 41
Dmitry Ziolkovskiy Avatar answered Oct 01 '22 01:10

Dmitry Ziolkovskiy