1、摘要
本文主要讲解:lgb训练模型后使用AutoML-NNI对注塑工艺参数调参优化 主要思路:
- 使用lgb训练注塑品的三维尺寸预测模型,保存模型
- 将训练特征作为需要调优的参数,使用保存的模型对这个参数进行预测
- 将预测的结果作为nni的最佳指标,当最佳指标为最优的时候,即为最优的工艺参数
2、数据介绍
a) 传感器高频数据:该数据来自于模温机及模具传感器采集的数据,文件夹内每一个模次对应一个csv文件,单个模次时长为40~43s,采样频率根据阶段有20Hz和50Hz两种,含有24个传感器采集的数据; b) 成型机状态数据(data_spc):该数据来自成型机机台,均为表征成型过程中的一些状态数据,每一行对应一个模次,数据维度为86维; c) 机台工艺设定参数(data_set):文件夹中含有注塑成型的81种工艺设定参数; d) 产品测量尺寸(size):文件夹内含有每个模次产品的3维尺寸; e) 现场巡检数据(初赛不提供此类数据)。各数据字段含义可在数据下载包中获得。 数据下载链接
3、相关技术
注塑工艺参数优化 工艺参数优化指的是对生产制造领域所需的工艺参数进行优化,在大部分制造业都存在工艺参数优化问题,例如注塑行业需要对保压压力、时间、温度等参数进行优化,以提高注塑品的质量,防止注塑品异常。
NNI (Neural Network Intelligence) 是一个轻量但强大的工具包,帮助用户自动的进行特征工程,神经网络架构搜索,超参调优以及模型压缩。
NNI 管理自动机器学习 (AutoML) 的 Experiment,调度运行由调优算法生成的 Trial 任务来找到最好的神经网络架构和/或超参。
4、完整代码和步骤
代码输出如下: 当最佳指标达到300时,说明已找到最佳的工艺生产参数
训练模型并保存
'''
This project is for automatically tuning parameters for GBDT.
Trial(尝试) 是将一组参数组合(例如,超参)在模型上独立的一次尝试。
定义 NNI 的 Trial,需要首先定义参数组(例如,搜索空间),并更新模型代码。
nnictl create --config config.yml -p 8888 --debug
'''
import logging
import lightgbm as lgb
import nni
import pandas as pd
from sklearn.metrics import mean_squared_error
import os
from sklearn.model_selection import train_test_split
from my_utils.data_utils import min_max_select
os.chdir(r'H:\工业大数据\工业大数据创新竞赛-注塑成型完整解决方案\data\Train')
LOG = logging.getLogger('auto-gbdt')
def get_default_parameters():
params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': {'l2', 'auc'},
'num_leaves': 28,
'learning_rate': 0.2,
'feature_fraction': 0.930,
'bagging_fraction': 0.7656,
'bagging_freq': 4,
'verbose': 0
}
return params
def load_data():
df_TRAIN = pd.read_csv('df_TRAIN.csv')
size_train = pd.read_csv('Size.csv')
X_col = [i for i in df_TRAIN.columns if not i in ['Id', 'spcTime']]
i = 'size3'
X_train = df_TRAIN[X_col]
y_train = size_train[i]
X_train,X_eval,y_train ,y_eval = train_test_split(X_train,y_train, test_size=0.3, random_state=0)
X_eval,X_test,y_eval ,y_test = train_test_split(X_eval,y_eval, test_size=0.6, random_state=0)
lgb_train = lgb.Dataset(X_train, y_train)
lgb_eval = lgb.Dataset(X_eval, y_eval, reference=lgb_train)
return lgb_train, lgb_eval, X_test, y_test
def run(lgb_train, lgb_eval, params, X_test, y_test):
print('Start training...')
params['num_leaves'] = int(params['num_leaves'])
gbm = lgb.train(params,
lgb_train,
num_boost_round=20,
valid_sets=lgb_eval,
early_stopping_rounds=5)
print('Start predicting...')
y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration)
gbm.save_model('gbm_model_size3.txt')
rmse = mean_squared_error(y_test, y_pred) ** 0.5
print('The rmse of prediction is:', rmse)
nni.report_final_result(rmse)
if __name__ == '__main__':
lgb_train, lgb_eval, X_test, y_test = load_data()
try:
RECEIVED_PARAMS = nni.get_next_parameter()
LOG.debug(RECEIVED_PARAMS)
PARAMS = get_default_parameters()
PARAMS.update(RECEIVED_PARAMS)
LOG.debug(PARAMS)
run(lgb_train, lgb_eval, PARAMS, X_test, y_test)
except Exception as exception:
LOG.exception(exception)
raise
主运行程序入口 命令 nnictl create --config config.yml -p 8882 --debug
import logging
import lightgbm as lgb
import nni
import numpy as np
import os
os.chdir(r'H:\工业大数据\工业大数据创新竞赛-注塑成型完整解决方案\data\Train')
LOG = logging.getLogger('auto-gbdt')
'''
目前用机器学习模型拟合工艺过程,并用NNI求优,但一方面调优算法具有随机性,
每次寻优结果都不同,另一方面无法解决通过时序性的产品尺寸(即输出)来优化工艺参数(即输入)。
根据y(即输出)来寻找x(即输入),解一般都是很多组,很难找到最优解,加额外的约束条件。
'''
def get_default_parameters():
params = {"Sensor1_mean": 26312.942, "Sensor2_mean": 8837.184, "Sensor3_mean": 31569.637, "IJ_mean": 40236.952, "Sensor5_mean": 189215.722, "Sensor6_mean": 154451.291, "MouldTemp1_mean": 23400.0, "MouldTemp2_mean": 23400.0, "MouldTemp3_mean": 23400.0, "MouldTemp4_mean": 23400.0, "MouldTemp5_mean": 23400.0, "MouldTemp9_mean": 121115.999, "MouldTemp10_mean": 126094.039, "MouldTemp11_mean": 123119.346, "MouldTemp12_mean": 24315.0, "MouldTemp13_mean": 122862.835, "MouldTemp14_mean": 124401.039, "Sensor8_mean": -57212.977, "MouldFlow1_mean": 7550.0, "MouldFlow2_mean": 7550.0, "MouldFlow3_mean": 7550.0, "SP_mean": 81659.177, "EL_CYC_TIME": 9.238, "EL_IV_END_TIME": 0.0, "EL_PLST_TIME": 0.0, "EL_LAST_COOL_TIME": 0.0, "EL_MC_TIME": 0.406, "EL_MO_TIME": 0.0, "EL_EF_TIME": 0.0, "EL_EB_TIME": 0.0, "EL_IDLE_TIME": 0.0, "EL_CORE_IN_TIME_0": 0.0, "EL_CORE_IN_TIME_1": 0.0, "EL_CORE_IN_TIME_2": 0.0, "EL_CORE_IN_TIME_3": 0.0, "EL_CORE_OUT_TIME_0": 0.0, "EL_CORE_OUT_TIME_1": 0.0, "EL_CORE_OUT_TIME_2": 0.0, "EL_CORE_OUT_TIME_3": 0.0, "EL_IV_TIME": 1.522, "EL_PP_TIME": 2.927, "EL_SB1_TIME": 0.0, "EL_SB2_TIME": 0.0, "EL_CF_TIME": 0.0, "EL_CB_TIME": 0.0, "EL_IV_STG_TIME_0": 0.311, "EL_IV_STG_TIME_1": 0.346, "EL_IV_STG_TIME_2": 0.527, "EL_IV_STG_TIME_3": 0.106, "EL_IV_STG_TIME_4": 0.048, "op_mode": 2.0, "ext_mold_time_flt_0": 0.0, "EL_VPS_TIME": 0.0, "ext_mold_time_flt_1": 0.0, "ext_mold_time_flt_2": 0.0, "ext_mold_time_flt_3": 0.0, "ext_mold_time_flt_4": 0.0, "EL_GET_PART_TIME": 0.0, "EL_NZL_MEAN": 248.00876, "EL_BARREL_MEAN_1": 258.09712, "EL_BARREL_MEAN_2": 248.4281, "EL_BARREL_MEAN_3": 238.21912, "EL_BARREL_MEAN_4": 211.84534, "EL_BARREL_MEAN_5": 15.0, "EL_BARREL_MEAN_6": 15.0, "EL_BARREL_MEAN_7": 15.0, "EL_HTRN_TI_0": 15.0, "EL_HTRN_TI_1": 15.0, "EL_HTRN_TI_2": 15.0, "EL_HTRN_TI_3": 15.0, "EL_HTRN_TI_4": 15.0, "EL_HTRN_TI_5": 15.0, "EL_HTRN_TI_6": 15.0, "EL_HTRN_TI_7": 15.0, "ext_mold_temp_flt_0": 15.0, "ext_mold_temp_flt_1": 15.0, "ext_mold_temp_flt_2": 15.0, "ext_mold_temp_flt_3": 15.0, "ext_mold_temp_flt_4": 0.0, "ext_mold_temp_flt_5": 0.0, "ext_mold_temp_flt_6": 0.0, "ext_mold_temp_flt_7": 0.0, "rob_enbl": 1.0, "ext_mold_flow_flt_0": 5.0, "EL_MOP": 10.0, "EL_PLST_END_ERR": 10.0, "EL_INJ_START_STR": 104.02236, "EL_IV_END_STR": 39.980728, "EL_VPS_END_STR": 16.880154, "EL_MIN_INJ_STR": 16.598343, "EL_EF_END_POS": 48.023884, "EL_PP_STG_POS_0": 21.992069, "EL_PP_STG_POS_1": 21.335789, "EL_PP_STG_POS_2": 16.876579, "EL_PP_STG_POS_3": 10.0, "EL_PP_STG_POS_4": 10.0, "EL_CLAMP_PRESS": 53.827827, "EL_MAX_INJ_PRESS": 62.843433, "EL_IV_END_PRESS": 41.594925, "EL_CF_END_PRESS": 5.0, "EL_PLST_BP_0": 5.0, "EL_PLST_BP_1": 5.0, "EL_PLST_BP_2": 5.0, "EL_PLST_BP_3": 5.0, "EL_PLST_BP_4": 5.0}
return params
def run(params):
print('Start training...')
X_test = np.array(list(params.values())).reshape(1, 105)
print('Start predicting...')
gbm = lgb.Booster(model_file='gbm_model_size3.txt')
y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration)
nni.report_final_result(round(y_pred[0], 3))
if __name__ == '__main__':
try:
RECEIVED_PARAMS = nni.get_next_parameter()
LOG.debug(RECEIVED_PARAMS)
PARAMS = get_default_parameters()
PARAMS.update(RECEIVED_PARAMS)
LOG.debug(PARAMS)
run(PARAMS)
except Exception as exception:
LOG.exception(exception)
raise
config.yml
authorName: default
experimentName: zhusu-gbdt
trialConcurrency: 1
maxExecDuration: 10h
maxTrialNum: 100
trainingServicePlatform: local
searchSpacePath: search_space1.json
useAnnotation: false
tuner:
builtinTunerName: Anneal
classArgs:
optimize_mode: minimize
trial:
command: python main1.py
codeDir: .
gpuNum: 0
search_space1.json
{"Sensor1_mean": {"_type": "uniform", "_value": [26312.942000000003, 75080.232]}, "Sensor2_mean": {"_type": "uniform", "_value": [8837.184000000001, 199116.303]}, "Sensor3_mean": {"_type": "uniform", "_value": [31569.637, 80692.242]}, "IJ_mean": {"_type": "uniform", "_value": [40236.95200000001, 89024.65400000002]}, "Sensor5_mean": {"_type": "uniform", "_value": [189215.722, 212030.38]}, "Sensor6_mean": {"_type": "uniform", "_value": [154451.29100000006, 219932.88300000003]}, "MouldTemp1_mean": {"_type": "uniform", "_value": [23400.0, 145170.95300000007]}, "MouldTemp2_mean": {"_type": "uniform", "_value": [23400.0, 95793.20000000004]}, "MouldTemp3_mean": {"_type": "uniform", "_value": [23400.0, 112825.865]}, "MouldTemp4_mean": {"_type": "uniform", "_value": [23400.0, 164148.8]}, "MouldTemp5_mean": {"_type": "uniform", "_value": [23400.0, 144523.1]}, "MouldTemp9_mean": {"_type": "uniform", "_value": [121115.99900000001, 159108.548]}, "MouldTemp10_mean": {"_type": "uniform", "_value": [126094.039, 167141.36099999998]}, "MouldTemp11_mean": {"_type": "uniform", "_value": [123119.346, 1381420.0000000005]}, "MouldTemp12_mean": {"_type": "uniform", "_value": [24315.0, 1315599.4000000004]}, "MouldTemp13_mean": {"_type": "uniform", "_value": [122862.835, 1315599.4000000004]}, "MouldTemp14_mean": {"_type": "uniform", "_value": [124401.03900000002, 1315599.4000000004]}, "Sensor8_mean": {"_type": "uniform", "_value": [-57212.977000000006, 31930.982000000004]}, "MouldFlow1_mean": {"_type": "uniform", "_value": [7550.0, 36424.32]}, "MouldFlow2_mean": {"_type": "uniform", "_value": [7550.0, 8500.0]}, "MouldFlow3_mean": {"_type": "uniform", "_value": [7550.0, 8500.0]}, "SP_mean": {"_type": "uniform", "_value": [81659.177, 125933.012]}, "EL_CYC_TIME": {"_type": "uniform", "_value": [9.238, 42.073]}, "EL_IV_END_TIME": {"_type": "uniform", "_value": [0.0, 0.0]}, "EL_PLST_TIME": {"_type": "uniform", "_value": [0.0, 9.933]}, "EL_LAST_COOL_TIME": {"_type": "uniform", "_value": [0.0, 9.21]}, "EL_MC_TIME": {"_type": "uniform", "_value": [0.406, 3.276]}, "EL_MO_TIME": {"_type": "uniform", "_value": [0.0, 2.141]}, "EL_EF_TIME": {"_type": "uniform", "_value": [0.0, 2.092]}, "EL_EB_TIME": {"_type": "uniform", "_value": [0.0, 3.127]}, "EL_IDLE_TIME": {"_type": "uniform", "_value": [0.0, 4.412]}, "EL_CORE_IN_TIME_0": {"_type": "uniform", "_value": [0.0, 1.1]}, "EL_CORE_IN_TIME_1": {"_type": "uniform", "_value": [0.0, 0.0]}, "EL_CORE_IN_TIME_2": {"_type": "uniform", "_value": [0.0, 0.0]}, "EL_CORE_IN_TIME_3": {"_type": "uniform", "_value": [0.0, 0.0]}, "EL_CORE_OUT_TIME_0": {"_type": "uniform", "_value": [0.0, 1.1]}, "EL_CORE_OUT_TIME_1": {"_type": "uniform", "_value": [0.0, 0.0]}, "EL_CORE_OUT_TIME_2": {"_type": "uniform", "_value": [0.0, 0.0]}, "EL_CORE_OUT_TIME_3": {"_type": "uniform", "_value": [0.0, 0.0]}, "EL_IV_TIME": {"_type": "uniform", "_value": [1.5219999999999998, 2.128]}, "EL_PP_TIME": {"_type": "uniform", "_value": [2.927, 13.502]}, "EL_SB1_TIME": {"_type": "uniform", "_value": [0.0, 0.0]}, "EL_SB2_TIME": {"_type": "uniform", "_value": [0.0, 1.497]}, "EL_CF_TIME": {"_type": "uniform", "_value": [0.0, 0.0]}, "EL_CB_TIME": {"_type": "uniform", "_value": [0.0, 0.0]}, "EL_IV_STG_TIME_0": {"_type": "uniform", "_value": [0.311, 0.358]}, "EL_IV_STG_TIME_1": {"_type": "uniform", "_value": [0.34600000000000003, 0.914]}, "EL_IV_STG_TIME_2": {"_type": "uniform", "_value": [0.527, 0.7440000000000001]}, "EL_IV_STG_TIME_3": {"_type": "uniform", "_value": [0.106, 0.229]}, "EL_IV_STG_TIME_4": {"_type": "uniform", "_value": [0.048, 0.353]}, "op_mode": {"_type": "uniform", "_value": [2.0, 4.0]}, "ext_mold_time_flt_0": {"_type": "uniform", "_value": [0.0, 28000.0]}, "EL_VPS_TIME": {"_type": "uniform", "_value": [0.0, 0.0]}, "ext_mold_time_flt_1": {"_type": "uniform", "_value": [0.0, 8000.0]}, "ext_mold_time_flt_2": {"_type": "uniform", "_value": [0.0, 20000.0]}, "ext_mold_time_flt_3": {"_type": "uniform", "_value": [0.0, 2000.0]}, "ext_mold_time_flt_4": {"_type": "uniform", "_value": [0.0, 2000.0]}, "EL_GET_PART_TIME": {"_type": "uniform", "_value": [0.0, 0.0]}, "EL_NZL_MEAN": {"_type": "uniform", "_value": [248.00876, 252.9367]}, "EL_BARREL_MEAN_1": {"_type": "uniform", "_value": [258.09712, 270.70891]}, "EL_BARREL_MEAN_2": {"_type": "uniform", "_value": [248.4281, 258.29715]}, "EL_BARREL_MEAN_3": {"_type": "uniform", "_value": [238.21912000000003, 246.75429]}, "EL_BARREL_MEAN_4": {"_type": "uniform", "_value": [211.84534, 219.92403]}, "EL_BARREL_MEAN_5": {"_type": "uniform", "_value": [15.0, 15.0]}, "EL_BARREL_MEAN_6": {"_type": "uniform", "_value": [15.0, 15.0]}, "EL_BARREL_MEAN_7": {"_type": "uniform", "_value": [15.0, 15.0]}, "EL_HTRN_TI_0": {"_type": "uniform", "_value": [15.0, 15.0]}, "EL_HTRN_TI_1": {"_type": "uniform", "_value": [15.0, 15.0]}, "EL_HTRN_TI_2": {"_type": "uniform", "_value": [15.0, 15.0]}, "EL_HTRN_TI_3": {"_type": "uniform", "_value": [15.0, 15.0]}, "EL_HTRN_TI_4": {"_type": "uniform", "_value": [15.0, 15.0]}, "EL_HTRN_TI_5": {"_type": "uniform", "_value": [15.0, 15.0]}, "EL_HTRN_TI_6": {"_type": "uniform", "_value": [15.0, 15.0]}, "EL_HTRN_TI_7": {"_type": "uniform", "_value": [15.0, 15.0]}, "ext_mold_temp_flt_0": {"_type": "uniform", "_value": [15.0, 68.2]}, "ext_mold_temp_flt_1": {"_type": "uniform", "_value": [15.0, 58.9]}, "ext_mold_temp_flt_2": {"_type": "uniform", "_value": [15.0, 31.7]}, "ext_mold_temp_flt_3": {"_type": "uniform", "_value": [15.0, 103.5]}, "ext_mold_temp_flt_4": {"_type": "uniform", "_value": [0.0, 0.0]}, "ext_mold_temp_flt_5": {"_type": "uniform", "_value": [0.0, 0.0]}, "ext_mold_temp_flt_6": {"_type": "uniform", "_value": [0.0, 0.0]}, "ext_mold_temp_flt_7": {"_type": "uniform", "_value": [0.0, 0.0]}, "rob_enbl": {"_type": "uniform", "_value": [1.0, 1.0]}, "ext_mold_flow_flt_0": {"_type": "uniform", "_value": [5.0, 18.084266]}, "EL_MOP": {"_type": "uniform", "_value": [10.0, 289.28607]}, "EL_PLST_END_ERR": {"_type": "uniform", "_value": [10.0, 95.41502]}, "EL_INJ_START_STR": {"_type": "uniform", "_value": [104.02236, 106.61102]}, "EL_IV_END_STR": {"_type": "uniform", "_value": [39.980728000000006, 42.0]}, "EL_VPS_END_STR": {"_type": "uniform", "_value": [16.880153999999994, 41.98508]}, "EL_MIN_INJ_STR": {"_type": "uniform", "_value": [16.598343, 39.419006]}, "EL_EF_END_POS": {"_type": "uniform", "_value": [48.023884, 50.703327]}, "EL_PP_STG_POS_0": {"_type": "uniform", "_value": [21.992069, 44.527035]}, "EL_PP_STG_POS_1": {"_type": "uniform", "_value": [21.335789000000002, 44.223236]}, "EL_PP_STG_POS_2": {"_type": "uniform", "_value": [16.876579, 36.093275]}, "EL_PP_STG_POS_3": {"_type": "uniform", "_value": [10.0, 10.0]}, "EL_PP_STG_POS_4": {"_type": "uniform", "_value": [10.0, 10.0]}, "EL_CLAMP_PRESS": {"_type": "uniform", "_value": [53.827827, 133.01634]}, "EL_MAX_INJ_PRESS": {"_type": "uniform", "_value": [62.843433, 136.74251]}, "EL_IV_END_PRESS": {"_type": "uniform", "_value": [41.594925, 58.31493]}, "EL_CF_END_PRESS": {"_type": "uniform", "_value": [5.0, 5.0]}, "EL_PLST_BP_0": {"_type": "uniform", "_value": [5.0, 11.872882]}, "EL_PLST_BP_1": {"_type": "uniform", "_value": [5.0, 11.8771534]}, "EL_PLST_BP_2": {"_type": "uniform", "_value": [5.0, 5.0]}, "EL_PLST_BP_3": {"_type": "uniform", "_value": [5.0, 5.0]}, "EL_PLST_BP_4": {"_type": "uniform", "_value": [5.0, 5.0]}}
5、学习链接
注塑成型工艺的虚拟量测和调机优化 自动机器学习平台NNI
|