180512 tensorboard高维数据可视化

Tensoflow实战Google深度学习框架-第2版-第11章 tensorboard可视化
这里写图片描述python

from tensorflow.contrib.tensorboard.plugins import projector

def visualisation(final_result):
    # 【定义变量】定义新的变量来保存最终的数据层向量的结果,用final_result初始化变量y的值
    y = tf.Variable(final_result, name = TENSOR_NAME)summary_writer

    # 【定义总结】
    summary_writer = tf.summary.FileWriter(LOG_DIR)

    # 【建立配置】经过projector.ProjectorConfig()类来帮助生成日志文件
    config = projector.ProjectorConfig()
    # 【添加嵌套】增长一个须要可视化的embedding的结果
    embedding = config.embeddings.add()
    # 【添加名称】指定这个embedding结果对应的Tensorflow变量名称
    embedding.tensor_name = y.name

    # 【指定文件】指定每一个数据的正确标数据文件,可选,没有则无标签
    # Specify where you find the metadata
    embedding.metadata_path = META_FIEL

    # 【指定图片】指定sprite图像,可选,没有则没一个点就是一个小圆点,没有具体的图片,
    # single_image_dim指定单张图片大小,这将保证用于从sprite图像中截取争取的原始图片
    # Specify where you find the sprite (we will create this later)
    embedding.sprite.image_path = SPRITE_FILE
    embedding.sprite.single_image_dim.extend([28,28])

    # 【写入日志】将PROJECTOR所须要带的内容写入日志文件
    # Say that you want to visualise the embeddings
    projector.visualize_embeddings(summary_writer, config)

    #【生成会话,初始化】
    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())

    #【将日志写入文件】
    saver = tf.train.Saver()
    saver.save(sess, os.path.join(LOG_DIR, "model"), TRAINING_STEPS)

    summary_writer.close()