《TensorFlow+Keras深度学习人工智能实践应用》林大贵著第六章第七章代码

书本上提供的代码下载地址失效了

第六章 kerasMNIST手写数字识别数据集和第七章Keras多层感知器识别手写数字的代码

单纯的按书上的顺序copy下来的,自己加了点注释

jupyter notebook上实现

import numpy as np
import pandas as pd
from keras.utils import np_utils
np.random.seed(10)
from keras.datasets import mnist
(X_train_image,y_train_label),\
(X_test_image,y_test_label)=mnist.load_data()
print('train data=',len(X_train_image))
print('test data=',len(X_test_image))
print('X_train_image:',X_train_image.shape)
print('y_train_label:',y_train_label.shape)
import matplotlib.pyplot as plt
def plot_image(image):
    fig=plt.gcf()
    fig.set_size_inches(2,2)
    plt.imshow(image,cmap='binary')
    plt.show()

plot_image(X_train_image[0])
y_train_label[0]
import matplotlib.pyplot as plt
def plot_images_labels_prediction(image,labels,prediction,idx,num=10):   
#num要显示的数据项数,默认为10,最大25
    fig=plt.gcf()
    fig.set_size_inches(12,14)
    if num>25:num=25
    for i in range(0,num):
        ax=plt.subplot(5,5,1+i)
        ax.imshow(image[idx],cmap='binary')
        title="label="+str(labels[idx])
        itle='label='+str(labels[idx])
        if(len(prediction)>0):
            title+=",prediction="+str(prediction[idx])
        ax.set_title(title,fontsize=10)
        ax.set_xticks([])
        ax.set_yticks([])
        idx+=1
    plt.show()

plot_images_labels_prediction(X_train_image,y_train_label,[],0,10)
print('X_test_image:',X_test_image.shape)
print('y_test_label:',y_test_label.shape)
plot_images_labels_prediction(X_test_image,y_test_label,[],0,10)
X_Train=X_train_image.reshape(60000,784).astype('float32')
#将28*28的数字图像reshape转换为一维向量,astype转为float
X_Test=X_test_image.reshape(10000,784).astype('float32')
print('X_Train:',X_Train.shape)
print('X_Test:',X_Test.shape)
X_train_image[0]
X_Train_normalize=X_Train/255
X_Test_normalize=X_Test/255
X_Train_normalize[0]
y_train_label[:5]
y_Train_OneHot=np_utils.to_categorical(y_train_label) #np_utils.to_categorical进行热编码转换
y_Test_OneHot=np_utils.to_categorical(y_test_label)

print(y_train_label[:5])#转换前
y_Train_OneHot[:5]#转换后,每个label用10个0,1来表示,对应10个神经元

用多层感知机来训练

#数据预处理
import numpy as np
from keras.utils import np_utils
from keras.datasets import mnist
np.random.seed(10)

(x_train_image,y_train_label),(x_test_image,y_test_label)=mnist.load_data()
x_Train=x_train_image.reshape(60000,784).astype('float32')
x_Test=x_test_image.reshape(10000,784).astype('float32')
x_Train_normalize=x_Train/255
x_Test_normalize=x_Test/255
y_Train_OneHot=np_utils.to_categorical(y_train_label)
y_Test_OneHot=np_utils.to_categorical(y_test_label)

#建立模型
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
model=Sequential()#Sequential模型
#units隐藏层神经元个数,输入层的神经元个数784,使用正态分布的随机数来初始化权重和偏差,激活函数为relu
model.add(Dense(units=1000,input_dim=784,kernel_initializer='normal',activation='relu'))
model.add(Dropout(0.5))

#加入第二层隐藏层
model.add(Dense(units=1000,kernel_initializer='normal',activation='relu'))
model.add(Dropout(0.5))
#输出层的神经元个数10,使用正态分布的随机数来初始化权重和偏差,激活函数为sofmax
model.add(Dense(units=10,kernel_initializer='normal',activation='relu'))
#查看模型
print(model.summary())
#compile对训练模型进行设置
#loss为交叉熵,adam优化器,metrics模型评估的方式是准确率
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

#开始训练
#训练过程会储存在train_history中,x特征值,y真实的label,validation_split训练与验证数据集的比例
#epoch训练周期,batch_size每一批次的数据,
train_history=model.fit(x=x_Train_normalize,y=y_Train_OneHot,validation_split=0.2,
                        epochs=10,batch_size=200,verbose=2)
import matplotlib.pyplot as plt
def show_train_history(train_history,train,validation):
    plt.plot(train_history.history[train])
    plt.plot(train_history.history[validation])
    plt.title('train history')
    plt.xlabel('Epoch')
    plt.ylabel('train')
    plt.legend(['train','validation'],loc='upper left')
    plt.show()
show_train_history(train_history,'acc','val_acc')
show_train_history(train_history,'loss','val_loss')
#评估模型准确率
scores=model.evaluate(x_Test_normalize,y_Test_OneHot)
print('accuracy=',scores[1])

#进行预测
prediction=model.predict_classes(x_Test)
plot_images_labels_prediction(x_test_image,y_test_label,
                              prediction,idx=340) 
#建立混淆矩阵
import pandas as pd
pd.crosstab(y_test_label,prediction,rownames=['label'], colnames=['predict'])

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值