源代码
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
import matplotlib.pyplot as plt
import numpy as np
# 0 读取mnist数据集
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
tf.compat.v1.disable_eager_execution() #因为tensorflow版本问题的不同,所以为保证sess.run正常运行而进行的更改
# 1 读取训练集的一张图片与标签
batch_xs, batch_ys = mnist.train.next_batch(1)
img = tf.reshape(batch_xs, [28, 28])
label = tf.argmax(batch_ys, 1)
# 2 读取测试集的一张图片与标签
img_tt = mnist.test.images[0]
img_t = tf.reshape(img_tt, [28, 28])
label_tt = mnist.test.labels[0]
label_tt = np.reshape(label_tt, [1, 10])
label_t = tf.argmax(label_tt, 1)
with tf.compat.v1.Session() as sess:
# 训练集 打印标签 像素值
print("训练集:%d" % sess.run(label))
im = sess.run(img)
for i in range(28):
for j in range(28):
print("%d " % round(im[i][j]), end='')
print()
# 测试集 打印标签 像素值
print("测试集:%d" % sess.run(label_t))
im_t = sess.run(img_t)
for i in range(28):
for j in range(28):
print("%d " % round(im_t[i][j]), end='')
print()
# 显示图片
plt.figure()
plt.subplot(121)
plt.imshow(im, cmap='gray')
plt.subplot(122)
plt.imshow(im_t, cmap='gray')
plt.show()
运行结果
2021-05-10 00:23:48.679784: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2021-05-10 00:23:48.680059: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
WARNING:tensorflow:From D:/pythonProject/手写数字.py:7: read_data_sets (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as: tensorflow_datasets.load('mnist')
WARNING:tensorflow:From D:\Anaconda3\envs\tensorflow2.2_gpu\lib\site-packages\tensorflow\examples\tutorials\mnist\input_data.py:297: _maybe_download (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
WARNING:tensorflow:From D:\Anaconda3\envs\tensorflow2.2_gpu\lib\site-packages\tensorflow\examples\tutorials\mnist\input_data.py:299: _extract_images (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting MNIST_data/train-images-idx3-ubyte.gz
WARNING:tensorflow:From D:\Anaconda3\envs\tensorflow2.2_gpu\lib\site-packages\tensorflow\examples\tutorials\mnist\input_data.py:304: _extract_labels (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
WARNING:tensorflow:From D:\Anaconda3\envs\tensorflow2.2_gpu\lib\site-packages\tensorflow\examples\tutorials\mnist\input_data.py:112: _dense_to_one_hot (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.one_hot on tensors.
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
WARNING:tensorflow:From D:\Anaconda3\envs\tens