-
Notifications
You must be signed in to change notification settings - Fork 705
Initialize XGBoost codegen #765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Yancey0623
merged 7 commits into
sql-machine-learning:develop
from
Yancey0623:xgb_codegen
Sep 4, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e359497
initialize xgboost codegen
Yancey0623 50e7031
initialize xgboost codegen
Yancey0623 fe66c3d
Merge branch 'develop' of https://github.com/sql-machine-learning/sql…
Yancey0623 e83530b
init xgboost codegen
Yancey0623 545645e
fix typo
Yancey0623 0b1d9a3
remove unused code
Yancey0623 60ca030
remove xgb resolver
Yancey0623 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Copyright 2019 The SQLFlow Authors. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package sql | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "text/template" | ||
| ) | ||
|
|
||
| type xgbTrainConfig struct { | ||
| NumBoostRound int `json:"num_boost_round,omitempty"` | ||
| Maximize bool `json:"maximize,omitempty"` | ||
| } | ||
|
|
||
| type xgbFiller struct { | ||
| IsTrain bool | ||
| TrainingDatasetSQL string | ||
| ValidationDatasetSQL string | ||
| TrainCfg *xgbTrainConfig | ||
| Features []*featureMeta | ||
| Label *featureMeta | ||
| Save string | ||
| ParamsCfgJSON string | ||
| TrainCfgJSON string | ||
| *connectionConfig | ||
| } | ||
|
|
||
| func newXGBFiller(pr *extendedSelect, ds *trainAndValDataset, fts fieldTypes, db *DB) (*xgbFiller, error) { | ||
| var err error | ||
| training, validation := trainingAndValidationDataset(pr, ds) | ||
| r := &xgbFiller{ | ||
| IsTrain: pr.train, | ||
| TrainingDatasetSQL: training, | ||
| ValidationDatasetSQL: validation, | ||
| Save: pr.save, | ||
| } | ||
| // TODO(Yancey1989): fill the train_args and parameters by WITH statment | ||
| r.TrainCfgJSON = "" | ||
| r.ParamsCfgJSON = "" | ||
|
|
||
| if r.connectionConfig, err = newConnectionConfig(db); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| for _, columns := range pr.columns { | ||
| feaCols, colSpecs, err := resolveTrainColumns(&columns) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if len(colSpecs) != 0 { | ||
| return nil, fmt.Errorf("newXGBoostFiller doesn't support DENSE/SPARSE") | ||
| } | ||
| for _, col := range feaCols { | ||
| fm := &featureMeta{ | ||
| FeatureName: col.GetKey(), | ||
| Dtype: col.GetDtype(), | ||
| Delimiter: col.GetDelimiter(), | ||
| InputShape: col.GetInputShape(), | ||
| IsSparse: false, | ||
| } | ||
| r.Features = append(r.Features, fm) | ||
| } | ||
| } | ||
| r.Label = &featureMeta{ | ||
| FeatureName: pr.label, | ||
| Dtype: "int32", | ||
| Delimiter: ",", | ||
| InputShape: "[1]", | ||
| IsSparse: false, | ||
| } | ||
|
|
||
| return r, nil | ||
| } | ||
|
|
||
| func genXGBoost(w io.Writer, pr *extendedSelect, ds *trainAndValDataset, fts fieldTypes, db *DB) error { | ||
| r, e := newXGBFiller(pr, ds, fts, db) | ||
| if e != nil { | ||
| return e | ||
| } | ||
| if pr.train { | ||
| return xgbTrainTemplate.Execute(w, r) | ||
| } | ||
| return fmt.Errorf("xgboost prediction codegen has not been implemented") | ||
| } | ||
|
|
||
| var xgbTrainTemplate = template.Must(template.New("codegenXGBTrain").Parse(xgbTrainTemplateText)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Copyright 2019 The SQLFlow Authors. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package sql | ||
|
|
||
| const testXGBoostTrainSelectIris = ` | ||
| SELECT * | ||
| FROM iris.train | ||
| TRAIN xgb.multi.softprob | ||
| WITH | ||
| train.num_boost_round = 30 | ||
| COLUMN sepal_length, sepal_width, petal_length, petal_width | ||
| LABEL class | ||
| INTO sqlflow_models.my_xgboost_model; | ||
| ` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Copyright 2019 The SQLFlow Authors. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package sql | ||
|
|
||
| const xgbTrainTemplateText = ` | ||
| import xgboost as xgb | ||
| from sqlflow_submitter.db import connect, db_generator | ||
|
|
||
| driver="{{.Driver}}" | ||
|
|
||
| {{if ne .Database ""}} | ||
| database="{{.Database}}" | ||
| {{else}} | ||
| database="" | ||
| {{end}} | ||
|
|
||
| session_cfg = {} | ||
| {{ range $k, $v := .Session }} | ||
| session_cfg["{{$k}}"] = "{{$v}}" | ||
| {{end}} | ||
|
|
||
| {{if ne .TrainCfgJSON ""}} | ||
| train_args = {{.TrainCfgJSON}} | ||
| {{else}} | ||
| train_args = {} | ||
| {{end}} | ||
|
|
||
| {{if ne .ParamsCfgJSON ""}} | ||
| params = {{.ParamsCfgJSON}} | ||
| {{else}} | ||
| params = {} | ||
| {{end}} | ||
|
|
||
| feature_column_names = [{{range .Features}} | ||
| "{{.FeatureName}}", | ||
| {{end}}] | ||
|
|
||
| {{/* Convert go side featureSpec to python dict for input_fn */}} | ||
| feature_specs = dict() | ||
| {{ range $value := .Features }} | ||
| feature_specs["{{$value.FeatureName}}"] = { | ||
| "feature_name": "{{$value.FeatureName}}", | ||
| "dtype": "{{$value.Dtype}}", | ||
| "delimiter": "{{$value.Delimiter}}", | ||
| "shape": {{$value.InputShape}}, | ||
| "is_sparse": "{{$value.IsSparse}}" == "true" | ||
| } | ||
| {{end}} | ||
|
|
||
|
|
||
|
|
||
| conn = connect(driver, database, user="{{.User}}", password="{{.Password}}", host="{{.Host}}", port={{.Port}}, auth="{{.Auth}}") | ||
|
|
||
| def xgb_dataset(fn, dataset_sql): | ||
| gen = db_generator(driver, conn, session_cfg, dataset_sql, feature_column_names, "{{.Label.FeatureName}}", feature_specs) | ||
| with open(fn, 'w') as f: | ||
| for item in gen(): | ||
| features, label = item | ||
| row_data = [str(label[0])] + ["%d:%f" % (i, v) for i, v in enumerate(features)] | ||
| f.write("\t".join(row_data) + "\n") | ||
| # TODO(yancey1989): genearte group and weight text file if necessary | ||
| return xgb.DMatrix(fn) | ||
|
|
||
| dtrain = xgb_dataset('train.txt', "{{.TrainingDatasetSQL}}") | ||
| dtest = xgb_dataset('test.txt', "{{.ValidationDatasetSQL}}") | ||
|
|
||
| #TODO(Yancey1989): specify the eval metrics by WITH statement in SQL | ||
| train_args["evals"] = [(dtest, "auc")] | ||
| bst = xgb.train(params, dtrain, **train_args) | ||
| bst.save_model("{{.Save}}") | ||
| ` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found our filler for tensorflow in
codegen.go,sqlflow/sql/codegen.go
Lines 67 to 69 in 9a0dc86
Featuresnamed toXandLabeltoY.I would suggest such consistent naming.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a better way is reuse the filler in
codegen, would optimize it in the next PR.