I have a trained neural network model by keras, It is a regression problem, where I am trying to predict 1 output variable using some 16 input variables or features. As a next step, I would like to minimize my output and want to determine what configuration these 16 inputs would take to get to the minimal value of the output.
So, essentially, so in order to pass the trained model to be my objective function in the minimize function , I saved the model and call it , I keep receiving this error
ValueError: Input 0 of layer sequential_3 is incompatible with the layer: expected axis -1 of input shape to have value 16 but received input with shape [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')