import tensorflow as tf from tensorflow import keras import matplotlib.pyplot as plt import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 任务 构建手写数字识别机器模型 测试数据MNIST # 下载数据,准备实验数据集和测试模型数据集 (x, y), (x_test, y_test) = tf.keras.datasets.mnist.load_data() # print('x,x_test,y,y_test的纬度分别为:{0},{1},{2},{3}'.format(x.shape, x_test.shape, y.shape, y_test.shape)) # 将实验数据集分割为两部分数据 一部分用来训练 一部分用来验证模型 # x_train, x_val = tf.split(x, num_or_size_splits=[50000, 10000], name='x_data') # print('x_train,x_val纬度信息为:{0},{1}'.format(x_train.shape, x_val.shape)) y_train, y_val = tf.split(y, num_or_size_splits=[50000, 10000], name='y_data') # print('y_train,y_val纬度信息为:{0},{1}'.format(y_train.shape, y_val.shape)) # 探究数据 以训练集为对象进行探究 # x_train_data = next(iter(x_train)) # y_train_data = next(iter(y_train)) # print(x_train_data, y_train_data) # x_train中的数据为[28,28],且数据类型为int类型 需要进行数据预处理 def preprocess(x, y): x = tf.cast(x, dtype=tf.float32) / 255 # 将x转化为[-1,784] x = tf.reshape(x, [-1, 784]) # 将y转化为独热编码形式 每个对象为(10,) y = tf.one_hot(y, depth=10) y = tf.cast(y, dtype=tf.int32) return x, y batchsz = 128 # 以128个数据为一个小批量进行训练 db_train = tf.data.Dataset.from_tensor_slices((x_train, y_train)) db_train = db_train.shuffle(50000).batch(batchsz, drop_remainder=True).map(preprocess) # 随机打散 再分成128个的批量若干 再map db_val = tf.data.Dataset.from_tensor_slices((x_val, y_val)).batch(batchsz, drop_remainder=True).map( preprocess) # 验证模型的数据集非必须打散、分批量 # print(next(iter(db_val))) # 以上预处理数据工作完毕,下一步构建识别图片的模型 model = tf.keras.Sequential([ tf.keras.layers.Dense(512, activation='relu', input_dim=784), tf.keras.layers.Dense(256, activation='relu'), tf.keras.layers.Dense(125, activation='relu'), tf.keras.layers.Dense(64, activation='relu'), tf.keras.layers.Dense(32, activation='relu'), tf.keras.layers.Dense(10, activation='softmax')]) # 使用compile函数 评估模型 model.compile(optimizer='rmsprop', loss='categorical_crossentropy', # 多分类:categorical_crossentropy 二分类:binary_crossentropy # 均方误差回归问题:mse metrics=['accuracy']) # 训练模型 model.fit(db_train, epochs=6, validation_data=db_val, validation_freq=2) # 使用test数据集进行 图片识别测试 从测试数据集提取一个图片 及 图片的真实数字 db_test = tf.data.Dataset.from_tensor_slices((x_test, y_test)) db_test = db_test.map(preprocess) test_img = next(iter(db_test)) # print(test_img[0]) # 预测模型 res = model.predict(test_img[0], batch_size=None, verbose=0, steps=None, callbacks=None) res = res.argmax() img_print = tf.reshape(test_img[0], [28, 28]) # print(img_print) plt.matshow(img_print) plt.show() # print('预测值结果权重最大的概率为数字:{0},该图片为:{1}'.format(res, img_pred))
我感觉我写的这个有个繁琐重复的地方:
在构建计算模型的时候,将x的矩阵方式由(28,28) 转化为了(1,784),
参考:preprocess自定义的函数
然后再最后的测试时候 dao shudaoshu也得做这个工作,而要将预测的图片数字打印出来时候,又将其还原为(28,28)方可打印成原始图片。
有什么方案 欢迎留言讨论
网络世界,不加微信QQ手机,留言沟通
发表评论