add lookahead and radam, add use api

This commit is contained in:
myzrshjy 2019-11-12 16:53:22 +08:00
parent 4b95b2f77f
commit 90d204dba3
15 changed files with 273 additions and 54 deletions

View File

@ -110,6 +110,20 @@ step3: goto # Train&Usage(调用) and Predict&Usage(调用)
* transformer模型: [https://github.com/CyberZHG/keras-transformer](https://github.com/CyberZHG/keras-transformer)
* keras_albert_model: [https://github.com/TinkerMob/keras_albert_model](https://github.com/TinkerMob/keras_albert_model)
# 训练简单调用:
```python
from keras_textclassification import train
train(graph='TextCNN', # 必填, 算法名, 可选"ALBERT","BERT","XLNET","FASTTEXT","TEXTCNN","CHARCNN",
# "TEXTRNN","RCNN","DCNN","DPCNN","VDCNN","CRNN","DEEPMOJI",
# "SELFATTENTION", "HAN","CAPSULE","TRANSFORMER"
label=17, # 必填, 类别数, 训练集和测试集合必须一样
path_train_data=None, # 必填, 训练数据文件, csv格式, 必须含'label,ques'头文件, 详见keras_textclassification/data
path_dev_data=None, # 必填, 测试数据文件, csv格式, 必须含'label,ques'头文件, 详见keras_textclassification/data
rate=1, # 可填, 训练数据选取比例
hyper_parameters=None) # 可填, json格式, 超参数, 默认embedding为'char','random'
```
# Train&Usage(调用)
```python

View File

@ -7,6 +7,8 @@
from keras_textclassification.data_preprocess.generator_preprocess import PreprocessGenerator
from keras_textclassification.data_preprocess.text_preprocess import save_json
from keras_textclassification.keras_layers.keras_lookahead import Lookahead
from keras_textclassification.keras_layers.keras_radam import RAdam
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras.optimizers import Adam
from keras import backend as K
@ -48,6 +50,7 @@ class graph:
self.path_hyper_parameters = hyper_parameters_model.get('path_hyper_parameters', "path_hyper_parameters") # 超参数保存地址
self.path_fineture = hyper_parameters_model.get('path_fineture', "path_fineture") # embedding层保存地址, 例如静态词向量、动态词向量、微调bert层等
self.patience = hyper_parameters_model.get('patience', 3) # 早停, 2-3就可以了
self.optimizer_name = hyper_parameters_model.get('optimizer_name', 'RAdam,Lookahead') # 早停, 2-3就可以了
if self.gpu_memory_fraction:
# keras, tensorflow控制GPU使用率等
import tensorflow as tf
@ -103,9 +106,20 @@ class graph:
构建优化器损失函数和评价函数
:return:
"""
self.model.compile(optimizer=Adam(lr=self.lr, beta_1=0.9, beta_2=0.999, decay=0.0),
loss=self.loss,
metrics=[self.metrics])
if self.optimizer_name.upper() == "ADAM":
self.model.compile(optimizer=Adam(lr=self.lr, beta_1=0.9, beta_2=0.999, decay=0.0),
loss=self.loss,
metrics=[self.metrics]) # Any optimize
elif self.optimizer_name.upper() == "RADAM":
self.model.compile(optimizer=RAdam(lr=self.lr, beta_1=0.9, beta_2=0.999, decay=0.0),
loss=self.loss,
metrics=[self.metrics]) # Any optimize
else:
self.model.compile(optimizer=RAdam(lr=self.lr, beta_1=0.9, beta_2=0.999, decay=0.0),
loss=self.loss,
metrics=[self.metrics]) # Any optimize
lookahead = Lookahead(k=5, alpha=0.5) # Initialize Lookahead
lookahead.inject(self.model) # add into model
def fit(self, x_train, y_train, x_dev, y_dev):
"""

View File

@ -0,0 +1,77 @@
# !/usr/bin/python
# -*- coding: utf-8 -*-
# @time : 2019/11/12 16:14
# @author : Mo
# @function: lookahead of keras
# @codefrom: https://github.com/bojone/keras_lookahead
from keras import backend as K
class Lookahead(object):
"""Add the [Lookahead Optimizer](https://arxiv.org/abs/1907.08610) functionality for [keras](https://keras.io/).
"""
def __init__(self, k=5, alpha=0.5):
self.k = k
self.alpha = alpha
self.count = 0
def inject(self, model):
"""Inject the Lookahead algorithm for the given model.
The following code is modified from keras's _make_train_function method.
See: https://github.com/keras-team/keras/blob/master/keras/engine/training.py#L497
"""
if not hasattr(model, 'train_function'):
raise RuntimeError('You must compile your model before using it.')
model._check_trainable_weights_consistency()
if model.train_function is None:
inputs = (model._feed_inputs +
model._feed_targets +
model._feed_sample_weights)
if model._uses_dynamic_learning_phase():
inputs += [K.learning_phase()]
fast_params = model._collected_trainable_weights
with K.name_scope('training'):
with K.name_scope(model.optimizer.__class__.__name__):
training_updates = model.optimizer.get_updates(
params=fast_params,
loss=model.total_loss)
slow_params = [K.variable(p) for p in fast_params]
fast_updates = (model.updates +
training_updates +
model.metrics_updates)
slow_updates, copy_updates = [], []
for p, q in zip(fast_params, slow_params):
slow_updates.append(K.update(q, q + self.alpha * (p - q)))
copy_updates.append(K.update(p, q))
# Gets loss and metrics. Updates weights at each call.
fast_train_function = K.function(
inputs,
[model.total_loss] + model.metrics_tensors,
updates=fast_updates,
name='fast_train_function',
**model._function_kwargs)
def F(inputs):
self.count += 1
R = fast_train_function(inputs)
if self.count % self.k == 0:
K.batch_get_value(slow_updates)
K.batch_get_value(copy_updates)
return R
model.train_function = F
if __name__ == '__main__':
gg = 0
# useage
# model.compile(optimizer=Adam(1e-3), loss='mse') # Any optimizer
# lookahead = Lookahead(k=5, alpha=0.5) # Initialize Lookahead
# lookahead.inject(model) # add into model

View File

@ -0,0 +1,96 @@
# !/usr/bin/python
# -*- coding: utf-8 -*-
# @time : 2019/11/12 16:12
# @author : Mo
# @function: radam of keras
# @codefrom: https://github.com/bojone/keras_radam
from keras.legacy import interfaces
from keras.optimizers import Optimizer
import keras.backend as K
class RAdam(Optimizer):
"""RAdam optimizer.
Default parameters follow those provided in the original Adam paper.
# Arguments
lr: float >= 0. Learning rate.
beta_1: float, 0 < beta < 1. Generally close to 1.
beta_2: float, 0 < beta < 1. Generally close to 1.
epsilon: float >= 0. Fuzz factor. If `None`, defaults to `K.epsilon()`.
decay: float >= 0. Learning rate decay over each update.
amsgrad: boolean. Whether to apply the AMSGrad variant of this
algorithm from the paper "On the Convergence of Adam and
Beyond".
# References
- [RAdam - A Method for Stochastic Optimization]
(https://arxiv.org/abs/1908.03265)
- [On The Variance Of The Adaptive Learning Rate And Beyond]
(https://arxiv.org/abs/1908.03265)
"""
def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999,
epsilon=None, decay=0., **kwargs):
super(RAdam, self).__init__(**kwargs)
with K.name_scope(self.__class__.__name__):
self.iterations = K.variable(0, dtype='int64', name='iterations')
self.lr = K.variable(lr, name='lr')
self.beta_1 = K.variable(beta_1, name='beta_1')
self.beta_2 = K.variable(beta_2, name='beta_2')
self.decay = K.variable(decay, name='decay')
if epsilon is None:
epsilon = K.epsilon()
self.epsilon = epsilon
self.initial_decay = decay
@interfaces.legacy_get_updates_support
def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
self.updates = [K.update_add(self.iterations, 1)]
lr = self.lr
if self.initial_decay > 0:
lr = lr * (1. / (1. + self.decay * K.cast(self.iterations,
K.dtype(self.decay))))
t = K.cast(self.iterations, K.floatx()) + 1
beta_1_t = K.pow(self.beta_1, t)
beta_2_t = K.pow(self.beta_2, t)
rho = 2 / (1 - self.beta_2) - 1
rho_t = rho - 2 * t * beta_2_t / (1 - beta_2_t)
r_t = K.sqrt(
K.relu(rho_t - 4) * K.relu(rho_t - 2) * rho / ((rho - 4) * (rho - 2) * rho_t)
)
flag = K.cast(rho_t > 4, K.floatx())
ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
self.weights = [self.iterations] + ms + vs
for p, g, m, v in zip(params, grads, ms, vs):
m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g)
mhat_t = m_t / (1 - beta_1_t)
vhat_t = K.sqrt(v_t / (1 - beta_2_t))
p_t = p - lr * mhat_t * (flag * r_t / (vhat_t + self.epsilon) + (1 - flag))
self.updates.append(K.update(m, m_t))
self.updates.append(K.update(v, v_t))
new_p = p_t
# Apply constraints.
if getattr(p, 'constraint', None) is not None:
new_p = p.constraint(new_p)
self.updates.append(K.update(p, new_p))
return self.updates
def get_config(self):
config = {'lr': float(K.get_value(self.lr)),
'beta_1': float(K.get_value(self.beta_1)),
'beta_2': float(K.get_value(self.beta_2)),
'decay': float(K.get_value(self.decay)),
'epsilon': self.epsilon}
base_config = super(RAdam, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

View File

@ -26,12 +26,12 @@ import time
def train(hyper_parameters=None, rate=1.0):
if not hyper_parameters:
hyper_parameters = {
'len_max': 2, # 句子最大长度, 固定推荐20-50, bert越长会越慢, 占用空间也会变大, 小心OOM
'len_max': 32, # 句子最大长度, 固定推荐20-50, bert越长会越慢, 占用空间也会变大, 小心OOM
'embed_size': 768, # 字/词向量维度, bert取768, word取300, char可以更小些
'vocab_size': 20000, # 这里随便填的,会根据代码里修改
'trainable': False, # embedding是静态的还是动态的, 即控制可不可以微调
'level_type': 'char', # 级别, 最小单元, 字/词, 填 'char' or 'word', 注意:word2vec模式下训练语料要首先切好
'embedding_type': 'bert', # 级别, 嵌入类型, 还可以填'xlnet'、'random'、 'bert'、 'albert' or 'word2vec"
'embedding_type': 'random', # 级别, 嵌入类型, 还可以填'xlnet'、'random'、 'bert'、 'albert' or 'word2vec"
'gpu_memory_fraction': 0.76, #gpu使用率
'model': {'label': 17, # 类别数
'batch_size': 2, # 批处理尺寸, 感觉原则上越大越好,尤其是样本不均衡的时候, batch_size设置影响比较大
@ -45,6 +45,7 @@ def train(hyper_parameters=None, rate=1.0):
'activate_classify': 'softmax', # 最后一个layer, 即分类激活函数
'loss': 'categorical_crossentropy', # 损失函数
'metrics': 'accuracy', # 保存更好模型的评价标准
'optimizer_name': 'RAdam', # 优化器, 可选['Adam', 'Radam', 'RAdam,Lookahead']
'is_training': True, # 训练后者是测试模型
'model_path': path_model,
# 模型地址, loss降低则保存的依据, save_best_only=True, save_weights_only=True

View File

@ -51,10 +51,16 @@ def pred_tet(path_hyper_parameter=path_hyper_parameters, path_test=None, rate=1.
for x_one in x:
count += 1
ques_embed = ra_ed.sentence2idx(x_one)
if hyper_parameters['embedding_type'] == 'bert': # bert数据处理, token
if hyper_parameters['embedding_type'] in ['bert', 'albert']:
x_val_1 = np.array([ques_embed[0]])
x_val_2 = np.array([ques_embed[1]])
x_val = [x_val_1, x_val_2]
elif hyper_parameters['embedding_type'] == 'xlnet':
x_val_1 = np.array([ques_embed[0]])
x_val_2 = np.array([ques_embed[1]])
x_val_3 = np.array([ques_embed[2]])
x_val = [x_val_1, x_val_2, x_val_3]
else:
x_val = ques_embed
# 预测

View File

@ -26,16 +26,16 @@ import time
def train(hyper_parameters=None, rate=1.0):
if not hyper_parameters:
hyper_parameters = {
'len_max': 50, # 句子最大长度, 固定推荐20-50, bert越长会越慢, 占用空间也会变大, 本地win10-4G设为20就好, 过大小心OOM
'embed_size': 768, # 字/词向量维度, bert取768, word取300, char可以更小些
'len_max': 55, # 句子最大长度, 固定推荐20-50, bert越长会越慢, 占用空间也会变大, 本地win10-4G设为20就好, 过大小心OOM
'embed_size': 100, # 字/词向量维度, bert取768, word取300, char可以更小些
'vocab_size': 20000, # 这里随便填的,会根据代码里修改
'trainable': True, # embedding是静态的还是动态的, 即控制可不可以微调
'level_type': 'char', # 级别, 最小单元, 字/词, 填 'char' or 'word', 注意:word2vec模式下训练语料要首先切好
'embedding_type': 'bert', # 级别, 嵌入类型, 还可以填'xlnet'、'random'、 'bert'、 'albert' or 'word2vec"
'trainable': False, # embedding是静态的还是动态的, 即控制可不可以微调
'level_type': 'word', # 级别, 最小单元, 字/词, 填 'char' or 'word', 注意:word2vec模式下训练语料要首先切好
'embedding_type': 'word2vec', # 级别, 嵌入类型, 还可以填'xlnet'、'random'、 'bert'、 'albert' or 'word2vec"
'gpu_memory_fraction': 0.66, #gpu使用率
'model': {'label': 17, # 类别数
'batch_size': 2, # 批处理尺寸, 感觉原则上越大越好,尤其是样本不均衡的时候, batch_size设置影响比较大
'dropout': 0.32, # 随机失活, 概率
'dropout': 0.5, # 随机失活, 概率
'decay_step': 100, # 学习率衰减step, 每N个step衰减一次
'decay_rate': 0.9, # 学习率衰减系数, 乘法
'epochs': 20, # 训练最大轮次

View File

@ -17,7 +17,7 @@ from keras import regularizers
from keras_textclassification.base.graph import graph
class DeepMoji(graph):
class DeepMojiGraph(graph):
def __init__(self, hyper_parameters):
"""
初始化

View File

@ -20,7 +20,7 @@ from keras_textclassification.conf.path_config import path_baidu_qa_2019_train,
# 数据预处理, 删除文件目录下文件
from keras_textclassification.data_preprocess.text_preprocess import PreprocessText, delete_file
# 模型图
from keras_textclassification.m10_DeepMoji.graph import DeepMoji as Graph
from keras_textclassification.m10_DeepMoji.graph import DeepMojiGraph as Graph
# 计算时间
import time

View File

@ -73,7 +73,14 @@ def pred_tet(path_hyper_parameter=path_hyper_parameters, path_test=None, rate=1.
# 评估
report_predict = classification_report(index_y, index_pred,
target_names=target_names, digits=9)
count = 0
for i in range(len(index_pred)):
if index_y[i]==index_pred[i]:
count += 1
print(report_predict)
print('accuracy:{}'.format(count/len(index_y)))
print("耗时:" + str(time.time() - time_start))
@ -118,29 +125,15 @@ def pred_input(path_hyper_parameter=path_hyper_parameters):
if __name__=="__main__":
# 测试集预测
pred_tet(path_test=path_baidu_qa_2019_valid, rate=1) # sample条件下设为1,否则训练语料可能会很少
pred_tet(rate=1) # sample条件下设为1,否则训练语料可能会很少
# 可输入 input 预测
pred_input()
# 92218/92218 [==============================] - 138s 2ms/step - loss: 1.0619 - acc: 0.7534 - val_loss: 3.3621 - val_acc: 0.3412
# Epoch 00020: val_loss improved from 3.36474 to 3.36213, saving model to D:/workspace/pythonMyCode/django_project/Keras-TextClassification/keras_textclassification/data/model/fast_text/model_fast_text.h5
# 耗时:2830.3001523017883
# precision recall f1-score support
#
# 电子 0.428571429 0.375000000 0.400000000 8
# 社会 0.250000000 0.166666667 0.200000000 12
# 烦恼 0.500000000 0.800000000 0.615384615 20
# 电脑 0.700000000 0.549019608 0.615384615 51
# 汽车 0.333333333 0.600000000 0.428571429 5
# 商业 0.781250000 0.714285714 0.746268657 35
# 文化 0.000000000 0.000000000 0.000000000 7
# 健康 0.727272727 0.655737705 0.689655172 61
# 育儿 0.142857143 0.200000000 0.166666667 5
# 教育 0.638297872 0.517241379 0.571428571 58
# 娱乐 0.390243902 0.400000000 0.395061728 40
# 生活 0.487179487 0.387755102 0.431818182 49
# 体育 0.666666667 0.400000000 0.500000000 5
# 游戏 0.658333333 0.858695652 0.745283019 92
#
# accuracy 0.589285714 448
# macro avg 0.478857564 0.473171559 0.464680190 448
# weighted avg 0.595133343 0.589285714 0.583989557 448
# 92218/92218 [==============================] - 161s 2ms/step - loss: 0.0449 - acc: 0.9887 - val_loss: 0.7595 - val_acc: 0.8114
# Epoch 00018: val_loss improved from 0.76525 to 0.75950, saving model to D:/workspace/pythonMyCode/django_project/Keras-TextClassification/keras_textclassification/data/model/fast_text/model_fast_text.h5
# Epoch 19/20

View File

@ -31,19 +31,19 @@ def train(hyper_parameters=None, rate=1.0):
"""
if not hyper_parameters:
hyper_parameters = {
'len_max': 50, # 句子最大长度, 固定 推荐20-50
'len_max': 55, # 句子最大长度, 固定 推荐20-50
'embed_size': 300, # 字/词向量维度
'vocab_size': 20000, # 这里随便填的,会根据代码里修改
'trainable': True, # embedding是静态的还是动态的
'level_type': 'char', # 级别, 最小单元, 字/词, 填 'char' or 'word'
'embedding_type': 'random', # 级别, 嵌入类型, 还可以填'xlnet'、'random'、 'bert'、 'albert' or 'word2vec"
'gpu_memory_fraction': 0.66, # gpu使用率
'model': {'label': 17, # 类别数
'batch_size': 64, # 批处理尺寸, 感觉原则上越大越好,尤其是样本不均衡的时候, batch_size设置影响比较大
'trainable': False, # embedding是静态的还是动态的
'level_type': 'word', # 级别, 最小单元, 字/词, 填 'char' or 'word'
'embedding_type': 'word2vec', # 级别, 嵌入类型, 还可以填'xlnet'、'random'、 'bert'、 'albert' or 'word2vec"
'gpu_memory_fraction': 0.76, # gpu使用率
'model': {'label': 90, # 类别数
'batch_size': 256, # 批处理尺寸, 感觉原则上越大越好,尤其是样本不均衡的时候, batch_size设置影响比较大
'dropout': 0.5, # 随机失活, 概率
'decay_step': 100, # 学习率衰减step, 每N个step衰减一次
'decay_rate': 0.9, # 学习率衰减系数, 乘法
'epochs': 50, # 训练最大轮次
'epochs': 20, # 训练最大轮次
'patience': 5, # 早停,2-3就好
'lr': 5e-4, # 学习率, 对训练会有比较大的影响, 如果准确率一直上不去,可以考虑调这个参数
'l2': 1e-9, # l2正则化
@ -90,10 +90,4 @@ def train(hyper_parameters=None, rate=1.0):
if __name__ == "__main__":
train(rate=1) # sample条件下设为1,否则训练语料可能会很少
# 注意: 4G的1050Ti的GPU、win10下batch_size=32,len_max=20, gpu<=0.87, 应该就可以bert-fineture了。
# 全量数据训练一轮(batch_size=32),就能达到80%准确率(验证集), 效果还是不错的
# win10下出现过错误,gpu、len_max、batch_size配小一点就好:ailed to allocate 3.56G (3822520832 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
# 参数较多,不适合用bert,会比较慢和OOM
# 速度很慢呐,字/词维度(embed_size)一设大就OOM了不喜欢用
train(rate=1)

View File

@ -76,7 +76,7 @@ def train(graph='TextCNN', label=17, rate=1.0, hyper_parameters=None, path_train
hyper_parameters_real = {
'len_max': 50, # 句子最大长度, 固定 推荐20-50
'trainable': True, # embedding是静态的还是动态的
'embed_size': 768, # 字/词向量维度
'embed_size': 64, # 字/词向量维度
'vocab_size': 20000, # 这里随便填的,会根据代码里修改
'level_type': 'char', # 级别, 最小单元, 字/词, 填 'char' or 'word'
'embedding_type': 'random', # 级别, 嵌入类型, 还可以填'xlnet'、'random'、 'bert'、 'albert' or 'word2vec"
@ -160,4 +160,4 @@ def train(graph='TextCNN', label=17, rate=1.0, hyper_parameters=None, path_train
if __name__ == "__main__":
train(graph='TextCNN', label=17, rate=1, hyper_parameters=None, path_train_data=None, path_dev_data=None)
train(graph='TextCNN', label=17, rate=1, path_train_data=None, path_dev_data=None,hyper_parameters=None)

View File

@ -25,7 +25,7 @@ with codecs.open('requirements.txt', 'r', 'utf8') as reader:
install_requires = list(map(lambda x: x.strip(), reader.readlines()))
setup(name=NAME,
version='0.1.4',
version='0.1.5',
description=DESCRIPTION,
long_description=long_description,
long_description_content_type="text/markdown",

View File

@ -73,6 +73,11 @@ def pred_tet(path_hyper_parameter=path_hyper_parameters, path_test=None, rate=1.
# 评估
report_predict = classification_report(index_y, index_pred,
target_names=target_names, digits=9)
count = 0
for i in range(len(index_pred)):
if index_y[i] == index_pred[i]:
count += 1
print(count)
print(report_predict)
print("耗时:" + str(time.time() - time_start))
@ -123,4 +128,7 @@ if __name__=="__main__":
# 可输入 input 预测
pred_input()
# 180/180 [==============================] - 56s 313ms/step - loss: 0.0696 - acc: 0.9784 - val_loss: 0.8469 - val_acc: 0.8155
#
# Epoch 00016: val_loss improved from 0.85291 to 0.84695, saving model to D:/workspace/pythonMyCode/django_project/Keras-TextClassification/keras_textclassification/data/model/fast_text/model_fast_text.h5
# Epoch 17/20

16
test/tet_train.py Normal file
View File

@ -0,0 +1,16 @@
# !/usr/bin/python
# -*- coding: utf-8 -*-
# @time : 2019/11/12 16:45
# @author : Mo
# @function:
from keras_textclassification import train
train(graph='TextCNN', # 必填, 算法名, 可选"ALBERT","BERT","XLNET","FASTTEXT","TEXTCNN","CHARCNN",
# "TEXTRNN","RCNN","DCNN","DPCNN","VDCNN","CRNN","DEEPMOJI",
# "SELFATTENTION", "HAN","CAPSULE","TRANSFORMER"
label=17, # 必填, 类别数, 训练集和测试集合必须一样
path_train_data=None, # 必填, 训练数据文件, csv格式, 必须含'label,ques'头文件, 详见keras_textclassification/data
path_dev_data=None, # 必填, 测试数据文件, csv格式, 必须含'label,ques'头文件, 详见keras_textclassification/data
rate=1, # 可填, 训练数据选取比例
hyper_parameters=None) # 可填, json格式, 超参数, 默认embedding为'char','random'