keras文档学习

序列模型sequence

Model

Keras的函数式模型为==Model==,即广义的拥有输入和输出的模型,我们使用Model来初始化一个函数式模型

from keras.models import Model
from keras.layers import Input, Dense

a = Input(shape=(32,))
b = Dense(32)(a)
#模型的输入是a,输出是b
model = Model(inputs=a, outputs=b)

#多输入和多输出的模型
model = Model(inputs=[a1, a2], outputs=[b1, b3, b3])

常用的model属性

model.layer:组成模型图的各个层
model.inputs:模型的输入张量列表
model.outputs模型的输出张量列表

Model模型的方法

# ==compile== (编译模型以供训练)
compile(self, optimizer, loss, metrics=None, loss_weights=None, sample_weight_mode=None, weighted_metrics=None, target_tensors=None)
##optimizer:优化器
##loss损失函数
##metrics列表。评估模型的性能。metrics=['accuracy']

【Tips】如果你只是载入模型并利用其predict,可以不用进行compile。在Keras中,==compile==主要完成损失函数和优化器的一些配置,是为训练服务的。predict会在内部进行符号函数的编译工作(通过调用_make_predict_function生成函数)
当参数传入非法值时会抛出异常

# ==fit==(训练模型)
fit(self, x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None)
##x:输入数据
##y:标签
##batch_size:指定进行梯度下降时,每个batch包含的样本数。训练时,一个batch的样本会被计算一次梯度下降,使目标函数优化一步。
##epochs:整数,训练终止时epoch值,训练的轮数
##verbose:日志显示,0-不显示,1-输出进度条记录,2-每个epoch输出一行记录
##shuffle:布尔值,表示是否在训练过程中每个epoch前随机打乱输入样本的顺序

输入数据与规定数据不匹配时会报错

# ==evaluate==(batch计算在某些输入数据上模型的误差)
evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None)
##x:输入数据
##y:标签
##batch_size,verbose:含义同fit同名参数,verbose的值只能取0和1

==evaluate==本函数返回一个测试误差的标量值(如果模型没有其他评价指标),或一个标量的list(如果模型还有其他的评价指标)。model.metrics_names将给出list中各个值的含义。

# ==predict ==(本函数按batch获得输入数据对应的输出,函数返回值是numpy array)
predict(self, x, batch_size=32, verbose=0)

回调函数Callbacks(实际上是一个类)

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

# ==Callback==回掉函数的抽象类,定义新的回调函数必须继承自该类
keras.callbacks.Callback()
# ==ModelCheckpoint==
keras.callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1)
# 该回调函数将在每个epoch后保存训练模型到filepath。
## filename:字符串,保存模型的路径
## save_best_only:设置为True时,将只保存在验证集上性能最好的模型
## 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后停止训练。

==model.summary() : 打印出模型概况==


Flatten层

用来将输入“压平”,即把多维的输入一维化,常用在从卷积层到全连接层的过渡。Flatten不影响batch的大小。

model = Sequential()
model.add(Convolution2D(64, 3, 3,
            border_mode='same',
            input_shape=(3, 32, 32)))
# now: model.output_shape == (None, 64, 32, 32)

model.add(Flatten())
# now: model.output_shape == (None, 65536)

嵌入层 ==Embedding==

keras.layers.embeddings.Embedding(input_dim, output_dim, embeddings_initializer='uniform', embeddings_regularizer=None, activity_regularizer=None, embeddings_constraint=None, mask_zero=False, input_length=None)

嵌入层将正整数(下标)转换为具有固定大小的向量,如[[4],[20]]->[[0.25,0.1],[0.6,-0.2]]
Embedding层只能作为模型的第一层

输入shape
形如(samples,sequence_length)的2D张量

输出shape
形如(samples, sequence_length, output_dim)的3D张量

参数
* ==input_dim==:大或等于0的整数,字典长度,即输入数据最大下标+1

  • ==output_dim==:大于0的整数,代表全连接嵌入的维度

  • embeddings_initializer: 嵌入矩阵的初始化方法,为预定义初始化方法名的字符串,或用于初始化权重的初始化器。参考initializers

  • embeddings_regularizer: 嵌入矩阵的正则项,为Regularizer对象

  • embeddings_constraint: 嵌入矩阵的约束项,为Constraints对象

  • mask_zero:布尔值,确定是否将输入中的‘0’看作是应该被忽略的‘填充’(padding)值,该参数在使用递归层处理变长输入时有用。设置为True的话,模型中后续的层必须都支持masking,否则会抛出异常。如果该值为True,则下标0在字典中不可用,input_dim应设置为|vocabulary| + 1。

  • ==input_length==:当输入序列的长度固定时,该值为其长度。如果要在该层后接Flatten层,然后接Dense层,则必须指定该参数,否则Dense层的输出维度无法自动推断。

model = Sequential()
model.add(Embedding(1000, 64, input_length=10))
# the model will take as input an integer matrix of size (batch, input_length).
# the largest integer (i.e. word index) in the input should be no larger than 999 (vocabulary size).
# now model.output_shape == (None, 10, 64), where None is the batch dimension.

input_array = np.random.randint(1000, size=(32, 10))

model.compile('rmsprop', 'mse')
output_array = model.predict(input_array)
assert output_array.shape == (32, 10, 64)

输入shape
形如(samples,sequence_length)的2D张量
输出shape
形如(samples, sequence_length, output_dim)的3D张量

上面的例子讲解:sequence_length == input_length = 10
Embedding()的参数说的是1000–输入的最多是999,此时输入的维度是32,输出是64,所以最后输出的维度就是(32,10,64), 输出有个节点,输入有10的节点

循环层

keras.layers.recurrent.Recurrent(return_sequences=False, go_backwards=False, stateful=False, unroll=False, implementation=0)

循环层的子类–LSTM,GRU,SimpleRNN

输入shape
形如(samples,timesteps,input_dim)的3D张量
输出shape
如果return_sequences=True:返回形如(samples,timesteps,output_dim)的3D张量
否则,返回形如(samples,output_dim)的2D张量

# as the first layer in a Sequential model
model = Sequential()
model.add(LSTM(32, input_shape=(10, 64)))
# now model.output_shape == (None, 32)
# note: `None` is the batch dimension.

# the following is identical:
model = Sequential()
model.add(LSTM(32, input_dim=64, input_length=10))

# for subsequent layers, no need to specify the input size:
         model.add(LSTM(16))

# to stack recurrent layers, you must use return_sequences=True
# on any recurrent layer that feeds into another recurrent layer.
# note that you only need to specify the input size on the first layer.
model = Sequential()
model.add(LSTM(64, input_dim=64, input_length=10, return_sequences=True))
model.add(LSTM(32, return_sequences=True))
model.add(LSTM(10))

LSTM层

keras.layers.recurrent.LSTM(units, activation='tanh', recurrent_activation='hard_sigmoid', use_bias=True, kernel_initializer='glorot_uniform', recurrent_initializer='orthogonal', bias_initializer='zeros', unit_forget_bias=True, kernel_regularizer=None, recurrent_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, recurrent_constraint=None, bias_constraint=None, dropout=0.0, recurrent_dropout=0.0)

参数

  • units:输出维度

  • activation:激活函数,为预定义的激活函数名(参考激活函数

  • recurrent_activation: 为循环步施加的激活函数(参考激活函数

  • use_bias: 布尔值,是否使用偏置项

  • kernel_initializer:权值初始化方法,为预定义初始化方法名的字符串,或用于初始化权重的初始化器。参考initializers

  • recurrent_initializer:循环核的初始化方法,为预定义初始化方法名的字符串,或用于初始化权重的初始化器。参考initializers

  • bias_initializer:权值初始化方法,为预定义初始化方法名的字符串,或用于初始化权重的初始化器。参考initializers

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

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

  • recurrent_regularizer:施加在循环核上的正则项,为Regularizer对象

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

  • kernel_constraints:施加在权重上的约束项,为Constraints对象

  • recurrent_constraints:施加在循环核上的约束项,为Constraints对象

  • bias_constraints:施加在偏置上的约束项,为Constraints对象

  • dropout:0~1之间的浮点数,控制输入线性变换的神经元断开比例

  • recurrent_dropout:0~1之间的浮点数,控制循环状态的线性变换的神经元断开比例

  • input_dim:输入维度,当使用该层为模型首层时,应指定该值(或等价的指定input_shape)

  • input_length:当输入序列的长度固定时,该参数为输入序列的长度。当需要在该层后连接Flatten层,然后又要连接Dense层时,需要指定该参数,否则全连接的输出无法计算出来。注意,如果循环层不是网络的第一层,你需要在网络的第一层中指定序列的长度(通过input_shape指定)。


文本预处理

Tokenizer(分词器)

keras.preprocessing.text.Tokenizer(num_words=None,
                                   filters='!"#$%&()*+,-./:;<=>?@[\]^_`{|}~\t\n',
                                   lower=True,
                                   split=" ",
                                   char_level=False)
##参数同句子分割的参数
## num_words:None或整数,处理的最大单词数量。若被设置为整数,则分词器将被限制为待处理数据集中最常见的num_words个单词。

类方法:

  • ==fit_on_texts==(texts)

    • texts:要用以训练的文本列表
  • ==texts_to_sequences==(texts)

    • texts:待转为序列的文本列表
    • 返回值:序列的列表,列表中每个序列对应于一段输入文本

属性:

  • ==word_index==: 字典,将单词(字符串)映射为它们的排名或者索引。仅在调用fit_on_texts之后设置。

句子分割text_to_word_sequence

keras.preprocessing.text.text_to_word_sequence(text,
                                               filters='!"#$%&()*+,-./:;<=>?@[\]^_`{|}~\t\n',
                                               lower=True,
                                               split=" ")
## 将一个句子拆分成单词构成的列表
##text:字符串,待处理的文本
##filter:需要过滤的字符的列表或链接形式的字符串
##lower:布尔值,是否将序列设为小写形式
## split:字符串,单词的分隔符,如空格
##返回值:字符串列表

函数式(Functional)模型

from keras.layers import Input, Dense
from keras.models import Model

# This returns a tensor
inputs = Input(shape=(784,))

# a layer instance is callable on a tensor, and returns a tensor
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)

# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(data, labels)  # starts training

猜你喜欢

转载自blog.csdn.net/qq_23069955/article/details/80606885