(1)权重初始化
kernel_initializer='he_normal'
model = keras.Sequential([
layer.Dense(64,activation='relu',kernel_initializer='he_normal',input_shape=(784,)),
layer.Dense(64,activation='relu',kernel_initializer='he_normal'),
layer.Dense(64,activation='relu',kernel_initializer='he_normal'),
layer.Dense(10,activation='softmax')
])
(2)激活函数
relu max(0,x)
sigmoid 将值映射到(0,1),进行二分类
softmax
(3)批标准化
批标准化是解决:模型参数在学习阶段的 变化会使每个隐藏层输出的分布发生变化,改进神经网络的方法。
model = keras.Sequential([
layers.Dense(64,activation='sigmoid',input_shape=(13,)),
layers.BatchNormalization(),
layers.Dense(64,activation='sigmoid'),
layers.BatchNormalization(),
layers.Dense(64,activation='sigmoid'),
layers.BatchNormalization(),
layers.Dense(1)
])
model.summary()
(4)dropout 层
对深层结构的人工神经网络进行优化的方法,在学习过程中将隐藏层的部分权重或输出随机归零的方式来降低节点间依赖性、实现神经网络的正则化、降低网络结构风险并实现优化目标。
model = keras.Sequential([
layers.Dense(64,activation='sigmoid',input_shape=(13,)),
layers.BatchNormalization(),
layers.Dropout(0.1),
layers.Dense(64,activation='sigmoid'),
layers.BatchNormalization(),
layers.Dropout(0.1),
layers.Dense(64,activation='sigmoid'),
layers.BatchNormalization(),
layers.Dropout(0.1),
layers.Dense(1)
])
model.summary()
(5)模型集成
mlp作为基分类器,以软投票的方式集成。
import numpy as np
from tensorflow imprt keras
from tensorflow.keras import layers
from tensorflow.keras.wrappers.scikit_learn import KerasCassifier
from sklearn.ensemble import VotingClassifier
from sklearn.metrics import accuracy_score
(x_train,y_train),(x_test,y_test) = keras.datasets.mnist.load_data()
x_train = x_train.reshape([x_train.shape[0],-1)
x_test = x_test.reshape([x_test.shape[0],-1)
def mlp_model():
model = keras.Sequential([
layers.Dense(64,activation='relu',input_shape=(784,)),
layers.BatchNormalization(),
layers.Dropout(0.1),
layers.Dense(64,activation='relu'),
layers.BatchNormalization(),
layers.Dropout(0.1),
layers.Dense(64,activation='relu'),
layers.BatchNormalization(),
layers.Dropout(0.1),
layers.Dense(10,activation='softmax')])
model.complie(optimizer=keras.optimizer.SGD(),
loss=keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy'])
return model
model1 = KerasClassifier(build_fn=mlp_model,epochs=100,verbose=0)
model2 = KerasClassifier(build_fn=mlp_model,epochs=100,verbose=0)
model3 = KerasClassifier(build_fn=mlp_model,epochs=100,verbose=0)
ensemble_clf = VotingClassifier(eltimaters=[('model1',model1),('model2',model2),('model3',model3),voting='soft')
ensemble_clf.fit(x_train,y_train)
y_pred = ensemble_clf.predict(x_test)
print('acc:',accuracy_score(y_pred,y_test))