# 载入FD001数据集
file_load = 'd:/python_keras/predict aircraft engineer RUL/Dataset/PM_train_guiyihua.csv'
train_df = pd.read_csv(file_load, encoding='gbk')
# print(train_df)
new_features = ['cycle_norm', 's4', 's11', 's12', 's9', 's7', 's14', 's15',
's21', 's2', 's3', 's20', 'setting1', 's13', 's8', 's17']
x_t = train_df[new_features]
y_t = train_df['RUL']
# 模型参数设置
# 对模型进行优化(每层神经元数量, 每层丢弃率, 迭代步数, 批次样本数)
d = [0.05, 0.1, 0.2, 0.3, 0.4, 0.5]
neuron = [1024, 512, 256, 128, 64, 32]
epochs = [10, 20, 50, 100, 1000]
batch_size = [10, 25, 50, 100, 200]
seq_length = [10, 20, 30, 50]
def model_t_c(d, n1, n2):
model = Sequential()
model.add(GRU(n1, input_shape=(1, 16)))
model.add(Dropout(d))
model.add(Dense(n2, activation='sigmoid'))
model.add(Dense(1, activation='linear'))
model.compile(optimizer='rmsprop', loss='mse', metrics='mae')
model.summary()
return model
model_t = model_t_c(0.2, 256, 256)
# 贝叶斯优化
hyperparameters = {"d": Real(0, 1, prior='log-uniform'), "n1":Real(0, 1e4, prior='uniform'), "n2":Real(0, 1e4, prior='uniform')}
Kmodel = KerasClassifier(build_fn=model_t, verbose=1)
bayesian = BayesSearchCV(estimator = Kmodel, search_spaces = hyperparameters, n_iter = 200, cv = 5, random_state=42, n_jobs = -1, scoring='neg_mean_squared_error')
bayesian.fit(x_t, y_t)
print(f'Best parameters: {bayesian.best_params_}')
print(f'Best score: {bayesian.best_score_}')