I'm working on creating a surrogate model for some abaqus models base on Aluminum testing. I have 5 input parameters and 1 output parameter. I have 1000 rows of data for training and 20 rows of data for evaluating and showing prediction accuracy. I am having trouble getting the model to give me accurate predictions. I've tried a few things and would appreciate any suggestions.
Here is the base code for my functional api model.
#Model Generation
input0 = keras.layers.Input(shape=(1,))
input1 = keras.layers.Input(shape=(1,))
input2 = keras.layers.Input(shape=(1,))
input3 = keras.layers.Input(shape=(1,))
input4 = keras.layers.Input(shape=(1,))
merged = keras.layers.Concatenate(axis=1)([input0, input1, input2, input3, input4])
dense1 = keras.layers.Dense(5, input_dim=5, activation='relu')(merged)
output = keras.layers.Dense(1, activation='relu')(dense1)
model = keras.Model(inputs=[input0, input1, input2, input3, input4], outputs=[output])
#Model Summary and Print
model.summary()
#keras.utils.plot_model(model, "model.png")
#Need to install things to plot
#Compile the Model
model.compile(
loss='mse',
optimizer='adam',
metrics=["mae"]
)
#Train Model
model.fit([in_array_0, in_array_1, in_array_2, in_array_3, in_array_4],out_array, batch_size=32, epochs=500
#,callbacks=keras.callbacks.EarlyStopping(monitor='loss', min_delta=1e-2, patience=5,
verbose=0)
)
#Evaluate
model.evaluate([saved_in_array_0, saved_in_array_1, saved_in_array_2, saved_in_array_3,
saved_in_array_4],saved_out_array, verbose=2)
#save model
model.save('Al_Total_Surrogate_5-14-2021_adam.h5')
del model
model = load_model('Al_Total_Surrogate_5-14-2021_adam.h5')
#Test
predictions = model.predict([saved_in_array_0, saved_in_array_1, saved_in_array_2, saved_in_array_3,
saved_in_array_4])
print ("Input 0:", saved_in_array_0)
print ("Input 1:", saved_in_array_1)
print ("Input 2:", saved_in_array_2)
print ("Input 3:", saved_in_array_3)
print ("Input 4:", saved_in_array_4)
print ("predictions:", predictions)
print ("Actual:", saved_out_array)
for i in range(20):
val = predictions[i]
Per_Error = abs(val-saved_out_array[i])/saved_out_array[i]*100
print("Predicted: ", val, " Actual: ", saved_out_array[i], " Percent Error: ", Per_Error)
#File
RESULTSFILE = 'Al_Total_Surrogate_5-14-2021_adam.txt'
with open(RESULTSFILE, mode='w') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',')
csv_writer.writerow(['Epoch: 500'])
csv_writer.writerow(['Batch: 10'])
csv_writer.writerow(['Layers: 2'])
csv_writer.writerow(['Layer 1: relu'])
csv_writer.writerow(['Layer 2: relu'])
csv_writer.writerow(['Optimizer: adam'])
for i in range(20):
val = predictions[i]
Per_Error = abs(val-saved_out_array[i])/saved_out_array[i]*100
csv_writer.writerow(['Predicted: ', val, ' Actual: ', saved_out_array[i], ' Percent
Error: ', Per_Error])
Below are the results from the above code.
Predicted: [188.32097] Actual: 224.7629395 Percent Error: [16.213514]
Predicted: [153.02399] Actual: 163.6922913 Percent Error: [6.517292]
Predicted: [184.92519] Actual: 171.8843994 Percent Error: [7.5869517]
Predicted: [153.99652] Actual: 184.1511993 Percent Error: [16.374956]
Predicted: [170.86081] Actual: 198.1195374 Percent Error: [13.758728]
Predicted: [167.84995] Actual: 184.6322632 Percent Error: [9.089591]
Predicted: [175.9255] Actual: 202.3720093 Percent Error: [13.068262]
Predicted: [217.255] Actual: 204.0169067 Percent Error: [6.488726]
Predicted: [222.72789] Actual: 210.9862366 Percent Error: [5.565128]
Predicted: [161.50806] Actual: 199.8728027 Percent Error: [19.19458]
Predicted: [208.30794] Actual: 175.3465271 Percent Error: [18.797869]
Predicted: [213.59142] Actual: 183.5816803 Percent Error: [16.346804]
Predicted: [172.13594] Actual: 175.7514648 Percent Error: [2.0571802]
Predicted: [196.41121] Actual: 174.9219208 Percent Error: [12.285074]
Predicted: [182.2879] Actual: 215.4689026 Percent Error: [15.399438]
Predicted: [200.62784] Actual: 158.2305145 Percent Error: [26.794657]
Predicted: [206.18555] Actual: 198.1768951 Percent Error: [4.0411634]
Predicted: [192.50092] Actual: 195.0370026 Percent Error: [1.3003107]
Predicted: [159.96469] Actual: 177.1092072 Percent Error: [9.680195]
Predicted: [220.8257] Actual: 196.6077118 Percent Error: [12.317924]
The model seems a bit random and I'm having issues making it converge to a mean below 10% error. Any ideas would be greatly appreciated!
I also generated a sequential model for this about a year ago which provided very accurate results. I need to get functional api to work because I am trying to expand to 10 inputs for a similar problem.
Here is the sequential. How would I move over the estimator to the functional api?
########## Defining Base Model ##########
def baseline_model():
#creat model
model = Sequential()
model.add(Dense(5, input_dim=5, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, kernel_initializer='normal'))
#compile model
model.compile(loss='mse', optimizer='adam')
filename = 'finalized_model.sav'
#pickle.dump(model, open(filename, 'wb'))
return model
########## Seeding and Estimation ##########
# fix the random seed for reproducibility
seed = 10
np.random.seed(seed)
'''
This seed sets the randomability of the function to a number.
This way if you created multiple models they would all be based off the
same randominess.
'''
########## Create the Pipeline ##########
# The pipeline is the model in this case
estimators = []
estimators.append(('standardize', StandardScaler()))
estimators.append(('mlp', KerasRegressor(build_fn=baseline_model, epochs=5000, batch_size=10,
verbose=0)))
pipeline = Pipeline(estimators)
epoch=5000
batch=10
######### Evaluate the Model ##########
'''
This just tells you how good of a model it could create.
We end up using the saved model later in the fit section.
'''
kfold = KFold(n_splits=10)
#kfold = KFold(n_splits=10, random_state=seed)
results = cross_val_score(pipeline, inn, out, cv=kfold)
print("Results: %.2f (%.2f) MSE" % (results.mean(), results.std()))
######### Fit the Pipeline ##########
my_model = pipeline.fit(inn, out)