标签:手写 测试 classes core line inf learn 训练 mpi
1.手写数字数据集
digits = load_digits() x_data = digits.data.astype(np.float32) y_data = digits.target.astype(np.float32).reshape(-1, 1)
2.图片数据预处理
scaler = MinMaxScaler() x_data = scaler.fit_transform(x_data) print(x_data) x = x_data.reshape(-1, 8, 8, 1) y = OneHotEncoder().fit_transform(y_data).todense() x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0, stratify=y) print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)
得出结构:
3.设计卷积神经网络结构
模型图:
代码如下:
model = Sequential() ks = [3, 3] # 卷积核大小 model.add(Conv2D(filters=16, kernel_size=ks, padding=‘same‘, input_shape=x_train.shape[1:], activation=‘relu‘)) # 池化层 model.add(MaxPool2D(pool_size=(2, 2))) model.add(Dropout(0.25)) # 二层卷积 model.add(Conv2D(filters=32, kernel_size=ks, padding=‘same‘, activation=‘relu‘)) # 池化层 model.add(MaxPool2D(pool_size=(2, 2))) model.add(Dropout(0.25)) # 三层卷积 model.add(Conv2D(filters=64, kernel_size=ks, padding=‘same‘, activation=‘relu‘)) # 四层卷积 model.add(Conv2D(filters=128, kernel_size=ks, padding=‘same‘, activation=‘relu‘)) # 池化层 model.add(MaxPool2D(pool_size=(2, 2))) model.add(Dropout(0.25)) # 平坦层 model.add(Flatten()) # 全连接层 model.add(Dense(128, activation=‘relu‘)) model.add(Dropout(0.25)) # 激活函数 model.add(Dense(10, activation=‘softmax‘)) model.summary()
可得模型结果如图
4.模型训练
代码如下:
model.compile(loss=‘categorical_crossentropy‘, optimizer=‘adam‘, metrics=[‘accuracy‘]) train_history = model.fit(x=x_train, y=y_train, validation_split=0.2, batch_size=300, epochs=10, verbose=2) 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, ‘accuracy‘, ‘val_accuracy‘) # 损失率 show_train_history(train_history, ‘loss‘, ‘val_loss‘)
运行结果如图:
可视化结果如图:
5.模型评价
import pandas as pd import seaborn as sns score = model.evaluate(x_test, y_test)[1] print(‘模型准确率=‘,score) # 预测值 y_pre = model.predict_classes(x_test) y_pre[:10] # 交叉表和交叉矩阵 y_test1 = np.argmax(y_test, axis=1).reshape(-1) y_true = np.array(y_test1)[0] y_true.shape # 交叉表查看预测数据与原数据对比 pd.crosstab(y_true, y_pre, rownames=[‘true‘], colnames=[‘predict‘]) # 交叉矩阵 y_test1 = y_test1.tolist()[0] a = pd.crosstab(np.array(y_test1), y_pre, rownames=[‘Lables‘], colnames=[‘predict‘]) df = pd.DataFrame(a) print(df) sns.heatmap(df, annot=True, cmap="pink_r", linewidths=0.2, linecolor=‘G‘)
可得准确率以及交叉矩阵如图
可得热力图如图:
标签:手写 测试 classes core line inf learn 训练 mpi
原文地址:https://www.cnblogs.com/heiyedeshihouh/p/13085799.html