深度神经网络——神经网络案例

手写数字mnist

60000个训练样本和10000个测试样本,图像是固定大小(28x28像素),其值为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

数据加载

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

在这里插入图片描述

数据处理

在这里插入图片描述

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=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()

在这里插入图片描述
注意:dropout放最后,bn和激活顺序没有要求

模型编译

在这里插入图片描述

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

模型训练

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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='./'

在浏览器中打开指定网址,可查看损失函数和准确率的变化,图结构等

模型测试

score=model.evaluate(x_test,y_test)

在这里插入图片描述

模型保存

model.save('model.h5')
loadmodel=tf.keras.models.load_model('model.h5')
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醋酸洋红就是我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值