-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeterministic_main.py
More file actions
199 lines (153 loc) · 10.7 KB
/
Copy pathdeterministic_main.py
File metadata and controls
199 lines (153 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 17 11:21:19 2024
@author: afernandez
"""
def main():
import tensorflow as tf
import numpy as np
import os
from MODULES.PREPROCESSING.preprocessing_tools import read_data_previous
from MODULES.TRAINING.full_multivariate_models import My_Deterministic_InverseForward
from MODULES.POSTPROCESSING.postprocessing_tools import custom_plot_loss, plot_Data_loss, plot_GMM_loss, plot_All_losses
import tensorflow.keras as K
tf.config.list_physical_devices('GPU')
K.utils.set_random_seed(1024)
dt = 'float32' ## espcificar dtype para trabajar en float32.
K.backend.set_floatx(dt)
batch_size = 4096*2
full_cov = False
loc = os.path.join('Data', 'Data_nicoWT', 'joint.csv')
u_train, u_val, u_test, r_train, r_val, r_test, p_train, p_val, p_test = read_data_previous(loc, dt, batch_size, debug=False)
# n_selected = np.random.randint(1,u_train.shape[0]+1, size = 5*batch_size)
# u_train, r_train, p_train = u_train[n_selected,:], r_train[n_selected, :], p_train[n_selected,:]
# positions = [12, 4648, 7274, 2365, 243, 5015, 2458, 9659] # NEW SELECTION: IF WE CHANGE THE FIFTH CASE FROM 9657 TO 243.
# u_train, r_train, p_train = u_test[positions,:], r_test[positions,:], p_test[positions,:]
# u_val, r_val, p_val = u_train, r_train, p_train
# u_test, r_test, p_test = u_train, r_train, p_train
# batch_size = 8
# nf = 1*batch_size
# u_train, u_val, r_train, r_val, p_train, p_val = u_train[0:nf,:], u_val[0:nf,:], r_train[0:nf,:], r_val[0:nf,:], p_train[0:nf,:], p_val[0:nf,:]
u_val, r_val, p_val = u_test, r_test, p_test
u_train, r_train, p_train = u_test, r_test, p_test
# u_val, r_val, p_val = u_trin, r_train, p_train
# u_test, r_test, p_test = u_val, r_val, p_val
### the number of training data has to be multple of batch_size
# Para evitar que salgan Nones en las dimensiones no solo del batch sino en el resto de dimensiones (como numgaussians), teneemos qe ajustar el tamaño del dataset al batchsize:
#El dataset size tiene que ser múltiplo del batch size
# num_data: num_training_data//batch_size*batch_size to calculate how many data I have to use
# Nd = u_train.shape[0]//batch_size*batch_size
# # Add a string to indicate if diagonal or full covariance matrix:
if full_cov:
str_cov = "_full"
s_lb = 0.0001 #Lower bound for the scaling matrix diagonal terms
# s_ub_ = np.array([0.1]) #Upper bound for the Scaling matrix diagonal terms
s_ub_ = np.array([0.2]) #Upper bound for the Scaling matrix diagonal terms
else:
str_cov = "_diag"
## Lower and upper bound when diagonal covariance matrix:
s_lb = 0.000001 #Lower bound for the diagonal cov matrix elements
s_ub_ = np.array([1.]) #Upper bound for the diagonal cov matrix elements
date = 'Deterministic_Test_Cond_16Dec'+ str(str_cov)
# pos_features_ = [np.transpose([15,16,17,18,19,20,21,22,23,24,25,26,27,28,29])] #Only angles Roll, Pitch, Yaw.
# feature_names = ['RollPitchYaw']
pos_features_ = [np.transpose([15,16,17,18,19])] #Only Roll
feature_names = ['Roll']
# pos_features_ = [np.transpose([20,21,22,23,24])]
# feature_names = ['Pitch']
# pos_features_ = [np.transpose([15,16,17,18,19]), np.transpose([20,21,22,23,24]), np.transpose([25,26,27,28,29]),np.transpose([15,16,17,18,19,20,21,22,23,24]), np.transpose([15,16,17,18,19,25,26,27,28,29]), np.transpose([20,21,22,23,24,25,26,27,28,29]) ]
# feature_names = ['Roll', 'Pitch', 'Yaw', 'RollPitch', 'RollYaw', 'PitchYaw']
num_mixtures = p_train.shape[1]
num_gaussians_ = np.array([1])
num_samples_per_mixture_ = np.array([1])
input_dim_decoder = p_train.shape[1] + r_train.shape[1]
output_dim = u_train.shape[1]
loss_name = ['ELBOloss']
inverse_LR = 1e-04
n_epochs_enc_ = np.array([5000])
betas = [0.001] #because we are trying to introduce different alpha for each property
#For P_anch this beta is fine, but a lot of uncertainty is seen in the gaussians of p_bio. So that is why we need to keep it this lebel.
#Increasing dthe vlaue of beta witll result in gaussians close to uniform, covering the entire itnerval [0,1]
for k in range(len(num_gaussians_)):
for kk in range(len(n_epochs_enc_)):
for kkk in range(len(pos_features_)):
for kkkk in range(len(betas)):
for kkkkk in range(len(s_ub_)):
s_ub = s_ub_[kkkkk]
beta = betas[kkkk]
n_epochs_enc = n_epochs_enc_[kk]
num_samples_per_mixture = num_samples_per_mixture_[0]
num_gaussians = num_gaussians_[k]
selected_features = pos_features_[kkk]
feature_name = feature_names[kkk]
input_dim_encoder = u_train[:,selected_features].shape[1] + r_train.shape[1]
forward_path = os.path.join("Output", '23Apr_2Prop_Forward') #path of the best saved forward not to be touched anymore
model_forward = tf.saved_model.load(os.path.join(forward_path,'model_forward_2mix'))
model = My_Deterministic_InverseForward(input_dim_encoder, input_dim_decoder, output_dim, num_mixtures, num_gaussians, num_samples_per_mixture, model_forward, selected_features, beta, full_cov, s_ub, s_lb)
filename = f'{date}_{feature_name}_2Props_{beta}Beta_InverseForward_{num_gaussians}Gaussians_{num_samples_per_mixture}Samples_{inverse_LR}LR_{n_epochs_enc}epochs_{batch_size}batch'
folder_path = os.path.join('Output', filename)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
Problem_info = {
'input_dim_enc':input_dim_encoder,
'input_dim_dec':input_dim_decoder,
'n_mixtures': num_mixtures,
'n_gaussians':num_gaussians,
'n_samples': num_samples_per_mixture,
'epochs': n_epochs_enc,
'LR': inverse_LR,
'selected_features':selected_features,
'beta': beta,
's_lb': s_lb,
's_ub':s_ub
}
np.save(os.path.join(folder_path, 'Problem_info.npy'), Problem_info, allow_pickle=True)
Data_info = {
'u_val': u_val,
'r_val':r_val,
'u_test': u_test,
'r_test': r_test,
'p_val':p_val,
'p_test': p_test
}
np.save(os.path.join(folder_path, 'Data_info.npy'), Data_info, allow_pickle=True)
# checkpoint_path = os.path.join(folder_path, 'model_weights.ckpt')
# cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
# save_weights_only=True,
# verbose=1)
model.compile(optimizer = K.optimizers.Adam(learning_rate = inverse_LR), loss = model.Data_loss)
model_history = model.fit(x = [u_train, r_train, p_train],
y = u_train,
batch_size = batch_size,
epochs = n_epochs_enc,
shuffle = True,
validation_data = ([u_val, r_val, p_val], u_val))
# save the weights
model.save_weights(os.path.join(folder_path, "Weights_ae.h5"), overwrite = True)
inverse_model = model.Encoder_model
z_pred_test = inverse_model.predict([u_test[:,selected_features], r_test])
Test_results_info = {'test_p_predicted': z_pred_test }
np.save(os.path.join(folder_path, 'Test_results_info.npy'), Test_results_info, allow_pickle=True)
history_path = os.path.join(folder_path, 'model_history.npy')
np.save(os.path.join(history_path), model_history.history, allow_pickle=True)
# model_history = np.load(history_path, allow_pickle = True)
# history = np.load(os.path.join(folder_path, 'model_history.npy'), allow_pickle=True).item()
# custom_plot_loss(model_history,folder_path, 'loss_autoencoder')
# plot_All_losses(model_history, folder_path)
# plot_Data_loss(model_history.history['Conditional_likelihood_term'], folder_path)
# plot_GMM_loss(model_history.history['Mixture_dens_term'], folder_path)
# from MODULES.POSTPROCESSING.Multivariate_Full_Results_analysis import plot_test_contourplots_gpu, compare_losses
# plot_test_contourplots_gpu(model, selected_features, u_test, r_test, p_test, num_mixtures, num_gaussians, full_cov, folder_path)
# # compare_losses(model_history.history, folder_path)
# ######################## some calculations of the SNR
# aa = model_forward(tf.concat((p_train,r_train), axis = 1))
# A = beta * aa[:,4]
# A2 = tf.square(A)
# avg_A2 = tf.math.reduce_mean(A2)
# avg_S2 = tf.math.reduce_mean(tf.square(u_train[:,4]))
# Q = tf.cast(avg_S2, dtype = tf.float64)/tf.cast(avg_A2, dtype = tf.float64)
# SNR = 10*np.log10(Q)
############## This operation allows to exeute functions in the script
if __name__ == "__main__":
main()