Keras多層感知器識別手寫數字

2021-08-28 18:55:20 字數 3078 閱讀 6783

from keras.utils import np_utils

import numpy as np

np.random.seed(10);

from keras.datasets import mnist

(x_train_image,y_train_label),(x_test_image,y_test_label)=mnist.load_data();

# 特徵值進行reshape

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;

# 將標籤進行onehot編碼

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

model.add(dense(units=1000,input_dim=784,kernel_initializer='normal',activation='relu'));

model.add(dropout(0.5));

model.add(dense(units=10,kernel_initializer='normal',activation='softmax'));

# 建立模型的摘要

print(model.summary());

# 建立訓練

model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy']);

# train_history=model.fit(x=x_train_normalize,y=y_train_onehot,validation_split=0,epochs=10,batch_size=200,verbose=1,validation_data=[x_test_normalize,y_test_onehot],shuffle=true);

train_history=model.fit(x=x_train_normalize,y=y_train_onehot,validation_split=0.2,epochs=10,batch_size=200,verbose=1,shuffle=true);

# 建立show_train_history顯示過程

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.ylabel(train);

plt.xlabel('epoch');

plt.legend(['train','validation'],loc='upper left');

plt.show();

# show_train_history(train_history,'acc','val_acc');

# 測試資料評估模型準確率

scores=model.evaluate(x_test_normalize,y_test_onehot);

print("accuracy=",scores[1]);

# 進行**

prediction=model.predict_classes(x_test)

# 視覺化

def plot_images_labels_prediction(images,labels,prediction,idx,num=10):

# 設定顯示圖形大小

fig=plt.gcf();

fig.set_size_inches(12,14);

# 如果顯示項數引數大於25,就設定為25

if num>25:

num=25;

for i in range(0,num):

ax=plt.subplot(5,5,i+1);

ax.imshow(images[idx],cmap='binary');

title="label="+str(labels[idx]);

if len(prediction)>0:

title+=",predict="+str(prediction[idx]);

ax.set_title(title,fontsize=10);

# 設定不顯示刻度

ax.set_xticks();

ax.set_yticks();

idx+=1;

plt.show();

plot_images_labels_prediction(x_test_image,y_test_label,prediction,idx=340);

# 利用pandas建立混淆矩陣

import pandas as pd

error_metric=pd.crosstab(y_test_label,prediction,rownames=['label'],colnames=['predict']);

print(error_metric);

df=pd.dataframe();

print(df[(df.label==5)&(df.predict==3)]);

模式識別 多層感知器 MLP

由前面介紹看到,單個感知器能夠完成線性可分資料的分類問題,是一種最簡單的可以 學習 的機器。但他無法解決非線性問題。比如下圖中的xor問題 即 1,1 1,1 屬於同一類,而 1,1 1,1 屬於第二類的問題,不能由單個感知器正確分類。即在minsky和 t的專著 感知器 所分析的 感知器只能解決所...

Keras學習筆記1 MLP多層感知器

from keras.datasets import mnist from matplotlib import pyplot as plt import numpy as np from keras.models import sequential from keras.layers import ...

Keras多層感知機識別MNIST資料集

windows 10,python 3.5 import numpy as np import pandas as pd from keras.utils import np utils from keras.datasets import mnist import matplotlib.pyplo...