Deep Neural Networks - Neural Network Cases

handwritten digital mnist

60000 training samples and 10000 testing samples, the images are of fixed size (28x28 pixels) with values ​​from 0-255

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense,Dropout,Activation,BatchNormalization
from tensorflow.keras import utils
#正则化
from tensorflow.keras import regularizers
#数据集
from tensorflow.keras.datasets import mnist

data loading

(x_train,y_train),(x_test,y_test)=mnist.load_data()
plt.figure()
plt.imshow(x_train[1],cmap='gray')

insert image description here

data processing

insert image description here

x_train=x_train.reshape(60000,784)
x_test=x_test.reshape(10000,784)
x_train=x_train.astype('float32')
x_test=x_test.astype('float32')
x_train=x_train/255
x_test=x_test/255
y_train=utils.to_categorical(y_train,10)
y_test=utils.to_categorical(y_test,10)

model building

model=Sequential()
#全连接层:2个隐层,1个输出层
#第一个:512个神经元,先激活后bn,随机失活
model.add(Dense(512,activation='relu',input_shape=(784,)))
model.add(BatchNormalization())
model.add(Dropout(0.2))
#第二个:512个神经元,先bn后激活,随机失活
model.add(Dense(512,kernel_regularizer=regularizers.l2(0.01)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.2))

model.add(Dense(10,activation='softmax'))
model.summary()

insert image description here
Note: dropout is placed at the end, bn and activation order are not required

model compilation

insert image description here

model.compile(optimizer=tf.keras.optimizers.Adam(),loss=tf.keras.losses.CategoricalCrossentropy,metrics=tf.keras.metrics.Accuracy())

model training

history=model.fit(x_train,y_train,epochs=4,batch_size=128,validation_data=(x_test,y_test),verbose=1)

insert image description here

plt.figure()
plt.plot(history.history['loss'],label='train')
plt.plot(history.history['val_loss'],label='val')
plt.legend()
plt.grid()
plt.show()

insert image description here

plt.figure()
plt.plot(history.history['accuracy'],label='train')
plt.plot(history.history['val_accuracy'],label='val')
plt.legend()
plt.grid()
plt.show()

insert image description here

tensorboard=tf.keras.callbacks.tensorboard(log_dir='./graph')
history=model.fit(x_train,y_train,epochs=4,batch_size=128,validation_data=(x_test,y_test),verbose=1,callbacks=[tensorboard])
#指定存在文件的目录,打开下面命令
tensorboard --logdir='./'

Open the specified URL in the browser to view the change of loss function and accuracy rate, graph structure, etc.

model testing

score=model.evaluate(x_test,y_test)

insert image description here

model save

model.save('model.h5')
loadmodel=tf.keras.models.load_model('model.h5')

Guess you like

Origin blog.csdn.net/qq_40527560/article/details/131505287