Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
giuliobarl authored Jun 13, 2024
1 parent 53f1087 commit d83b65b
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 80 deletions.
77 changes: 56 additions & 21 deletions Regression/Couples.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
import tqdm
from scipy.optimize import least_squares
from itertools import combinations
import pickle
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)



# data preprocessing

Data = pd.read_excel('Gnielinski Noise.xlsx', index_col = 0)

train_df, test_df = train_test_split(Data, test_size=0.15, random_state=0)
Expand All @@ -27,9 +27,10 @@
y_train = train_df.iloc[:, -1]
y_test = test_df.iloc[:, -1]

#Let the normalization layer choose how to normalize data on the training set

normalizer = tf.keras.layers.Normalization(axis=-1)
normalizer.adapt(np.array(X_train)) # Let the normalization layer choose how to normalize data on the training set
normalizer = tf.keras.layers.Normalization(axis=-1)
normalizer.adapt(np.array(X_train))

def build_and_compile_model(norm): # Define the model
model = Sequential([
Expand All @@ -46,20 +47,42 @@ def build_and_compile_model(norm): #
Dense(1, activation = 'linear')
])

model.compile(loss = 'mean_absolute_error',
optimizer = Adam(0.001))
model.compile(loss='mean_absolute_error',
optimizer=tf.keras.optimizers.Adam(0.001))
return model

es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=200) # Use early stopping regularization
model = build_and_compile_model(normalizer) # Build the model (fitting can be avoided if the model has already been trained and saved)
# Callbacks
es = EarlyStopping(monitor = 'val_loss', mode = 'min',
verbose = 1, patience = 50)

checkpoint_filepath = './tmp/checkpoint_gniel'
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
filepath = checkpoint_filepath,
save_weights_only = True,
monitor = 'val_loss',
mode = 'min',
save_best_only = True)

model = build_and_compile_model(normalizer) # Build the model

#time
history = model.fit(
X_train,
y_train,
validation_split=0.15,
verbose=2, epochs=500, callbacks=[es])
verbose=2, epochs=500,
callbacks = [es, model_checkpoint_callback])

with open('GnielHistoryDict', 'wb') as file_pi:
pickle.dump(history.history, file_pi) # Save the model history

# Load saved weights (best model)
model.load_weights(checkpoint_filepath)
model.save('gnielinski_model.tf') # Save the weights of the best model for later use


with open('GnielHistoryDict', "rb") as file_pi:
history = pickle.load(file_pi)

test_predictions = model.predict(X_test).flatten()
test_labels = y_test.values # True y over samples of the testing set
Expand Down Expand Up @@ -89,21 +112,32 @@ def build_and_compile_model(norm): #
xy=(0.02 * delta, 0.75 * delta),
xytext=(0.02 * delta, 0.75 * delta),
)
ax1.set_xlabel(r"True y")
ax1.set_ylabel(r"Predicted y")
ax1.set_xlabel(r"True $\overline{\rm{Nu}}$")
ax1.set_ylabel(r"Predicted $\overline{\rm{Nu}}$")
ax1.text(-0.3, 0.9, 'a', transform=ax1.transAxes,
size=12, weight='bold')

ax2.plot(history.history['loss'], label='loss')
ax2.plot(history.history['val_loss'], label='validation loss')
ax2.plot(history['loss'], label='loss')
ax2.plot(history['val_loss'], label='validation loss')
ax2.set_yscale('log')
ax2.text(-0.35, 0.9, 'b', transform=ax2.transAxes,
size=12, weight='bold')

ax2.set_xlabel('Epoch')
ax2.set_ylabel('MAE')
ax2.legend(frameon = False)
plt.subplots_adjust(wspace=0.4)


plt.savefig('gniel_model.svg', bbox_inches='tight')

plt.show()


###############################################################################
#starting feature grouping analysis
new_model = tf.keras.models.load_model('gnielinski_model.tf') # alternatively, load saved model

model = tf.keras.models.load_model('gnielinski_model.tf') # alternatively, load saved model

other_columns = Data.iloc[:, :-1].columns

Expand Down Expand Up @@ -158,7 +192,6 @@ def delta(lista, Data, i): #
lista.append([ll[i][0], ll[i][1][::-1]])



N = 20 # number of points evaluated per iteration
repeat = 3 # number of iterations

Expand All @@ -173,6 +206,7 @@ def delta(lista, Data, i): #
c = -0.5*np.ones(N)
d = 0.5*np.ones(N)


for j in range(repeat):

beta = np.array([np.mean(a), np.mean(b), np.mean(c), np.mean(d)]) # initial guess for successive iterations
Expand Down Expand Up @@ -237,22 +271,22 @@ def MAIN(beta):

with tf.GradientTape(persistent=True) as tape:
tape.watch(x0)
preds1 = new_model(x0) # to differentiate the function
preds1 = model(x0) # to differentiate the function
dy_dx1 = tape.gradient(preds1, x0)[index_1] # evaluate the partial derivative of the model in x0 with respect to the first feature

with tf.GradientTape(persistent=True) as tape:
tape.watch(x0)
preds2 = new_model(x0)
preds2 = model(x0)
dy_dx2 = tape.gradient(preds2, x0)[index_2]

with tf.GradientTape(persistent=True) as tape:
tape.watch(x0)
preds3 = new_model(x0)
preds3 = model(x0)
dy_dx3 = tape.gradient(preds3, x0)[index_3]

with tf.GradientTape(persistent=True) as tape:
tape.watch(x0)
preds4 = new_model(x0)
preds4 = model(x0)
dy_dx4 = tape.gradient(preds4, x0)[index_4]

grad1 = dy_dx1.numpy() # convert to array
Expand All @@ -271,14 +305,14 @@ def MAIN(beta):

un = univec(a0, b0, c0, d0, x0.numpy(), index_1, index_2,
index_3, index_4) # get un to check for the condition of invariance (components of the gradient aligned with un in x0)
DIFF = np.array([np.dot(der, un),
DIFF = np.array([np.dot(der, un)[0],
a0**2 + b0**2 - 1, c0**2 + d0**2 - 1],
dtype = float) # evaluate the resiudal

return(DIFF)

x = least_squares(MAIN, beta, method = 'trf', ftol = 2.3e-16,
verbose = 0, max_nfev = 50) # least-squares for rectangular matrices
verbose = 2, max_nfev = 20) # least-squares for rectangular matrices

a[k] = x.x[0]
b[k] = x.x[1]
Expand All @@ -302,7 +336,8 @@ def MAIN(beta):

frame = pd.DataFrame(array, columns = ['mean a', 'std a', 'mean b', 'std b',
'mean c', 'std c', 'mean d',
'std d']) # convert to dataframe for simplicity
'std d'],
index = lista) # convert to dataframe for simplicity

# scatter plots
for ind in range(len(A)):
Expand Down
50 changes: 38 additions & 12 deletions Regression/Dittus_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import tqdm
from scipy.optimize import broyden1, least_squares
from itertools import combinations, product
import pickle
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

Expand Down Expand Up @@ -63,22 +64,43 @@ def build_and_compile_model(norm): #
optimizer=tf.keras.optimizers.Adam(0.001))
return model

es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=200) # Use early stopping regularization
# Callbacks
es = EarlyStopping(monitor = 'val_loss', mode = 'min',
verbose = 1, patience = 50)

checkpoint_filepath = './tmp/checkpoint_dittus'
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
filepath = checkpoint_filepath,
save_weights_only = True,
monitor = 'val_loss',
mode = 'min',
save_best_only = True)

model = build_and_compile_model(normalizer) # Build the model (fitting can be avoided if the model has already been trained and saved)

#time
history = model.fit(
X_train,
y_train,
validation_split=0.15,
verbose=2, epochs=500, callbacks=[es])
verbose=2, epochs=500,
callbacks = [es, model_checkpoint_callback])


with open('DittusHistoryDict', 'wb') as file_pi:
pickle.dump(history.history, file_pi) # Save the model history

# Load saved weights (best model)
model.load_weights(checkpoint_filepath)
model.save('dittus_model.tf') # Save the weights of the best model for later use


with open('DittusHistoryDict', "rb") as file_pi:
history = pickle.load(file_pi)

test_predictions = model.predict(X_test).flatten()
test_labels = y_test.values # True y over samples of the testing set

model.save('dittus_model.tf') # Save the model for later use

r2 = r2_score(test_labels, test_predictions)
mae = mean_absolute_error(test_labels, test_predictions)
rmse = np.sqrt(mean_squared_error(test_labels, test_predictions)) # Goodness metrics evaluation
Expand Down Expand Up @@ -109,8 +131,9 @@ def build_and_compile_model(norm): #
ax1.text(-0.3, 0.9, 'a', transform=ax1.transAxes,
size=12, weight='bold')

ax2.plot(history.history['loss'], label='loss')
ax2.plot(history.history['val_loss'], label='validation loss')
ax2.plot(history['loss'], label='loss')
ax2.plot(history['val_loss'], label='validation loss')
ax2.set_yscale('log')
ax2.text(-0.3, 0.9, 'b', transform=ax2.transAxes,
size=12, weight='bold')

Expand All @@ -120,13 +143,14 @@ def build_and_compile_model(norm): #
plt.subplots_adjust(wspace=0.4)


plt.savefig('model.svg', bbox_inches='tight')
plt.savefig('dittus_model.svg', bbox_inches='tight')

plt.show()


###############################################################################
#starting feature grouping analysis
new_model = tf.keras.models.load_model('dittus_model.tf') # alternatively, load saved model
model = tf.keras.models.load_model('dittus_model.tf') # load saved model

other_columns = X_train.columns

Expand Down Expand Up @@ -216,14 +240,14 @@ def MAIN(beta):
x0 = tf.constant(tx) # create x0

un = univec(a0, b0, x0.numpy(), index_1, index_2) # get un to check for the condition of invariance (components of the gradient aligned with un in x0)
DIFF = np.array([np.dot(der, un),
DIFF = np.array([np.dot(der, un)[0],
a0**2 + b0**2 - 1], dtype = float) # evaluate the resiudal

return(DIFF)


x = least_squares(MAIN, beta, method = 'trf', ftol = 2.3e-16,
verbose = 0, max_nfev = 50) # least-squares with Levenberg-Marquardt does not work with rectangular matrices
verbose = 1, max_nfev = 50) # least-squares with Levenberg-Marquardt does not work with rectangular matrices
x.x = x.x/np.linalg.norm(x.x)

a[k] = x.x[0] # save the results for each of the 100 iterations
Expand All @@ -250,6 +274,8 @@ def MAIN(beta):
plt.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left')
plt.ylim(-1,1)
plt.show()




###############################################################################
Expand Down Expand Up @@ -371,7 +397,7 @@ def MAIN(beta):


x = least_squares(MAIN, beta, method = 'trf', ftol = 2.3e-16,
verbose = 0, max_nfev = 50) # least-squares for rectangular matrices
verbose = 1, max_nfev = 50) # least-squares for rectangular matrices
x.x = x.x/np.linalg.norm(x.x)

a[k] = x.x[0] # save the results for each of the N iterations
Expand Down Expand Up @@ -403,4 +429,4 @@ def MAIN(beta):
plt.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left')
plt.ylim(-1,1)
plt.show()


Loading

0 comments on commit d83b65b

Please sign in to comment.