首页 > 解决方案 > 如何让最小化函数理解我的神经网络模型?

问题描述

我有一个训练有素的 keras 神经网络模型,这是一个回归问题,我试图使用大约 16 个输入变量或特征来预测 1 个输出变量。作为下一步,我想最小化我的输出,并想确定这 16 个输入将采用什么配置来达到输出的最小值。

所以,本质上,为了在最小化函数中将训练模型作为我的目标函数,我保存了模型并调用它,我一直收到这个错误

ValueError:层序 3 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 16,但接收到形状为 [None,1] 的输入

import keras
import tensorflow as tf
from setuptools.sandbox import save_path

tf.random.set_seed(7)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas.plotting import scatter_matrix
import seaborn as sb
sb.set(style="whitegrid")


# 2. read the csv file and add the names from another file
data = pd.read_csv("CBM.csv", names= ['Lever position', 'Ship speed',
                         'Gas Turbine shaft torque','Gas Turbine rate of revolutions','Gas Generator rate of revolutions',
                         'Starboard Propeller Torque','Port Propeller Torque','HP Turbine exit temperature','GT Compressor inlet air temperature',
                         'GT Compressor outlet air temperature','HP Turbine exit pressure','GT Compressor inlet air pressure',
                         'GT Compressor outlet air pressure','Gas Turbine exhaust gas pressure','Turbine Injecton Control',
                         'Fuel flow','GT Compressor decay state coefficient', 'GT Turbine decay state coefficient']
                         )

#-----------------------------------



#-------------------------------------------------------------------

# 3. Split the data to targets and features and transform to numpy array

targets = np.array(data[['GT Turbine decay state coefficient'])
print ("targets shape is \n",targets.shape)
features = data.drop(['GT Compressor decay state coefficient','GT Turbine decay state coefficient'], axis = 1)
"""
features = data.drop(['GT Compressor decay state coefficient','GT Compressor inlet air pressure',
                      'Fuel flow','Turbine Injecton Control','Gas Turbine exhaust gas pressure','Starboard Propeller Torque',
                      'Port Propeller Torque','Ship speed','Gas Turbine shaft torque',
                      'HP Turbine exit temperature','HP Turbine exit pressure','Gas Turbine rate of revolutions',
                      'GT Compressor inlet air temperature','GT Turbine decay state coefficient'], axis = 1)
"""
feature_list = list(features.columns) # to store the columns' names in a list for further useage

features = np.array(features[feature_list])
print ("features shape is \n",features.shape)
#----------------------------
# 4. normalize the data:
    # a. some normalize all the features then split to train and test sets OR,
    # b. others split the data to test and train sets then normalize the features sets

# import libraries for split
from sklearn import preprocessing
from sklearn.model_selection import train_test_split

min_max_scaler = preprocessing.MinMaxScaler()
X_scale = min_max_scaler.fit_transform(features)

X_train, X_test, Y_train, Y_test = train_test_split(X_scale, targets, test_size=0.25, random_state=1)
#X_train = min_max_scaler.fit_transform(Xs_train)
#X_test = min_max_scaler.fit_transform(Xs_test)
#print (Y_test.shape)

#plt.hist(data['GT Compressor decay state coefficient'])
"""
# try to draw the pair plot scatter

#x_label = feature_list[0:2]

#y_label = ['GT Compressor decay state coefficient','GT Turbine decay state coefficient']
#df = data[y_label + x_label]


# Pair plot
sb.pairplot(df, markers = '.')
plt.tight_layout()
plt.show()
"""
# 5. Build the neural model from keras library
# import libraries

from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
from keras.losses import mean_absolute_percentage_error
from keras.losses import mean_squared_error
from keras.losses import mean_absolute_error

    # define the keras model as a function

def get_model():
    model = Sequential()
    # add the the first layer, with  the activation function and determine the number of inputs
    model.add(Dense(12, activation = 'linear', input_dim = 16, kernel_initializer='uniform'))
    # add hidden layers as much as needed
    model.add(Dense(units=13, activation='sigmoid', kernel_initializer='uniform'))
    
    # Adding the output layer, determine the number of outputs (in this case we have 2)
    model.add(Dense(units=1, activation='linear'))
    opt = Adam(lr=0.025)
    model.compile(loss=mean_absolute_error, optimizer=opt, metrics=['mape'])

    return model



# 6. Calculate the features importance (Permutation importance)

from sklearn.model_selection import cross_val_score
from keras.wrappers.scikit_learn import KerasRegressor
import eli5
from eli5.sklearn import PermutationImportance

estimator = KerasRegressor(build_fn=get_model,validation_split = 0.2, batch_size=100, epochs=1000)
history = estimator.fit(X_train, Y_train)

perm = PermutationImportance(estimator, random_state=1).fit(X_train,Y_train)
w = eli5.show_weights(perm, feature_names = feature_list)
result = pd.read_html(w.data)[0]# this to read the object
print(result)

# 7. Summarize the history loss per epoch

Y_pred = get_model().predict(X_test)



#------------------------------
model = get_model()
model.save("model")


#"""
from scipy.optimize import minimize
import scipy




my_model = keras.models.load_model("model")

def obj (params):
       
    return my_model.predict(params)

s = [0]*16

guess_params= np.array([s,])

# Given a trained model, optimize the inputs to minimize the output.
optim_params = scipy.optimize.minimize(obj,guess_params,method='Nelder-Mead')

标签: scipy-optimize-minimize

解决方案


推荐阅读