import tensorflow as tf
from tensorflow.keras.datasets import mnist
import numpy as np
from sklearn.preprocessing import StandardScaler
print(tf.__version__)
2.0.0
##加载数据 60000条训练集 10000条测试集
(x_train_all, y_train_all), (x_test, y_test) = mnist.load_data()
print(type(x_train_all))
#print((x_train.shape),(x_test.shape)) #(60000, 28, 28) (10000, 28, 28)
#数据归一化
scaler = StandardScaler()
scaled_x_train_all = scaler.fit_transform(x_train_all.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)
scaled_x_test = scaler.transform(x_test.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)
#划分验证集和训练集
scaled_x_train,scaled_x_valid = scaled_x_train_all[5000:],scaled_x_train_all[:5000]
y_train,y_valid = y_train_all[5000:],y_train_all[:5000]
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense
a = Input(shape=(784,)) #单条数据维度,不包括数据总数
#构建多层神经网络
b = Dense(300,activation='relu')(a)
c = Dense(200,activation='relu')(b)
d = Dense(100,activation='relu')(c)
e = Dense(10)(d)
f = Dense(10,activation='softmax')(e) #最后一层softmax分类
model = Model(a,f)
model.summary()
Model: "model_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_2 (InputLayer) [(None, 784)] 0
_________________________________________________________________
dense_5 (Dense) (None, 300) 235500
_________________________________________________________________
dense_6 (Dense) (None, 200) 60200
_________________________________________________________________
dense_7 (Dense) (None, 100) 20100
_________________________________________________________________
dense_8 (Dense) (None, 10) 1010
_________________________________________________________________
dense_9 (Dense) (None, 10) 110
=================================================================
Total params: 316,920
Trainable params: 316,920
Non-trainable params: 0
_________________________________________________________________
model.compile(loss="sparse_categorical_crossentropy",optimizer = "sgd",metrics=["accuracy"]) #sparse_categorical_crossentropy可以将target由数字编码自动变为one-hot编码 categorical_crossentropy用于你target本身就是one-hot编码的情况
model.fit(scaled_x_train.reshape(-1,784),y_train,epochs=100,validation_data=(scaled_x_valid.reshape(-1,784),y_valid),verbose=2)
过程信息太长,省略....
#测试集评估
model.evaluate(scaled_x_test.reshape(-1,784),y_test,verbose=0) #verbose是否打印相关信息
[0.1206463799060793, 0.9783]
#随机选中图片测试
img_random = scaled_x_test[np.random.randint(0,len(scaled_x_test))]
import matplotlib.pyplot as plt
%matplotlib inline
plt.imshow(img_random)
plt.show()
#模型预测
prob = model.predict(img_random.reshape(-1,784))
print(np.argmax(prob))
9
明显最终结果要比只用softmax分类要好,准确率较高