先看看做算法有哪些痛点(我们假设大部分算法的代码都是基于Python的):
MLSQL如何解决这些问题呢?
MLSQL提供了一套SQL的超集的DSL语法MLSQL,数据处理,模型训练,模型预测部署等都是以MLSQL语言交互,该语言简单易懂,无论算法,分析师,甚至运营都能看懂,极大的减少了团队的沟通成本,同时也使得更多的人可以做算法方面的工作。
所有较为复杂的数据预处理和算法都是模块化的,通过函数以及纯SQL来进行衔接。比如:
-- load data
load parquet.`${rawDataPath}` as orginal_text_corpus;
-- select only columns we care
select feature,label from orginal_text_corpus as orginal_text_corpus;
-- feature enginere moduel
train zhuml_orginal_text_corpus as TfIdfInPlace.`${tfidfFeaturePath}`
where inputCol="content"
and `dic.paths`="/data/dict_word.txt"
and stopWordPath="/data/stop_words"
and nGrams="2";
-- load data
load parquet.`${tfidfFeaturePath}/data` as tfidfdata;
-- algorithm module
train zhuml_corpus_featurize_training as PythonAlg.`${modelPath}`
where pythonScriptPath="${sklearnTrainPath}"
-- kafka params for log
and `kafkaParam.bootstrap.servers`="${kafkaDomain}"
and `kafkaParam.topic`="test"
and `kafkaParam.group_id`="g_test-2"
and `kafkaParam.userName`="pi-algo"
-- distribute data
and enableDataLocal="true"
and dataLocalFormat="json"
-- sklearn params
and `fitParam.0.moduleName`="sklearn.svm"
and `fitParam.0.className`="SVC"
and `fitParam.0.featureCol`="features"
and `fitParam.0.labelCol`="label"
and `fitParam.0.class_weight`="balanced"
and `fitParam.0.verbose`="true"
and `fitParam.1.moduleName`="sklearn.naive_bayes"
and `fitParam.1.className`="GaussianNB"
and `fitParam.1.featureCol`="features"
and `fitParam.1.labelCol`="label"
and `fitParam.1.class_weight`="balanced"
and `fitParam.1.labelSize`="2"
-- python env
and `systemParam.pythonPath`="python"
and `systemParam.pythonParam`="-u"
and `systemParam.pythonVer`="2.7";
这段小脚本脚本完成了数据加载,特征工程,最后的训练。所有以train开头的,都是模块,以select 开头的都是标准sql, 以load开头的则是各种数据源的加载。
在MLSQL中,任何一个模块都有两个产出:模型和函数。训练时该模块会产生一个对应的模型,预测时该模型会提供一个函数,从而实现
所有数据处理模块,算法模块,都有标准的暴露参数的方式,也就是前面例子类似下面的句子:
and `fitParam.0.labelCol`="label"
and `fitParam.0.class_weight`="balanced"
and `fitParam.0.verbose`="true"
比如该算法暴露了class_weight,labelCol,verbose等参数。所有人开发的算法模块和数据处理模块都可以很好的进行复用。
MLSQL是基于Spark改造而成,这就直接继承了Spark的多个优点:
同行启动一个local模式的MLSQL Server,然后注册我们训练的时候使用到的数据处理模块和算法模块,每个模块都会产生一个函数,接着就能通过http接口传递一个函数嵌套的方式完成一个pipeline的使用了。对于函数我们确保其响应速度,一般都是在毫秒级。 注册就是一个简单的register语句:
-- transform model into udf
register PythonAlg.`${modelPath}` as topic_spam_predict options
pythonScriptPath="${sklearnPredictPath}"
;
只要实现MLSQL的标准规范,你就能够轻而易举的将各种框架集成到MLSQL中。目前已经支持SKlearn,同时有Keras图片处理等相关例子。算法可以专注于算法模块的研发,研发可以专注于数据处理模块的开发,所有的人都可以通过MLSQL复用这些模块,完成算法业务的开发。