keras 正则项 约束项 激活函数 回调函数 预训练模型

激活函数Activations

激活函数可以通过设置单独的激活层实现,也可以在构造层对象时通过传递activation参数实现。

from keras.layers.core import Activation, Dense

model.add(Dense(64))
model.add(Activation('tanh'))

等价于

model.add(Dense(64, activation='tanh'))

也可以通过传递一个逐元素运算的Theano/TensorFlow函数来作为激活函数:

from keras import backend as Kdef tanh(x):
    return K.tanh(x)

model.add(Dense(64, activation=tanh))
model.add(Activation(tanh)

预定义激活函数

  • softmax:对输入数据的最后一维进行softmax,输入数据应形如(nb_samples, nb_timesteps, nb_dims)(nb_samples,nb_dims)

  • softplus

  • softsign

  • relu

  • tanh

  • sigmoid

  • hard_sigmoid

  • linear

高级激活函数

对于简单的Theano/TensorFlow不能表达的复杂激活函数,如含有可学习参数的激活函数,可通过高级激活函数实现,如PReLU,LeakyReLU等




回调函数Callbacks

回调函数是一组在训练的特定阶段被调用的函数集,你可以使用回调函数来观察训练过程中网络内部的状态和统计信息。通过传递回调函数列表到模型的.fit()中,即可在给定的训练阶段调用该函数集中的函数。

【Tips】虽然我们称之为回调“函数”,但事实上Keras的回调函数是一个类,回调函数只是习惯性称呼

CallbackList

keras.callbacks.CallbackList(callbacks=[], queue_length=10)

Callback

keras.callbacks.Callback()

这是回调函数的抽象类,定义新的回调函数必须继承自该类

类属性

  • params:字典,训练参数集(如信息显示方法verbosity,batch大小,epoch数)

  • model:keras.models.Model对象,为正在训练的模型的引用

回调函数以字典logs为参数,该字典包含了一系列与当前batch或epoch相关的信息。

目前,模型的.fit()中有下列参数会被记录到logs中:

  • 在每个epoch的结尾处(on_epoch_end),logs将包含训练的正确率和误差,accloss,如果指定了验证集,还会包含验证集正确率和误差val_acc)val_lossval_acc还额外需要在.compile中启用metrics=['accuracy']

  • 在每个batch的开始处(on_batch_begin):logs包含size,即当前batch的样本数

  • 在每个batch的结尾处(on_batch_end):logs包含loss,若启用accuracy则还包含acc


BaseLogger

keras.callbacks.BaseLogger()

该回调函数用来对每个epoch累加metrics指定的监视指标的epoch平均值

该回调函数在每个Keras模型中都会被自动调用


ProgbarLogger

keras.callbacks.ProgbarLogger()

该回调函数用来将metrics指定的监视指标输出到标准输出上


History

keras.callbacks.History()

该回调函数在Keras模型上会被自动调用,History对象即为fit方法的返回值


ModelCheckpoint

keras.callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=False, mode='auto')

该回调函数将在每个epoch后保存模型到filepath

filepath可以是格式化的字符串,里面的占位符将会被epoch值和传入on_epoch_endlogs关键字所填入

例如,filename若为```weights.{epoch:02d-{val_loss:.2f}}.hdf5,则会生成对应epoch和验证集loss的多个文件。

参数

  • filename:字符串,保存模型的路径

  • monitor:需要监视的值

  • verbose:信息展示模式,0或1

  • save_best_only:当设置为True时,将只保存在验证集上性能最好的模型

  • mode:‘auto’,‘min’,‘max’之一,在save_best_only=True时决定性能最佳模型的评判准则,例如,当监测值为val_acc时,模式应为max,当检测值为val_loss时,模式应为min。在auto模式下,评价准则由被监测值的名字自动推断。

  • save_weights_only:若设置为True,则只保存模型权重,否则将保存整个模型(包括模型结构,配置信息等)


EarlyStopping

keras.callbacks.EarlyStopping(monitor='val_loss', patience=0, verbose=0, mode='auto')

当监测值不再改善时,该回调函数将中止训练

参数

  • monitor:需要监视的量

  • patience:当early stop被激活(如发现loss相比上一个epoch训练没有下降),则经过patience个epoch后停止训练。

  • verbose:信息展示模式

  • mode:‘auto’,‘min’,‘max’之一,在min模式下,如果检测值停止下降则中止训练。在max模式下,当检测值不再上升则停止训练。


RemoteMonitor

keras.callbacks.RemoteMonitor(root='http://localhost:9000')

该回调函数用于向服务器发送事件流,该回调函数需要requests

参数

  • root:该参数为根url,回调函数将在每个epoch后把产生的事件流发送到该地址,事件将被发往root + '/publish/epoch/end/'。发送方法为HTTP POST,其data字段的数据是按JSON格式编码的事件字典。


LearningRateScheduler

keras.callbacks.LearningRateScheduler(schedule)

该回调函数是学习率调度器

参数

  • schedule:函数,该函数以epoch号为参数(从0算起的整数),返回一个新学习率(浮点数)


TensorBoard

keras.callbacks.TensorBoard(log_dir='./logs', histogram_freq=0)

该回调函数是一个可视化的展示器

TensorBoard是TensorFlow提供的可视化工具,该回调函数将日志信息写入TensorBorad,使得你可以动态的观察训练和测试指标的图像以及不同层的激活值直方图。

如果已经通过pip安装了TensorFlow,我们可通过下面的命令启动TensorBoard:

tensorboard --logdir=/full_path_to_your_logs

更多的参考信息,请点击这里

参数

  • log_dir:保存日志文件的地址,该文件将被TensorBoard解析以用于可视化

  • histogram_freq:计算各个层激活值直方图的频率(每多少个epoch计算一次),如果设置为0则不计算。


编写自己的回调函数

我们可以通过继承keras.callbacks.Callback编写自己的回调函数,回调函数通过类成员self.model访问访问,该成员是模型的一个引用。

这里是一个简单的保存每个batch的loss的回调函数:

class LossHistory(keras.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.losses = []    def on_batch_end(self, batch, logs={}):
        self.losses.append(logs.get('loss'))

例子:记录损失函数的历史数据

class LossHistory(keras.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.losses = []    def on_batch_end(self, batch, logs={}):
        self.losses.append(logs.get('loss'))

model = Sequential()
model.add(Dense(10, input_dim=784, init='uniform'))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

history = LossHistory()
model.fit(X_train, Y_train, batch_size=128, nb_epoch=20, verbose=0, callbacks=[history])print history.losses# outputs'''
[0.66047596406559383, 0.3547245744908703, ..., 0.25953155204159617, 0.25901699725311789]

例子:模型检查点

from keras.callbacks import ModelCheckpoint

model = Sequential()
model.add(Dense(10, input_dim=784, init='uniform'))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')'''
saves the model weights after each epoch if the validation loss decreased
'''checkpointer = ModelCheckpoint(filepath="/tmp/weights.hdf5", verbose=1, save_best_only=True)
model.fit(X_train, Y_train, batch_size=128, nb_epoch=20, verbose=0, validation_data=(X_test, Y_test), callbacks=[checkpointer])





初始化方法

初始化方法定义了对Keras层设置初始化权重的方法

不同的层可能使用不同的关键字来传递初始化方法,一般来说指定初始化方法的关键字是init,例如:

model.add(Dense(64, init='uniform'))

预定义初始化方法

  • uniform

  • lecun_uniform: 即有输入节点数之平方根放缩后的均匀分布初始化(LeCun 98).

  • normal

  • identity:仅用于权值矩阵为方阵的2D层(shape[0]=shape[1]

  • orthogonal:仅用于权值矩阵为方阵的2D层(shape[0]=shape[1]),参考Saxe et al.

  • zero

  • glorot_normal:由扇入扇出放缩后的高斯初始化(Glorot 2010

  • glorot_uniform

  • he_normal:由扇入放缩后的高斯初始化(He et al.,2014

  • he_uniform

指定初始化方法传入的可以是一个字符串(必须与上面某种预定义方法匹配),也可以是一个可调用的对象.如果传入可调用的对象,则该对象必须包含两个参数:shape(待初始化的变量的shape)和name(该变量的名字),该可调用对象必须返回一个(Keras)变量,例如K.variable()返回的就是这种变量,下面是例子:

from keras import backend as Kimport numpy as npdef my_init(shape, name=None):
    value = np.random.random(shape)    return K.variable(value, name=name)

model.add(Dense(64, init=my_init))

你也可以按这种方法使用keras.initializations中的函数:

from keras import initializationsdef my_init(shape, name=None):
    return initializations.normal(shape, scale=0.01, name=name)

model.add(Dense(64, init=my_init))




正则项

正则项在优化过程中层的参数或层的激活值添加惩罚项,这些惩罚项将与损失函数一起作为网络的最终优化目标

惩罚项基于层进行惩罚,目前惩罚项的接口与层有关,但Dense, TimeDistributedDense, MaxoutDense, Covolution1D, Covolution2D具有共同的接口。

这些层有三个关键字参数以施加正则项:

  • W_regularizer:施加在权重上的正则项,为WeightRegularizer对象

  • b_regularizer:施加在偏置向量上的正则项,为WeightRegularizer对象

  • activity_regularizer:施加在输出上的正则项,为ActivityRegularizer对象

例子

from keras.regularizers import l2, activity_l2
model.add(Dense(64, input_dim=64, W_regularizer=l2(0.01), activity_regularizer=activity_l2(0.01)))

预定义正则项

keras.regularizers.WeightRegularizer(l1=0., l2=0.)
keras.regularizers.ActivityRegularizer(l1=0., l2=0.)

缩写

keras.regularizers支持以下缩写

  • l1(l=0.01):L1正则项,又称LASSO

  • l2(l=0.01):L2正则项,又称权重衰减或Ridge

  • l1l2(l1=0.01, l2=0.01): L1-L2混合正则项, 又称ElasticNet

  • activity_l1(l=0.01): L1激活值正则项

  • activity_l2(l=0.01): L2激活值正则项

  • activity_l1l2(l1=0.01, l2=0.01): L1+L2激活值正则项

【Tips】正则项通常用于对模型的训练施加某种约束,L1正则项即L1范数约束,该约束会使被约束矩阵/向量更稀疏。L2正则项即L2范数约束,该约束会使被约束的矩阵/向量更平滑,因为它对脉冲型的值有很大的惩罚。【@Bigmoyan】




约束项

来自constraints模块的函数在优化过程中为网络的参数施加约束

Dense, TimeDistributedDense, MaxoutDense, Covolution1D, Covolution2D具有共同的接口。

这些层通过一下关键字施加约束项

  • W_constraint:对主权重矩阵进行约束

  • b_constraint:对偏置向量进行约束

from keras.constraints import maxnorm
model.add(Dense(64, W_constraint = maxnorm(2)))

预定义约束项

  • maxnorm(m=2):最大模约束

  • nonneg():非负性约束

  • unitnorm():单位范数约束, 强制矩阵沿最后一个轴拥有单位范数





Application应用

Kera的应用模块Application提供了带有预训练权重的Keras模型,这些模型可以用来进行预测、特征提取和finetune

模型的预训练权重将下载到~/.keras/models/并在载入模型时自动载入

可用的模型

应用于图像分类的预训练权重训练自ImageNet:

所有的这些模型都兼容Theano和Tensorflow,并会自动基于~/.keras/keras.json的Keras的图像维度进行自动设置。例如,如果你设置image_dim_ordering=tf,则加载的模型将按照TensorFlow的维度顺序来构造,即“Width-Height-Depth”的顺序


示例

利用ResNet50网络进行ImageNet分类

from keras.applications.resnet50 import ResNet50from keras.preprocessing import imagefrom keras.applications.resnet50 import preprocess_input, decode_predictions

model = ResNet50(weights='imagenet')

img_path = 'elephant.jpg'img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
print('Predicted:', decode_predictions(preds))# print: [[u'n02504458', u'African_elephant']]

利用VGG16提取特征

from keras.applications.vgg16 import VGG16from keras.preprocessing import imagefrom keras.applications.vgg16 import preprocess_input

model = VGG16(weights='imagenet', include_top=False)

img_path = 'elephant.jpg'img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

features = model.predict(x)

从VGG19的任意中间层中抽取特征

from keras.applications.vgg19 import VGG19from keras.preprocessing import imagefrom keras.applications.vgg19 import preprocess_inputfrom keras.models import Model

base_model = VGG19(weights='imagenet')
model = Model(input=base_model.input, output=base_model.get_layer('block4_pool').output)

img_path = 'elephant.jpg'img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

block4_pool_features = model.predict(x)

利用新数据集finetune InceptionV3

from keras.applications.inception_v3 import InceptionV3from keras.preprocessing import imagefrom keras.models import Modelfrom keras.layers import Dense, GlobalAveragePooling2Dfrom keras import backend as K# create the base pre-trained modelbase_model = InceptionV3(weights='imagenet', include_top=False)# add a global spatial average pooling layerx = base_model.output
x = GlobalAveragePooling2D()(x)# let's add a fully-connected layerx = Dense(1024, activation='relu')(x)# and a logistic layer -- let's say we have 200 classespredictions = Dense(200, activation='softmax')(x)# this is the model we will trainmodel = Model(input=base_model.input, output=predictions)# first: train only the top layers (which were randomly initialized)# i.e. freeze all convolutional InceptionV3 layersfor layer in base_model.layers:
    layer.trainable = False# compile the model (should be done *after* setting layers to non-trainable)model.compile(optimizer='rmsprop', loss='categorical_crossentropy')# train the model on the new data for a few epochsmodel.fit_generator(...)# at this point, the top layers are well trained and we can start fine-tuning# convolutional layers from inception V3. We will freeze the bottom N layers# and train the remaining top layers.# let's visualize layer names and layer indices to see how many layers# we should freeze:for i, layer in enumerate(base_model.layers):
   print(i, layer.name)# we chose to train the top 2 inception blocks, i.e. we will freeze# the first 172 layers and unfreeze the rest:for layer in model.layers[:172]:
   layer.trainable = Falsefor layer in model.layers[172:]:
   layer.trainable = True# we need to recompile the model for these modifications to take effect# we use SGD with a low learning ratefrom keras.optimizers import SGD
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')# we train our model again (this time fine-tuning the top 2 inception blocks# alongside the top Dense layersmodel.fit_generator(...)

在定制的输入tensor上构建InceptionV3

from keras.applications.inception_v3 import InceptionV3from keras.layers import Input# this could also be the output a different Keras model or layerinput_tensor = Input(shape=(224, 224, 3))  # this assumes K.image_dim_ordering() == 'tf'model = InceptionV3(input_tensor=input_tensor, weights='imagenet', include_top=True)

VGG16模型


keras.applications.vgg16.VGG16(include_top=True, weights='imagenet', input_tensor=None)

参数

  • include_top:是否保留顶层的3个全连接网络

  • weights:None代表随机初始化,即不加载预训练权重。'imagenet'代表加载预训练权重

  • input_tensor:可填入Keras tensor作为模型的图像输出tensor

返回值

Keras 模型对象

参考文献

License

预训练权重由牛津VGG组发布的预训练权重移植而来,基于Creative Commons Attribution License


VGG19模型


keras.applications.vgg19.VGG19(include_top=True, weights='imagenet', input_tensor=None)

参数

  • include_top:是否保留顶层的3个全连接网络

  • weights:None代表随机初始化,即不加载预训练权重。'imagenet'代表加载预训练权重

  • input_tensor:可填入Keras tensor作为模型的图像输出tensor

返回值

Keras 模型对象

参考文献

License

预训练权重由牛津VGG组发布的预训练权重移植而来,基于Creative Commons Attribution License


ResNet50模型


keras.applications.resnet50.ResNet50(include_top=True, weights='imagenet', input_tensor=None)

参数

  • include_top:是否保留顶层的3个全连接网络

  • weights:None代表随机初始化,即不加载预训练权重。'imagenet'代表加载预训练权重

  • input_tensor:可填入Keras tensor作为模型的图像输出tensor

返回值

Keras 模型对象

参考文献

License

预训练权重由Kaiming He发布的预训练权重移植而来,基于MIT License


InceptionV3模型


keras.applications.inception_v3.InceptionV3(include_top=True, weights='imagenet', input_tensor=None)

参数

  • include_top:是否保留顶层的3个全连接网络

  • weights:None代表随机初始化,即不加载预训练权重。'imagenet'代表加载预训练权重

  • input_tensor:可填入Keras tensor作为模型的图像输出tensor

返回值

Keras 模型对象

参考文献

License

预训练权重由我们自己训练而来,基于MIT License




常用数据库

CIFAR10 小图片分类数据集

该数据库具有50,000个32*32的彩色图片作为训练集,10,000个图片作为测试集。图片一共有10个类别。

使用方法

from keras.datasets import cifar10

(X_train, y_train), (X_test, y_test) = cifar10.load_data()

返回值:

两个Tuple

X_trainX_test是形如(nb_samples, 3, 32, 32)的RGB三通道图像数据,数据类型是无符号8位整形(uint8)

Y_train和 Y_test是形如(nb_samples,)标签数据,标签的范围是0~9


CIFAR100 小图片分类数据库

该数据库具有50,000个32*32的彩色图片作为训练集,10,000个图片作为测试集。图片一共有100个类别,每个类别有600张图片。这100个类别又分为20个大类。

使用方法

from keras.datasets import cifar100

(X_train, y_train), (X_test, y_test) = cifar100.load_data(label_mode='fine')

参数

  • label_mode:为‘fine’或‘coarse’之一,控制标签的精细度,‘fine’获得的标签是100个小类的标签,‘coarse’获得的标签是大类的标签

返回值

两个Tuple,(X_train, y_train), (X_test, y_test),其中

  • X_train和X_test:是形如(nb_samples, 3, 32, 32)的RGB三通道图像数据,数据类型是无符号8位整形(uint8)

  • y_train和y_test:是形如(nb_samples,)标签数据,标签的范围是0~9


IMDB影评倾向分类

本数据库含有来自IMDB的25,000条影评,被标记为正面/负面两种评价。影评已被预处理为词下标构成的序列。方便起见,单词的下标基于它在数据集中出现的频率标定,例如整数3所编码的词为数据集中第3常出现的词。这样的组织方法使得用户可以快速完成诸如“只考虑最常出现的10,000个词,但不考虑最常出现的20个词”这样的操作

按照惯例,0不代表任何特定的词,而用来编码任何未知单词

使用方法

from keras.datasets import imdb

(X_train, y_train), (X_test, y_test) = imdb.load_data(path="imdb_full.pkl",
                                                      nb_words=None,
                                                      skip_top=0,
                                                      maxlen=None,
                                                      test_split=0.1)
                                                      seed=113,
                                                      start_char=1,
                                                      oov_char=2,
                                                      index_from=3)

参数

  • path:如果你在本机上已有此数据集(位于'~/.keras/datasets/'+path),则载入。否则数据将下载到该目录下

  • nb_words:整数或None,要考虑的最常见的单词数,任何出现频率更低的单词将会被编码到0的位置。

  • skip_top:整数,忽略最常出现的若干单词,这些单词将会被编码为0

  • maxlen:整数,最大序列长度,任何长度大于此值的序列将被截断

  • seed:整数,用于数据重排的随机数种子

  • start_char:字符,序列的起始将以该字符标记,默认为1因为0通常用作padding

  • oov_char:字符,因nb_wordsskip_top限制而cut掉的单词将被该字符代替

  • index_from:整数,真实的单词(而不是类似于start_char的特殊占位符)将从这个下标开始

返回值

两个Tuple,(X_train, y_train), (X_test, y_test),其中

  • X_train和X_test:序列的列表,每个序列都是词下标的列表。如果指定了nb_words,则序列中可能的最大下标为nb_words-1。如果指定了maxlen,则序列的最大可能长度为maxlen

  • y_train和y_test:为序列的标签,是一个二值list


路透社新闻主题分类

本数据库包含来自路透社的11,228条新闻,分为了46个主题。与IMDB库一样,每条新闻被编码为一个词下标的序列。

使用方法

from keras.datasets import reuters


(X_train, y_train), (X_test, y_test) = reuters.load_data(path="reuters.pkl",
                                                         nb_words=None,
                                                         skip_top=0,
                                                         maxlen=None,
                                                         test_split=0.2,
                                                         seed=113,
                                                         start_char=1,
                                                         oov_char=2,
                                                         index_from=3)

参数的含义与IMDB同名参数相同,唯一多的参数是: test_split,用于指定从原数据中分割出作为测试集的比例。该数据库支持获取用于编码序列的词下标:

word_index = reuters.get_word_index(path="reuters_word_index.pkl")

上面代码的返回值是一个以单词为关键字,以其下标为值的字典。例如,word_index['giraffe']的值可能为1234

数据库将会被下载到'~/.keras/datasets/'+path


MNIST手写数字识别

本数据库有60,000个用于训练的28*28的灰度手写数字图片,10,000个测试图片

使用方法

from keras.datasets import mnist

(X_train, y_train), (X_test, y_test) = mnist.load_data()

返回值

两个Tuple,(X_train, y_train), (X_test, y_test),其中

  • X_train和X_test:是形如(nb_samples, 28, 28)的灰度图片数据,数据类型是无符号8位整形(uint8)

  • y_train和y_test:是形如(nb_samples,)标签数据,标签的范围是0~9

数据库将会被下载到'~/.keras/datasets/'+path

猜你喜欢

转载自blog.csdn.net/liangjiubujiu/article/details/81019374