def visualize(self, visual_fld, num_visualize): """ run "'tensorboard --logdir='visualization'" to see the embeddings """ # create the list of num_variable most common words to visualize word2vec_utils.most_common_words(visual_fld, num_visualize) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state(os.path.dirname('checkpoints/checkpoint')) # if that checkpoint exists, restore from checkpoint if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) final_embed_matrix = sess.run(self.embed_matrix) # you have to store embeddings in a new variable embedding_var = tf.Variable(final_embed_matrix[:num_visualize], name='embedding') sess.run(embedding_var.initializer) config = projector.ProjectorConfig() summary_writer = tf.summary.FileWriter(visual_fld) # add embedding to the config file embedding = config.embeddings.add() embedding.tensor_name = embedding_var.name # link this tensor to its metadata file, in this case the first NUM_VISUALIZE words of vocab embedding.metadata_path = 'vocab_' + str(num_visualize) + '.tsv' # saves a configuration file that TensorBoard will read during startup. projector.visualize_embeddings(summary_writer, config) saver_embed = tf.train.Saver([embedding_var]) saver_embed.save(sess, os.path.join(visual_fld, 'model.ckpt'), 1)
def visualize(self, visual_fld, num_visualize): word2vec_utils.most_common_words(visual_fld, num_visualize) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state(os.path.dirname('checkpoints/checkpoint')) # if that checkpoint exists, restore from checkpoint # if ckpt and ckpt.model_checkpoint_path: # saver.restore(sess, ckpt.model_checkpoint_path) final_embed_matrix = sess.run(self.embed_matrix) embedding_var = tf.Variable(final_embed_matrix[:num_visualize], name='embedding') sess.run(embedding_var.initializer) config = projector.ProjectorConfig() summary_writer = tf.summary.FileWriter(visual_fld) embedding = config.embeddings.add() embedding.tensor_name = embedding_var.name embedding.metadata_path = 'vocab_' + str(num_visualize) + '.tsv' projector.visualize_embeddings(summary_writer, config) saver_embed = tf.train.Saver([embedding_var]) saver_embed.save(sess, os.path.join(visual_fld, 'model.ckpt'), 1)
def visualize(self, visual_fd, num_visualize): """Run `tensorboard --logdir visualization` to get embeddings""" # ceate a list of most common words to visualize word2vec_utils.most_common_words(visual_fd, num_visualize) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state( os.path.dirname('checkpoints/checkpoint')) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) final_embed_matrix = sess.run(self.embed_matrix) # Must store embedding in a new variable embeddings = tf.Variable(final_embed_matrix[:num_visualize], name='embedding') sess.run(embeddings.initializer) config = projector.ProjectorConfig() summary_writer = tf.summary.FileWriter(visual_fd) # add embedding to config file embedding = config.embeddings.add() embedding.tensor_name = embeddings.name # link this tensor to its metadata file embedding_metadata_file = f'vocab_{str(num_visualize)}.tsv' # save the config file that the tensorboard will read projector.visualize_embeddings(summary_writer, config) saver_embed = tf.train.Saver([embeddings]) saver_embed.save(sess, os.path.join(visual_fd, 'model.ckpt'), 1)
def visualize2(visual_fld, num_visualize): word2vec_utils.most_common_words(visual_fld, num_visualize) embed_matrix = tf.get_variable('embed_matrix', shape=[VOCAB_SIZE, EMBED_SIZE], initializer=tf.random_uniform_initializer()) saver = tf.train.Saver([embed_matrix]) with tf.Session() as sess: saver.restore(sess, './embed_saved/') final_embed_matrix = sess.run(embed_matrix) embedding_var = tf.Variable(final_embed_matrix[:num_visualize], name='embedding') sess.run(embedding_var.initializer) config = projector.ProjectorConfig() summary_writer = tf.summary.FileWriter(visual_fld) embedding = config.embeddings.add() embedding.tensor_name = embedding_var.name embedding.metadata_path = 'vocab_' + str(num_visualize) + '.tsv' projector.visualize_embeddings(summary_writer, config) saver_embed = tf.train.Saver([embedding_var]) saver_embed.save(sess, os.path.join(visual_fld, 'model.ckpt'), 1)