当前位置: 首页>>代码示例>>Python>>正文


Python projector.visualize_embeddings函数代码示例

本文整理汇总了Python中tensorflow.contrib.tensorboard.plugins.projector.visualize_embeddings函数的典型用法代码示例。如果您正苦于以下问题:Python visualize_embeddings函数的具体用法?Python visualize_embeddings怎么用?Python visualize_embeddings使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了visualize_embeddings函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: set_model

    def set_model(self, model):
        if self.embeddings_freq:
            self.saver = tf.train.Saver()

            embeddings_layer_names = self.embeddings_layer_names

            elayers = find_embedding_layers(model.layers)
            if not embeddings_layer_names:
                embeddings_layer_names = [layer.name for layer in elayers]

            embeddings = {layer.name: layer.weights[0] for layer in elayers
                          if layer.name in embeddings_layer_names}

            embeddings_metadata = {}

            if not isinstance(self.embeddings_metadata, str):
                embeddings_metadata = self.embeddings_metadata
            else:
                embeddings_metadata = {layer_name: self.embeddings_metadata
                                       for layer_name in embeddings.keys()}

            config = projector.ProjectorConfig()
            self.embeddings_logs = []

            for layer_name, tensor in embeddings.items():
                embedding = config.embeddings.add()
                embedding.tensor_name = tensor.name

                self.embeddings_logs.append(os.path.join(self.log_dir,
                                                         layer_name + '.ckpt'))

                if layer_name in embeddings_metadata:
                    embedding.metadata_path = embeddings_metadata[layer_name]

            projector.visualize_embeddings(self.writer, config)
开发者ID:NthTensor,项目名称:keras_experiments,代码行数:35,代码来源:tensorboard_embedding.py

示例2: visualize_char

def visualize_char(model, path="/home/aegis/igor/LM4paper/tests/textchar.txt", ):
    chars = open(path, 'r').read().splitlines()
    embedding = np.empty(shape=(len(chars), model.hps.emb_char_size), dtype=np.float32)
    for i, char in enumerate(chars):
        embedding[i] = model.get_char_embedding(char)
    print(embedding)
    print(embedding.shape)

    logdir = "/data/visualog/char/"
    metadata = os.path.join(logdir, "metadata.tsv")

    with open(metadata, "w") as metadata_file:
        for c in chars:
            metadata_file.write("%s\n" % c)

    tf.reset_default_graph()
    with tf.Session() as sess:
        X = tf.Variable([0.0], name='embedding')
        place = tf.placeholder(tf.float32, shape=embedding.shape)
        set_x = tf.assign(X, place, validate_shape=False)
        sess.run(tf.global_variables_initializer())
        sess.run(set_x, feed_dict={place: embedding})

        saver = tf.train.Saver([X])

        saver.save(sess, os.path.join(logdir, 'char.ckpt'))

        config = projector.ProjectorConfig()
        # One can add multiple embeddings.
        embedding = config.embeddings.add()
        embedding.tensor_name = X.name
        # Link this tensor to its metadata file (e.g. labels).
        embedding.metadata_path = metadata
        # Saves a config file that TensorBoard will read during startup.
        projector.visualize_embeddings(tf.summary.FileWriter(logdir), config)
开发者ID:IgorWang,项目名称:RNNLM,代码行数:35,代码来源:visualize.py

示例3: test

	def test(self, step, number=400):  # 256 self.batch_size
		session = sess = self.session
		config = projector.ProjectorConfig()
		if visualize_cluster:
			embedding = config.embeddings.add()  # You can add multiple embeddings. Here just one.
			embedding.tensor_name = self.last_layer.name  # last_dense
			# embedding.tensor_path
			# embedding.tensor_shape
			embedding.sprite.image_path = PATH_TO_SPRITE_IMAGE
			# help(embedding.sprite)
			embedding.sprite.single_image_dim.extend([width, hight])  # if mnist   thumbnail
			# embedding.single_image_dim.extend([28, 28]) # if mnist   thumbnail
			# Link this tensor to its metadata file (e.g. labels).
			embedding.metadata_path = os.path.join(LOG_DIR, 'metadata.tsv')
			# Saves a configuration file that TensorBoard will read during startup.
			projector.visualize_embeddings(self.summary_writer, config)

		run_metadata = tf.RunMetadata()
		run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
		# Calculate accuracy for 256 mnist test images

		test_images, test_labels = self.next_batch(number, session, test=True)

		feed_dict = {self.x: test_images, self.y: test_labels, self.keep_prob: 1., self.train_phase: False}
		# accuracy,summary= self.session.run([self.accuracy, self.summaries], feed_dict=feed_dict)
		accuracy, summary = session.run([self.accuracy, self.summaries], feed_dict, run_options, run_metadata)
		print('\t' * 3 + "Test Accuracy: ", accuracy)
		self.summary_writer.add_run_metadata(run_metadata, 'step #%03d' % step)
		self.summary_writer.add_summary(summary, global_step=step)
开发者ID:duydb2,项目名称:tensorflow-speech-recognition,代码行数:29,代码来源:net.py

示例4: visualize

    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)
开发者ID:XJTUeducation,项目名称:stanford-tensorflow-tutorials,代码行数:35,代码来源:04_word2vec_visualize.py

示例5: generate_embeddings

def generate_embeddings():
    # Import data
    mnist = input_data.read_data_sets(FLAGS.data_dir,
                                      one_hot=True,
                                      fake_data=FLAGS.fake_data)
    sess = tf.InteractiveSession()

    # Input set for Embedded TensorBoard visualization
    # Performed with cpu to conserve memory and processing power
    with tf.device("/cpu:0"):
        embedding = tf.Variable(tf.stack(mnist.test.images[:FLAGS.max_steps], axis=0), trainable=False, name='embedding')

    tf.global_variables_initializer().run()

    saver = tf.train.Saver()
    writer = tf.summary.FileWriter(FLAGS.log_dir + '/projector', sess.graph)

    # Add embedding tensorboard visualization. Need tensorflow version
    # >= 0.12.0RC0
    config = projector.ProjectorConfig()
    embed= config.embeddings.add()
    embed.tensor_name = 'embedding:0'
    embed.metadata_path = os.path.join(FLAGS.log_dir + '/projector/metadata.tsv')
    embed.sprite.image_path = os.path.join(FLAGS.data_dir + '/mnist_10k_sprite.png')

    # Specify the width and height of a single thumbnail.
    embed.sprite.single_image_dim.extend([28, 28])
    projector.visualize_embeddings(writer, config)

    saver.save(sess, os.path.join(
            FLAGS.log_dir, 'projector/a_model.ckpt'), global_step=FLAGS.max_steps)
开发者ID:pramitchoudhary,项目名称:Experiments,代码行数:31,代码来源:tensorboard_embedding_visualization.py

示例6: write_embeddings

    def write_embeddings(self, Wv, name="WordVectors"):
        """Write embedding matrix to the right place.

        Args:
          Wv: (numpy.ndarray) |V| x d matrix of word embeddings
        """
        with tf.Graph().as_default(), tf.Session() as session:
            ##
            # Feed embeddings to tf, and save.
            embedding_var = tf.Variable(Wv, name=name, dtype=tf.float32)
            session.run(tf.global_variables_initializer())

            saver = tf.train.Saver()
            saver.save(session, self.CHECKPOINT_FILE, 0)

            ##
            # Save metadata
            summary_writer = tf.summary.FileWriter(self.LOGDIR)
            config = projector.ProjectorConfig()
            embedding = config.embeddings.add()
            embedding.tensor_name = embedding_var.name
            embedding.metadata_path = self.VOCAB_FILE_BASE
            projector.visualize_embeddings(summary_writer, config)

        msg = "Saved {s0:d} x {s1:d} embedding matrix '{name}'"
        msg += " to LOGDIR='{logdir}'"
        print(msg.format(s0=Wv.shape[0], s1=Wv.shape[1], name=name,
                         logdir=self.LOGDIR))

        print("To view, run:")
        print("\n  tensorboard --logdir=\"{logdir}\"\n".format(logdir=self.LOGDIR))
        print("and navigate to the \"Embeddings\" tab in the web interface.")
开发者ID:divyag9,项目名称:2018-summer-main,代码行数:32,代码来源:tf_embed_viz.py

示例7: save_embeddings

def save_embeddings(model: adagram.VectorModel, output: Path, words: List[str]):
    labels = []
    senses = []
    for word in words:
        for sense, _ in model.word_sense_probs(word):
            labels.append('{} #{}'.format(word, sense))
            v = model.sense_vector(word, sense)
            senses.append(v / np.linalg.norm(v))
    output.mkdir(exist_ok=True)
    labels_path = output.joinpath('labels.tsv')
    labels_path.write_text('\n'.join(labels))
    senses = np.array(senses)

    with tf.Session() as session:
        embedding_var = tf.Variable(senses, trainable=False, name='senses')
        session.run(tf.global_variables_initializer())
        saver = tf.train.Saver()
        saver.save(session, str(output.joinpath('model.ckpt')))

        summary_writer = tf.train.SummaryWriter(str(output))
        config = projector.ProjectorConfig()
        embedding = config.embeddings.add()
        embedding.tensor_name = embedding_var.name
        embedding.metadata_path = str(labels_path)
        projector.visualize_embeddings(summary_writer, config)
开发者ID:lopuhin,项目名称:WSI,代码行数:25,代码来源:tf_embeddings.py

示例8: save_tb_embeddings

def save_tb_embeddings(embeddings_filename):
    f = open(embeddings_filename, 'rb')
    embeddings = pickle.load(f)

    images = embeddings['images']
    zs = embeddings['zs']

    # overwrite Tensorboard log dir if necessary
    if os.path.exists(TB_DIR):
        shutil.rmtree(TB_DIR)
    os.makedirs(TB_DIR)

    # create grid image
    img_width, img_height = save_sprite_image(images)

    with tf.device('cpu:0'):
        # create embedding var
        embedding_var = tf.Variable(initial_value=zs)

        # save projector config
        summary_writer = tf.summary.FileWriter(TB_DIR)
        config = projector.ProjectorConfig()
        embedding = config.embeddings.add()
        embedding.tensor_name = embedding_var.name
        embedding.sprite.image_path = SPRITE_IMAGE_FILENAME
        embedding.sprite.single_image_dim.extend([img_width, img_height])
        projector.visualize_embeddings(summary_writer, config)

        # save embeddings
        sess = tf.Session()
        sess.run(embedding_var.initializer)
        saver = tf.train.Saver([embedding_var])
        saver.save(sess, os.path.join(TB_DIR, 'model.ckpt'))
开发者ID:Dasona,项目名称:DIGITS,代码行数:33,代码来源:gan_embeddings.py

示例9: main

def main(vectors_loc, out_loc, name="spaCy_vectors"):
    meta_file = "{}.tsv".format(name)
    out_meta_file = path.join(out_loc, meta_file)

    print('Loading spaCy vectors model: {}'.format(vectors_loc))
    model = spacy.load(vectors_loc)
    print('Finding lexemes with vectors attached: {}'.format(vectors_loc))
    strings_stream = tqdm.tqdm(model.vocab.strings, total=len(model.vocab.strings), leave=False)
    queries = [w for w in strings_stream if model.vocab.has_vector(w)]
    vector_count = len(queries)

    print('Building Tensorboard Projector metadata for ({}) vectors: {}'.format(vector_count, out_meta_file))

    # Store vector data in a tensorflow variable
    tf_vectors_variable = numpy.zeros((vector_count, model.vocab.vectors.shape[1]))

    # Write a tab-separated file that contains information about the vectors for visualization
    #
    # Reference: https://www.tensorflow.org/programmers_guide/embedding#metadata
    with open(out_meta_file, 'wb') as file_metadata:
        # Define columns in the first row
        file_metadata.write("Text\tFrequency\n".encode('utf-8'))
        # Write out a row for each vector that we add to the tensorflow variable we created
        vec_index = 0
        for text in tqdm.tqdm(queries, total=len(queries), leave=False):
            # https://github.com/tensorflow/tensorflow/issues/9094
            text = '<Space>' if text.lstrip() == '' else text
            lex = model.vocab[text]

            # Store vector data and metadata
            tf_vectors_variable[vec_index] = model.vocab.get_vector(text)
            file_metadata.write("{}\t{}\n".format(text, math.exp(lex.prob) * vector_count).encode('utf-8'))
            vec_index += 1

    print('Running Tensorflow Session...')
    sess = tf.InteractiveSession()
    tf.Variable(tf_vectors_variable, trainable=False, name=name)
    tf.global_variables_initializer().run()
    saver = tf.train.Saver()
    writer = tf.summary.FileWriter(out_loc, sess.graph)

    # Link the embeddings into the config
    config = ProjectorConfig()
    embed = config.embeddings.add()
    embed.tensor_name = name
    embed.metadata_path = meta_file

    # Tell the projector about the configured embeddings and metadata file
    visualize_embeddings(writer, config)

    # Save session and print run command to the output
    print('Saving Tensorboard Session...')
    saver.save(sess, path.join(out_loc, '{}.ckpt'.format(name)))
    print('Done. Run `tensorboard --logdir={0}` to view in Tensorboard'.format(out_loc))
开发者ID:IndicoDataSolutions,项目名称:spaCy,代码行数:54,代码来源:vectors_tensorboard.py

示例10: _add_emb_vis

 def _add_emb_vis(self, embedding_var):
   """Do setup so that we can view word embedding visualization in Tensorboard, as described here:
   https://www.tensorflow.org/get_started/embedding_viz
   Make the vocab metadata file, then make the projector config file pointing to it."""
   train_dir = os.path.join(FLAGS.log_root, "train")
   vocab_metadata_path = os.path.join(train_dir, "vocab_metadata.tsv")
   self._vocab.write_metadata(vocab_metadata_path) # write metadata file
   summary_writer = tf.summary.FileWriter(train_dir)
   config = projector.ProjectorConfig()
   embedding = config.embeddings.add()
   embedding.tensor_name = embedding_var.name
   embedding.metadata_path = vocab_metadata_path
   projector.visualize_embeddings(summary_writer, config)
开发者ID:sra4077,项目名称:RLSeq2Seq,代码行数:13,代码来源:model.py

示例11: testVisualizeEmbeddings

  def testVisualizeEmbeddings(self):
    # Create a dummy configuration.
    config = projector_config_pb2.ProjectorConfig()
    config.model_checkpoint_path = 'test'
    emb1 = config.embeddings.add()
    emb1.tensor_name = 'tensor1'
    emb1.metadata_path = 'metadata1'

    # Call the API method to save the configuration to a temporary dir.
    temp_dir = self.get_temp_dir()
    self.addCleanup(shutil.rmtree, temp_dir)
    writer = writer_lib.FileWriter(temp_dir)
    projector.visualize_embeddings(writer, config)

    # Read the configuratin from disk and make sure it matches the original.
    with gfile.GFile(os.path.join(temp_dir, 'projector_config.pbtxt')) as f:
      config2 = projector_config_pb2.ProjectorConfig()
      text_format.Parse(f.read(), config2)
      self.assertEqual(config, config2)
开发者ID:AliMiraftab,项目名称:tensorflow,代码行数:19,代码来源:projector_api_test.py

示例12: __get_tensorboard_writer

    def __get_tensorboard_writer(self, path):
        tensorboard_writer = tf.summary.FileWriter(path, graph=self.graph, filename_suffix=".bot")

        # set the projector's configuration to add the embedding summary also:
        conf = projector.ProjectorConfig()
        embedding_field = conf.embeddings.add()
        embedding_content_label = conf.embeddings.add()

        # set the tensors to these embedding matrices
        embedding_field.tensor_name = self.field_embedding_matrix.name
        embedding_content_label.tensor_name = self.content_label_embedding_matrix.name

        # add the metadata paths to these embedding_summaries:
        embedding_field.metadata_path = os.path.join("..", "Metadata/fields.vocab")
        embedding_content_label.metadata_path = os.path.join("..", "Metadata/content_labels.vocab")

        # save the configuration file for this
        projector.visualize_embeddings(tensorboard_writer, conf)

        # return the so created tensorboard_writer
        return tensorboard_writer
开发者ID:ChenglongChen,项目名称:natural-language-summary-generation-from-structured-data,代码行数:21,代码来源:Model.py

示例13: run_latent

def run_latent(model, n_images, data_out_path, sprite=True):
	
	tensorboard_path = os.path.join(data_out_path, 'tensorboard')
	saver = tf.train.Saver()
	with tf.Session() as session:

		# Initializer and restoring model.
		session.run(tf.global_variables_initializer())
		check = get_checkpoint(data_out_path)
		saver.restore(session, check)

		# Inputs for tensorboard.
		tf_data = tf.Variable(tf.zeros((n_images, model.z_dim)), name='tf_data')
		input_sample = tf.placeholder(tf.float32, shape=(n_images, model.z_dim))
		set_tf_data = tf.assign(tf_data, input_sample, validate_shape=False)

		if sprite:
			# Sample images.
			gen_samples, sample_z = show_generated(session=session, z_input=model.z_input, z_dim=model.z_dim, output_fake=model.output_gen, n_images=n_images, show=False)
			# Generate sprite of images.
			write_sprite_image(filename=os.path.join(data_out_path, 'gen_sprite.png'), data=gen_samples)
		else:
			sample_z = np.random.uniform(low=-1., high=1., size=(n_images, model.z_dim))

		# Variable for embedding.
		saver_latent = tf.train.Saver([tf_data])
		session.run(set_tf_data, feed_dict={input_sample: sample_z})
		saver_latent.save(sess=session, save_path=os.path.join(tensorboard_path, 'tf_data.ckpt'))

		# Tensorflow embedding.
		config = projector.ProjectorConfig()
		embedding = config.embeddings.add()
		embedding.tensor_name = tf_data.name
		if sprite:
			embedding.metadata_path = os.path.join(data_out_path, 'metadata.tsv')
			embedding.sprite.image_path = os.path.join(data_out_path, 'gen_sprite.png')
		embedding.sprite.single_image_dim.extend([model.image_height, model.image_width])
		projector.visualize_embeddings(tf.summary.FileWriter(tensorboard_path), config)	
开发者ID:AdalbertoCq,项目名称:Pathology-GAN,代码行数:38,代码来源:tools.py

示例14: visualize_embeddings

    def visualize_embeddings(self, sess, tensor, name):
        """
        Visualises an embedding vector into Tensorboard

        :param sess: Tensorflow session object
        :param tensor:  The embedding tensor to be visualizd
        :param name: Name of the tensor
        """

        # make directory if not exist
        if not tf.os.path.exists(self.save_dir):
            tf.os.makedirs(self.save_dir)

        # summary writer
        summary_writer = tf.summary.FileWriter(self.save_dir, graph=tf.get_default_graph())

        # embedding visualizer
        config = projector.ProjectorConfig()
        emb = config.embeddings.add()
        emb.tensor_name = name  # tensor
        emb.metadata_path = tf.os.path.join(self.DEFAULT_META_DATA_DIR, self.meta_file)  # metadata file
        print(tf.os.path.abspath(emb.metadata_path))
        projector.visualize_embeddings(summary_writer, config)
开发者ID:BenJamesbabala,项目名称:deep-atrous-ner,代码行数:23,代码来源:base_data_loader.py

示例15: embedding_view

def embedding_view(EMB, y, EMB_C, sess, opt):

    EMB = [(x / np.linalg.norm(x)).tolist()  for x in EMB]
    EMB_C = [(x / np.linalg.norm(x) ).tolist() for x in EMB_C]


    
    embedding_var = tf.Variable(EMB + EMB_C,  name='Embedding_of_sentence')
    sess.run(embedding_var.initializer)
    EB_summary_writer = tf.summary.FileWriter(opt.log_path)
    config = projector.ProjectorConfig()
    embedding = config.embeddings.add()
    embedding.metadata_path = os.path.join(opt.log_path, 'metadata.tsv')
    projector.visualize_embeddings(EB_summary_writer, config)
    saver = tf.train.Saver([embedding_var])
    saver.save(sess, os.path.join(opt.log_path, 'model2.ckpt'), 1)
    metadata_file = open(os.path.join(opt.log_path, 'metadata.tsv'), 'w')
    metadata_file.write('ClassID\tClass\n')
    for i in range(len(y)):
        metadata_file.write('%06d\t%s\n' % (y[i], opt.class_name[y[i]]))
    for i in range(opt.num_class):
        metadata_file.write('%06d\t%s\n' % (i, "class_"+opt.class_name[i]))
    metadata_file.close()
    print("embedding created")
开发者ID:niurouli,项目名称:SWEM,代码行数:24,代码来源:utils.py


注:本文中的tensorflow.contrib.tensorboard.plugins.projector.visualize_embeddings函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。