| 182 | f.write(onx.SerializeToString()) |
| 183 | |
| 184 | def train_stacking_classifier(data): |
| 185 | model1 = MLPClassifier(hidden_layer_sizes=(100, 20),learning_rate='adaptive', max_iter=1000, |
| 186 | random_state=1, verbose=True, activation='logistic', solver='adam') |
| 187 | model2 = KNeighborsClassifier(n_neighbors=10, n_jobs=8) |
| 188 | model3 = RandomForestClassifier(class_weight='balanced', random_state=1, n_jobs=8, n_estimators=200) |
| 189 | meta_model = LogisticRegression() |
| 190 | sclf = StackingClassifier(estimators=[('MLPClassifier', model1), ('KNeighborsClassifier', model2), ('RandomForestClassifier', model3)], |
| 191 | final_estimator=meta_model, n_jobs=15, |
| 192 | passthrough=True) |
| 193 | logging.info('#### Stacking ####') |
| 194 | scores = cross_val_score(sclf, data[0], data[1], cv=5, scoring='f1_macro', n_jobs=15) |
| 195 | logging.info('f1 macro %s' % str(scores)) |
| 196 | sclf.fit(data[0], data[1]) |
| 197 | |
| 198 | initial_type = [('mindfulness_input', FloatTensorType([1, 5]))] |
| 199 | onx = convert_sklearn(sclf, initial_types=initial_type, target_opset=11, options={type(sclf): {'zipmap': False}}) |
| 200 | with open('stacking_mindfulness.onnx', 'wb') as f: |
| 201 | f.write(onx.SerializeToString()) |
| 202 | |
| 203 | def main(): |
| 204 | logging.basicConfig(level=logging.INFO) |