1. 程式人生 > >mnist各種網路研究3 網路組合

mnist各種網路研究3 網路組合

嘗試先訓練幾個獨立的網路,預測的時候再組合到一起:

import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential,Model
from keras.layers import Input,Conv2D,Dense,Dropout,Convolution2D,MaxPooling2D,Flatten,SeparableConv2D,concatenate
from keras.optimizers import Adam
from keras import optimizers,regularizers
import tensorflow as tf
from keras.utils import multi_gpu_model
from keras.callbacks import LearningRateScheduler, TensorBoard, ModelCheckpoint

import time

(x_train,y_train),(x_test,y_test) = mnist.load_data()

x_train = x_train.reshape(-1,28,28,1)/255.0
x_test = x_test.reshape(-1,28,28,1)/255.0

from keras.models import load_model
model = load_model('Net14.h5')
#model.summary()
loss,accuracy = model.evaluate(x_test,y_test)
#print('test loss',loss)
print('test accuracy',accuracy)

model2 = load_model('Net16.h5')
#model2.summary()
loss,accuracy = model2.evaluate(x_test,y_test)
#print('test loss2',loss)
print('test accuracy2',accuracy)

test1 = model.predict(x_test,batch_size=2000, verbose=1)
test2 = model2.predict(x_test,batch_size=2000, verbose=1)
test1 = test1+test2


acc = 0
for i in range(test1.shape[0]):
    c = np.argmax(test1[i])
    if (c==y_test[i]) :
        acc = acc+1

acc = acc/test1.shape[0]
print('acc=',acc)

下面網路都是重新訓練的,所以acc和上一篇記錄的略有差異

網路 acc
Net3 0.9921
Net3_2 0.9921
Net4 0.9883
Net4_2 0.9888
Net14 0.9764
Net14_2 0.971
Net16 0.982
Net14+16 0.9839
Net14+14_2 0.9797
Net14+14_2+16 0.9852
Net14+4 0.9872
Net4+4_2 0.9891
Net3+4 0.9923

結論:
1、差不多準確率的網路結果組合能提高準確率
2、準確率相差較大的網路結果組合反而降低準確率