Tensorflow学习-MNIST数据集
Softmax
①数据集导入,keras自带的下载或者从某盘提取点击获取数据集,提取码:45yf
#导入数据集
from keras.datasets import mnist
(x_train,y_train),(x_test,y_test)=mnist.load_data('E:/TensorFlow_mnist/MNIST_data/mnist.npz')
print(x_train.shape,type(x_train)) #60000张28*28的图片
print(y_train.shape,type(y_train)) #60000个标签
②图像和数据类型的转化
#将图像28*28的转换成784
X_train = x_train.reshape(60000,784)
X_test = x_test.reshape(10000,784)
print(X_train.shape,type(X_train))
print(X_test.shape,type(X_test))
#将数据转换为float32
X_train=X_train.astype('float32')
X_test=X_test.astype('float32')
#数据归一化
X_train/=255
X_test/=255
③统计训练数据集中各标签的数量并可视化展示
#统计训练数据中的各标签数量
import numpy as np
import matplotlib.pyplot as plt
label,count=np.unique(y_train,return_counts=True)
print(label,count)
#lable的可视化输出
fig = plt.figure()
plt.bar(label,count,width=0.7,align='center')
plt.title("Label Distribution")
plt.xlabel("Label")
plt.ylabel("Co