-
Notifications
You must be signed in to change notification settings - Fork 1
/
light_gbm.py
135 lines (104 loc) · 4.53 KB
/
light_gbm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
import numpy as np
# NLP imports
from nltk.corpus import stopwords
stopwords=stopwords.words('german')
# modeling imports
from sklearn.model_selection import GridSearchCV
import lightgbm as lgb
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from sklearn.preprocessing import MinMaxScaler
from utils import modeling as m
from utils import cleaning, transformers
import mlflow
from modeling.config import TRACKING_URI, EXPERIMENT_NAME#, TRACKING_URI_DEV
import logging
from time import time
# set logging
logger = logging.getLogger(__name__)
logging.basicConfig(format="%(asctime)s: %(message)s")
logging.getLogger("pyhive").setLevel(logging.CRITICAL) # avoid excessive logs
logger.setLevel(logging.INFO)
if __name__ == "__main__":
data = m.Posts()
#embedding_dict_glove = transformers.load_embedding_vectors(embedding_style='glove')
#embedding_dict_w2v = transformers.load_embedding_vectors(embedding_style='word2vec')
trans_os = {None: [None], 'translate':[0.8,0.9,1.0], 'oversample':[0.8,0.9,1.0]}
#vecs = {CountVectorizer(): 'count',
# TfidfVectorizer(): 'tfidf',
# transformers.MeanEmbeddingVectorizer(embedding_dict=embedding_dict_glove): 'glove',
# transformers.MeanEmbeddingVectorizer(embedding_dict=embedding_dict_w2v): 'word2vec',
# }
vecs = {CountVectorizer(): 'count',
TfidfVectorizer(): 'tfidf',
}
lem = cleaning.lem_germ
stem = cleaning.stem_germ
norm = cleaning.normalize
for vec, vec_name in vecs.items():
print(vec_name)
if vec_name in ['count', 'tfidf']:
pipeline = Pipeline([
("vectorizer", vec),
("clf", lgb.LGBMClassifier()),
])
param_grid = {
"vectorizer__ngram_range" : [(1,1), (1,2), (1,3)],
"vectorizer__stop_words" : [stopwords, None],
"vectorizer__min_df": np.linspace(0, 0.1, 3),
"vectorizer__max_df": np.linspace(0.9, 1.0, 3),
"vectorizer__preprocessor": [norm, stem, lem],
'clf__n_estimators': [100, 400, 700], # default 100
'clf__max_depth': [-1, 60, 120], # default -1
}
else:
pipeline = Pipeline([
("vectorizer", vec),
("clf", lgb.LGBMClassifier()),
])
param_grid = {
'clf__n_estimators': [100, 400, 700], # default 100
'clf__max_depth': [-1, 60, 120], # default -1
}
# For clear logging output use verbose=1
gs = GridSearchCV(pipeline, param_grid, scoring="f1", cv=5, verbose=1)
# MLFlow params have limited characters, therefore stopwords must not be given as list
grid_search_params = param_grid.copy()
grid_search_params["vectorizer__stop_words"] = ["NLTK-German", None]
if vec_name in ['count', 'tfidf']:
grid_search_params["vectorizer__preprocessor"] = ["norm", "lem", "stem"]
mlflow_params = {
"vectorizer": vec_name,
"normalization": "lower",
"model": "LightGBM",
"grid_search_params": str(grid_search_params)[:249],
}
mlflow_tags = {
"cycle2": True,
}
TARGET_LABELS = ['label_argumentsused', 'label_discriminating', 'label_inappropriate',
'label_offtopic', 'label_personalstories', 'label_possiblyfeedback',
'label_sentimentnegative', 'label_sentimentpositive',]
IS_DEVELOPMENT = False
mlflow_logger = m.MLFlowLogger(
uri=TRACKING_URI,
experiment=EXPERIMENT_NAME,
is_dev=IS_DEVELOPMENT,
params=mlflow_params,
tags=mlflow_tags
)
training = m.Modeling(data, gs, mlflow_logger)
for method, strat in trans_os.items():
for strategy in strat:
print(method, strategy)
for label in TARGET_LABELS:
logger.info(f"-"*20)
logger.info(f"Target: {label}")
data.set_label(label=label)
data.set_balance_method(balance_method=method, sampling_strategy=strategy)
training.train()
training.evaluate(["train", "val"])
#if True:
with mlflow.start_run() as run:
mlflow_logger.log()