Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorboard projector visualisation - PCA keeps loading

Tensorboard projector visualisation - PCA keeps hanging.

I wrote a simple NN to predict the class type of iris dataset. NN model works fine.

import pandas as pd 
import numpy as np

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn import preprocessing

import tensorflow as tf
import tensorflow as tf
from tensorflow import keras

iris_data = load_iris()
x = pd.DataFrame(iris_data.data, columns=iris_data.feature_names)
y = pd.DataFrame(iris_data.target, columns=['class'])

encoder = preprocessing.OneHotEncoder(categories='auto')
encoder.fit(y)
#Transform
y_enc = encoder.transform(y).toarray()

x_train, x_test, y_train, y_test = train_test_split(x, y_enc)


model = keras.Sequential()
model.add(keras.layers.Dense(8, name='input_layer', activation=tf.nn.relu, input_shape=(x_train.shape[1],)))
model.add(keras.layers.Dense(4, name='hidden_layer', activation=tf.nn.relu))
model.add(keras.layers.Dense(3, name='out_layer', activation=tf.nn.softmax))

model.compile(optimizer=tf.keras.optimizers.Adam(0.005),
              loss=keras.losses.binary_crossentropy,
              metrics=[keras.metrics.categorical_accuracy])
model.fit(x_train, y_train, epochs=50, verbose=0)
result = model.predict(x_test)

Now I am trying to visualise the output of the test set. Below is the code for Tensorboard projector. I don't know what I am missing but PCA keeps loading even after starting the Tensorboard several minutes ago.

import tensorflow as tf
from tensorflow.contrib.tensorboard.plugins import projector

import numpy as np
import os

LOG_DIR = 'logs'  # FULL PATH HERE!!!

metadata_file = os.path.join(LOG_DIR, 'metadata.tsv')
with open(metadata_file, 'w') as f:
f.write('{}\t{}\n'.format('class_name','class_id'))
with open(metadata_file, 'a') as f:
for i in range(len(y_test)):
    c = np.nonzero(y_test[i])[0][0]
    f.write('{}\t{}\n'.format(iris_data.target_names[c],c))


embedding_var = tf.Variable(result,  name='final_layer_embedding')
sess = tf.Session()
sess.run(embedding_var.initializer)
summary_writer = tf.summary.FileWriter(LOG_DIR)
config = projector.ProjectorConfig()
embedding = config.embeddings.add()
embedding.tensor_name = embedding_var.name

embedding.metadata_path = 'metadata.tsv'

projector.visualize_embeddings(summary_writer, config)
saver = tf.train.Saver([embedding_var])
saver.save(sess, os.path.join(LOG_DIR, 'model.ckpt'), 1)

enter image description here

I googled to understand what I am doing wrong but I could not fix it. Despite being my model is small very I could not visualise. Any help to resolve this problem would be highly appreciable.

like image 956
shakthydoss Avatar asked Nov 06 '18 06:11

shakthydoss


1 Answers

I am answering for my own question.

As suggested by @Tay2510 in the comment.

Same code works after upgrading tensorboard version to 1.12.0 from 1.11.0.

However my tensorflow version remained the the same to 1.11.0.

like image 190
shakthydoss Avatar answered Nov 09 '22 11:11

shakthydoss