应用于Tensorflow2.7的,具体结果如图:
数字图片显示:
具体的代码:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
%matplotlib inline
print("Tensorflow version:", tf.__version__)
文件下载的代码:
mnist =tf.keras.datasets.mnist
(train_images,train_labels),(test_images,test_labels)= mnist.load_data()
print("Train image shape:",train_images.shape,"Train label shape:",train_labels.shape)
print("Test image shape:",test_images.shape,"Test label shape:",test_labels.shape)
print("image data:",train_images[1])
print("train labels:",train_labels[1])
数字图片函数定义及实现代码:
def plot_image(image):
plt.imshow(image.reshape(28,28),cmap='binary')
plt.show()
选择train_image[]的可以正常显示图片。
imshow 的图片格式变换信息可以参考:Matplotlib imshow()函数_叫我SKY的博客-优快云博客_plt.imshow()https://blog.youkuaiyun.com/qq_21763381/article/details/100169288