欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Tensorflow可视化展示

程序员文章站 2022-07-14 16:27:53
...
# 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()

输出结果:

                                       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, cmap="hot") # 'hot' 是热图

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

plt.show()

输出结果:

                                     Tensorflow可视化展示