欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tensorflow2.0之keras手写数字识别

程序员文章站 2022-07-13 13:22:56
...
import tensorflow as tf
import tensorflow.keras as keras
import tensorflow.keras.layers as layers
from tensorflow.keras import Sequential
import matplotlib.pyplot as plt
import pandas as pd
from tensorflow.keras.utils import normalize
from tensorflow.keras.utils import to_categorical
import tensorflow.keras.optimizers as optimizers
import tensorflow.keras.losses as losses
import tensorflow.keras.metrics as metrics
  • 导入数据
(x_train, y_train),(x_test, y_test) = tf.keras.datasets.mnist.load_data()

x1_train = x_train.reshape(-1, 784) #改变形状,变一维
x1_test = x_test.reshape(-1, 784)

x_train_nor = normalize(x1_train, axis=1)#归一化
x_test_nor = normalize(x1_test, axis=1)

y_train_onehot = to_categorical(y_train)#标签转为onehot类型
y_test_onthot = to_categorical(y_test)
  • 创建网络
network = Sequential()
network.add(layers.Dense(units=256, kernel_initializer='normal', activation='relu'))
network.add(layers.Dropout(0.5))
network.add(layers.Dense(units=10,kernel_initializer='normal',activation='softmax'))

network.build(input_shape=(4,784))#此处的build是为了创建参数,所以需要设置
  • 查看网络:
network.summary()

tensorflow2.0之keras手写数字识别

  • 训练参数设置以及训练
#设置优化方法, 损失函数, 以及评价指标
network.compile(optimizer=optimizers.Adam(), loss=losses.CategoricalCrossentropy(), metrics=['accuracy'])

#设置训练数据以及标签, 验证集占比重, 循环次数, 批大小, 日志级别 
train_history = network.fit(x=x_train_nor, y=y_train_onehot, validation_split=0.2, epochs=5, batch_size=200, verbose=2)

其中verbose是日志等级: verbose:日志显示
verbose = 0 为不在标准输出流输出日志信息
verbose = 1 为输出进度条记录
verbose = 2 为每个epoch输出一行记录
注意: 默认为 1

Train on 48000 samples, validate on 12000 samples
Epoch 1/5
48000/48000 - 4s - loss: 0.9353 - accuracy: 0.7783 - val_loss: 0.3760 - val_accuracy: 0.9023
Epoch 2/5
48000/48000 - 1s - loss: 0.3890 - accuracy: 0.8895 - val_loss: 0.2828 - val_accuracy: 0.9208
Epoch 3/5
48000/48000 - 1s - loss: 0.3156 - accuracy: 0.9080 - val_loss: 0.2428 - val_accuracy: 0.9312
Epoch 4/5
48000/48000 - 1s - loss: 0.2757 - accuracy: 0.9200 - val_loss: 0.2166 - val_accuracy: 0.9382
Epoch 5/5
48000/48000 - 1s - loss: 0.2461 - accuracy: 0.9281 - val_loss: 0.1941 - val_accuracy: 0.9457

  • 查看训练过程中相关指标的值:可用于后期画图使用
train_history.history

{‘loss’: [0.9352779900034268,
0.3889504156385859,
0.3156389035284519,
0.27566256585220494,
0.24609261999527612],
‘accuracy’: [0.7783125, 0.8895208, 0.9079792, 0.91997916, 0.928125],
‘val_loss’: [0.37601099784175557,
0.28281597085297105,
0.24280174300074578,
0.21655768944571416,
0.19407257350782553],
‘val_accuracy’: [0.9023333, 0.92083335, 0.93116665, 0.9381667, 0.9456667]}

  • 画图:
def show_train_history(history, train, validation):
    plt.plot(history.history[train])
    plt.plot(history.history[validation])
    plt.title('Train History')
    plt.xlabel('epoch')
    plt.ylabel('accuracy')
    plt.legend(['validation','train'],loc='upper left')

tensorflow2.0之keras手写数字识别

  • 网络评估:
scores = network.evaluate(x_test_nor, y_test_onthot)
  • 类别预测
prediction = network.predict_classes(x_test_nor)
  • 查看分类结果
    分类表:
pd.crosstab(y_test, prediction, rownames=['label'], colnames=['prediction'])

tensorflow2.0之keras手写数字识别
预测错误的样本位置:

df = pd.DataFrame({'label':y_test,'predict':prediction})
print(df[(df.label==5)&(df.predict==3)]) #查看是5预测错成3的数据位置

tensorflow2.0之keras手写数字识别