Digital learning handwriting recognition through the actual use Tensorflow to learn relevant knowledge Tensorflow
Load data sets
Tensorflow There are many good training data set can be for our use, the first step we first load the data set, the first time use, pycharm will automatically help you set Tensorflow to download data from local
import tensorflow as tf
mnist=tf.keras.datasets.mnist #获得数据集对象
(x_train,y_train),(x_test,y_test)=mnist.load_data()#加载数据集
print(x_train.shape,y_train.shape) #输出数据集
print(x_test.shape,y_test.shape) #输出测试数据集
Export
(60000, 28, 28) (60000,)
(10000, 28, 28) (10000,)
Displaying a preview of the data set
import matplotlib.pyplot as plt
image_index=1234 #显示第几个数据集
plt.imshow(x_train[image_index],cmap='Greys') #加入cmap='Greys'即为显示黑白图像
plt.show()
Show results
Data format conversion set
The data set used file format is no way to go directly to the Tensorflow
Tensorflow input requirement is 32 so the image 32 to be filled to 32 32
as read data is set is 0-255 shaping, so to be converted to floating point type
data regularization is not really understand why the
dimensionality of the data is converted into [n, h, w, c ] [number, height, width, channel]
import numpy as np
#将图片从28*28扩充为32*32
x_train=np.pad(x_train,((0,0),(2,2),(2,2)),'constant',constant_values=0)
#数据类型准换
x_train=x_train.astype('float32')
#数据正则化
x_train/=255
#数据维度转换
x_train=x_train.reshape(x_train.shape[0],32,32,1)
print(x_train.shape)
Dimension Data Output
(60000, 32, 32, 1)