Tensorflow细节-P309-高维向量可视化

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import os
from tensorflow.examples.tutorials.mnist import input_data

LOG_DIR = 'log'
SPRITE_FILE = 'mnist_sprite.jpg'
META_FIEL = "mnist_meta.tsv"


def create_sprite_image(images):
    """Returns a sprite image consisting of images passed as argument. Images should be count x width x height"""
    if isinstance(images, list):
        images = np.array(images)
    img_h = images.shape[1]
    img_w = images.shape[2]
    n_plots = int(np.ceil(np.sqrt(images.shape[0])))  # 大图长和宽的值

    spriteimage = np.ones((img_h * n_plots, img_w * n_plots))  # 一块白板

    for i in range(n_plots):
        for j in range(n_plots):
            this_filter = i * n_plots + j
            if this_filter < images.shape[0]:
                this_img = images[this_filter]  # 往里填充
                spriteimage[i * img_h:(i + 1) * img_h,
                j * img_w:(j + 1) * img_w] = this_img

    return spriteimage


mnist = input_data.read_data_sets("./MNIST_data", one_hot=False)
to_visualise = 1 - np.reshape(mnist.test.images, (-1, 28, 28))  # 数字由白变黑
sprite_image = create_sprite_image(to_visualise)
path_for_mnist_sprites = os.path.join(LOG_DIR, SPRITE_FILE)
plt.imsave(path_for_mnist_sprites, sprite_image, cmap='gray')  # 这里写入的是jpg文件
plt.imshow(sprite_image, cmap='gray')


path_for_mnist_metadata = os.path.join(LOG_DIR, META_FIEL)

with open(path_for_mnist_metadata,'w') as f:  # 这里写入的是tsv文件
    f.write("Index\tLabel\n")
    for index,label in enumerate(mnist.test.labels):
        f.write("%d\t%d\n" % (index,label))

你可能感兴趣的:(Tensorflow细节-P309-高维向量可视化)