Tensorflow可视化展示

# import modules
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import os

os.environ['CUDA_VISIBLE_DEVICES'] = '0'

# 设置按需使用GPU
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.InteractiveSession(config=config)

mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

# 取出mnist.train中的第二个图片
img1 = mnist.train.images[1]
label1 = mnist.train.labels[1]


print(img1)
print('Before shape =', img1.shape)  # Before shape = (784,)
img1.shape = [28, 28]
print('After shape =', img1.shape)  # After shape = (28, 28)
print("-" * 50)

# 所以这个是数字 3 的图片 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
print('The label is :', label1)  # The label is : [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]

plt.subplot(1, 2, 1)
plt.imshow(img1)
plt.axis('off')  # off不显示坐标轴,默认为on
plt.subplot(1, 2, 2)
plt.imshow(img1)
plt.axis('on')  # on显示坐标轴
plt.show()

输出结果:

                                       

# import modules
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import os

os.environ['CUDA_VISIBLE_DEVICES'] = '0'

# 设置按需使用GPU
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.InteractiveSession(config=config)

mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

# 取出mnist.train中的第二个图片
img1 = mnist.train.images[1]
label1 = mnist.train.labels[1]

print(img1)
print('Before shape =', img1.shape)  # Before shape = (784,)
img1.shape = [28, 28]
print('After shape =', img1.shape)  # After shape = (28, 28)
print("-" * 50)

# 所以这个是数字 3 的图片 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
print('The label is :', label1)  # The label is : [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]

plt.subplot(1, 2, 1)
plt.imshow(img1, cmap="hot") # 'hot' 是热图

plt.subplot(1, 2, 2)
plt.imshow(img1, cmap="gray") # gray是灰度图

plt.show()

输出结果:

                                     

猜你喜欢

转载自blog.csdn.net/qq_36201400/article/details/108477848