Keras库学习记-one 多层感知器

定义模型

Dense类定义完全连接的层

from keras.models import Sequential

from keras.layers import Dense


model = Sequential()

model.add(Dense(unit=12,input_dim=8,activation='relu'))

model.add(Dense(8,activation='relu'))

model.add(Dense(1,activation='sigmoid'))

其中unit(层中神经元个数)、init(初始化方法)、activation(激活函数)

init为将网络权重初始化为均匀分布的小随机数,默认介于0和0.05之间的随机数。也可选择高斯分布产生小随机数。

activation包括ReLu(现研究表明可得更好性能)、sigmoid、tanh(首选)。

                  二分类的输出层通常采用sigmoid作为激活函数,单层神经网络中使用sgn,多分类使用softmax 。

                  回归问题的输出层不设置激活函数。


编译模型

指定训练模型时所需的一些属性。

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

其中loss(评估一组权重的损失函数)、optimizer(搜索网络不同权重的优化器)

loss对于二进制分类问题的对数损失函数被定义为二进制交叉熵('binary_crossentropy')。

optimizer优化器选取梯度下降法('adam'),默认值。


训练模型

model.fit(x=x , y=Y , epochs=150 , batch_size=10)

其中epochs(固定迭代次数)、batch_size(执行神经网络中的权重更新的每个批次中所用实例的个数)


评估模型

scores = model.evaluate(x=x , y=Y)

print('%s: %.2f%%' % (model.metrics_names[1],scores[1]*100))

evaluate()函数用来评估模型的精确度 


深度学习模型调参

from sklearn.model_selection import GridSearchCV

from keras.wrappers.scikit_learn import KerasClassifier


def create_model(optimizer='adam',init='glorot_uniform'):

      model = Sequential()

      model.add(Dense(12,kernel_initializer=init,input_dim=8,activation='relu'))

      model.add(Dense(8,kernel_initializer=init,activation='relu'))

      model.add(Dense(1,kernel_initializer=init,activation='sigmoid'))

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

      return model

seed = 7

np.random.seed(seed)

dataset = np.loadtxt('C:\\Users\\ThinkPad\\Desktop\\Keras实践\\多层感知器\\pima-indians-diabetes.csv',delimiter=',')

x = dataset[:,0:8]

y = dataset[:,8]

model = KerasClassifier(build_fn=create_model,verbose=0)

param_grid = {}

param_grid['optimizer'] = ['rmsprop','adam']

param_grid['init'] = ['glorot_uniform','normal','uniform']

param_grid['epochs'] = [50,100,150,200]

param_grid['batch_size'] = [5,10,20]

grid = GridSearchCV(estimator=model,param_grid=param_grid)

results = grid.fit(x,y)

print('Best: %f using %s' % (results.best_score_, results.best_params_))

means = results.cv_results_['mean_test_scores']

stds = results.cv_results_['std_test_scores']

params = results.cv_results_['params']

for mean,std,param in zip(means,stds,params):

         print('%f (%f) with: %r' % (mean,std,param))


Pipeline框架

便于在交叉验证的每一个折中执行数据标准化处理。

首先标准化数据集,然后创建和评估基线神经网络模型。

steps = []

steps.append(('standardize',StandardScaler()))

steps.append(('mlp',model))

pipeline = Pipeline(steps)

kfold = KFold(n_splits=10,shuffle=True,random_state=seed)

results = cross_val_score(pipeline,x,y,cv=kfold)


模型增量更新

为了保证模型的时效性,需要定期对模型进行更新,这个时间间隔通常是3~6个月,甚至1~2个月。

在数据量非常大是,若每次采用全部数据取重新训练模型,则时间开销非常大,因此可以采用增量更新模型的方式对模型进行训练。

对于时间序的预测,增量更新相当于默认给最新的数据增加了权重,提高精确度。

from sklearn.model_selection import train_test_split

x_train,x_increment,Y_train,Y_increment = train_test_split(x,Y,test_size=0.2,random_state=seed)

······

new_model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['accuracy'])

Y_increment_labels =to_categorical(Y_increment,num_classes=3) 

new_model.fit(x_increment,Y_increment_labels,epochs=10,batch_size=5,verbose=2) 

scores = new_model.evaluate(x_increment,Y_increment_labels,verbose=0) 

print('Incremrnt %s: %.2f%%' % (model.metrics_names[1],scores[1]*100))  


神经网络的检查点

from keras.callbacks import ModelCheckpoint

filepath = 'weights-improvment-{epoch:02d}-{val_acc:.2f}.h5'

checkpoint = ModelCheckpoint(filepath=filepath,monitor='val_acc',verbose=1,save_best_only=False,mode='max')

''''''

#自动保存最优模型

filepath = 'weights.best.h5'

checkpoint = ModelCheckpoint(filepath=filepath,monitor='val_acc',verbose=1,save_best_only=True,mode='max')

''''''

callback_list = [checkpoint]

model.fit(x,Y_labels,validation_split=0.2,epochs=200,batch_size=5,verbose=0,callbacks=callback_list)

从检查点导入模型

def load_model(optimizer='rmsprop',init='glorot_uniform'):

    model = Sequential()     model.add(Dense(units=4,activation='relu',input_dim=4,kernel_initializer=init)) 

    model.add(Dense(units=6,activation='relu',kernel_initializer=init))     model.add(Dense(units=3,activation='softmax',kernel_initializer=init)) 

    filepath = 'weights.best.h5'

    model.load_weights(filepath=filepath)     model.compile(loss='categorical_crossentropy',optimizer=optimizer,metrics=['accuracy'])

     return model

#导入模型

model = load_model()

scores = model.evaluate(x,Y_labels,verbose=0)

print('%s: %.2f%%' % (model.metrics_names[1],scores[1]*100))


Drop与学习率衰减

drop针对神经网络模型的正则化方法。即在训练过程中,随机地忽略部分神经元。

输入层使用Dropout

dropout率设置为20%,这意味着每个更新周期中20%的输入将被随机排除。

def create_model(optimizer='rmsprop',init='glorot_uniform'):

    model = Sequential()

    model.add(Dropout(rate=0.2,input_shape=(4,)))

    model.add(Dense(units=4,activation='relu',input_dim=4,kernel_initializer=init))

    model.add(Dense(units=6,activation='relu',kernel_initializer=init))

    model.add(Dense(units=3,activation='softmax',kernel_initializer=init))


    sgd = SGD(lr=0.01,momentum=0.8,decay=0.0,nesterov=False)  #定义Dropout


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


    return model

隐含层使用Dropout

def create_model(init='glorot_uniform'):

    model = Sequential()

    model.add(Dense(units=4,activation='relu',input_dim=4,kernel_initializer=init,kernel_constraint=maxnorm(3))) #约束权重最大限度不超过3

    model.add(Dropout(rate=0.2))

    model.add(Dense(units=6,activation='relu',input_dim=4,kernel_initializer=init,kernel_constraint=maxnorm(3)))

    model.add(Dropout(rate=0.2))

    model.add(Dense(units=3,activation='softmax',kernel_initializer=init))


    sgd = SGD(lr=0.01,momentum=0.8,decay=0.0,nesterov=False)


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


    return model

学习率衰减

# 学习率线性衰减

def create_model(init='glorot_uniform'):

    model = Sequential()

    model.add(Dense(units=4,activation='relu',input_dim=4,kernel_initializer=init))

    model.add(Dense(units=6,activation='relu',kernel_initializer=init))

    model.add(Dense(units=3,activation='softmax',kernel_initializer=init))

    learningRate = 0.1

    momentum = 0.9

    decay_rate = 0.005

    sgd = SGD(lr=learningRate,momentum=momentum,decay=decay_rate,nesterov=False)

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

    return model

epochs = 200

model = KerasClassifier(build_fn = create_model,epochs=epochs,batch_size=5,verbose=1)

model.fit(x,Y)


# 学习率指数衰减

def step_decay(epoch):

    init_lrate = 0.1

    drop = 0.5

    epochs_drop = 10

    lrate = init_lrate*pow(drop,floor(1+epoch)/epochs_drop)

    return lrate

def create_model(init='glorot_uniform'):

    model = Sequential()

    model.add(Dense(units=4,activation='relu',input_dim=4,kernel_initializer=init))

    model.add(Dense(units=6,activation='relu',kernel_initializer=init))

    model.add(Dense(units=3,activation='softmax',kernel_initializer=init))

    learningRate = 0.1

    momentum = 0.9

    decay_rate = 0.0

    sgd = SGD(lr=learningRate,momentum=momentum,decay=decay_rate,nesterov=False)

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

    return model

lrate = LearningRateScheduler(step_decay)

epochs = 200

model = KerasClassifier(build_fn = create_model,epochs=epochs,batch_size=5,verbose=1,callbacks=[lrate])

model.fit(x,Y)

你可能感兴趣的:(Keras库学习记-one 多层感知器)