def create_image_training_epoch(X_data_tr, Y_data_tr, X_data_val, Y_data_val, tr_loss, val_loss, x_grid, y_grid, cf_a, video_fotograms_folder, epoch_i): """ Creates the image of the training and validation accuracy """ gl.init_figure(); ax1 = gl.subplot2grid((2,1), (0,0), rowspan=1, colspan=1) ax2 = gl.subplot2grid((2,1), (1,0), rowspan=1, colspan=1) plt.title("Training") ## First plot with the data and predictions !!! ax1 = gl.scatter(X_data_tr, Y_data_tr, ax = ax1, lw = 3,legend = ["tr points"], labels = ["Analysis of training", "X","Y"]) gl.scatter(X_data_val, Y_data_val, lw = 3,legend = ["val points"]) gl.plot (x_grid, y_grid, legend = ["Prediction function"]) gl.set_zoom(xlimPad = [0.2, 0.2], ylimPad = [0.2,0.2], X = X_data_tr, Y = Y_data_tr) ## Second plot with the evolution of parameters !!! ax2 = gl.plot([], tr_loss, ax = ax2, lw = 3, labels = ["RMSE. lr: %.3f"%cf_a.lr, "epoch","RMSE"], legend = ["train"]) gl.plot([], val_loss, lw = 3, legend = ["validation"], loc = 3) gl.set_fontSizes(ax = [ax1,ax2], title = 20, xlabel = 20, ylabel = 20, legend = 20, xticks = 12, yticks = 12) # Set final properties and save figure gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.30, hspace=0.30) gl.savefig(video_fotograms_folder +'%i.png'%epoch_i, dpi = 100, sizeInches = [14, 10], close = True, bbox_inches = None)
def plot_learnt_function(X_data_tr, Y_data_tr, X_data_val, Y_data_val, x_grid, y_grid, cf_a, folder_images): gl.init_figure() ax1 = gl.scatter(X_data_tr, Y_data_tr, lw=3, legend=["tr points"], labels=["Data", "X", "Y"], alpha=0.2) ax2 = gl.scatter(X_data_val, Y_data_val, lw=3, legend=["val points"], alpha=0.2) gl.set_fontSizes(ax=[ax1, ax2], title=20, xlabel=20, ylabel=20, legend=20, xticks=12, yticks=12) gl.plot(x_grid, y_grid, legend=["training line"]) gl.savefig(folder_images + 'Training_Example_Data.png', dpi=100, sizeInches=[14, 4])
def create_image_training_epoch(X_data_tr, Y_data_tr, X_data_val, Y_data_val, tr_loss, val_loss, x_grid, y_grid, cf_a, video_fotograms_folder, epoch_i): """ Creates the image of the training and validation accuracy """ gl.init_figure() ax1 = gl.subplot2grid((2, 1), (0, 0), rowspan=1, colspan=1) ax2 = gl.subplot2grid((2, 1), (1, 0), rowspan=1, colspan=1) plt.title("Training") ## First plot with the data and predictions !!! ax1 = gl.scatter(X_data_tr, Y_data_tr, ax=ax1, lw=3, legend=["tr points"], labels=["Analysis of training", "X", "Y"]) gl.scatter(X_data_val, Y_data_val, lw=3, legend=["val points"]) gl.plot(x_grid, y_grid, legend=["Prediction function"]) gl.set_zoom(xlimPad=[0.2, 0.2], ylimPad=[0.2, 0.2], X=X_data_tr, Y=Y_data_tr) ## Second plot with the evolution of parameters !!! ax2 = gl.plot([], tr_loss, ax=ax2, lw=3, labels=["RMSE. lr: %.3f" % cf_a.lr, "epoch", "RMSE"], legend=["train"]) gl.plot([], val_loss, lw=3, legend=["validation"], loc=3) gl.set_fontSizes(ax=[ax1, ax2], title=20, xlabel=20, ylabel=20, legend=20, xticks=12, yticks=12) # Set final properties and save figure gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.30, hspace=0.30) gl.savefig(video_fotograms_folder + '%i.png' % epoch_i, dpi=100, sizeInches=[14, 10], close=True, bbox_inches=None)
def plot_evolution_RMSE(tr_loss, val_loss, cf_a, folder_images): gl.init_figure() ax1 = gl.plot([], tr_loss, lw = 3, labels = ["RMSE loss and parameters. Learning rate: %.3f"%cf_a.lr, "","RMSE"], legend = ["train"]) gl.plot([], val_loss, lw = 3, legend = ["validation"]) gl.set_fontSizes(ax = [ax1], title = 20, xlabel = 20, ylabel = 20, legend = 20, xticks = 12, yticks = 12) gl.savefig(folder_images +'Training_Example_Parameters.png', dpi = 100, sizeInches = [14, 7])
def plot_learnt_function(X_data_tr, Y_data_tr, X_data_val, Y_data_val, x_grid, y_grid, cf_a, folder_images): gl.init_figure() ax1 = gl.scatter(X_data_tr, Y_data_tr, lw = 3,legend = ["tr points"], labels = ["Data", "X","Y"], alpha = 0.2) ax2 = gl.scatter(X_data_val, Y_data_val, lw = 3,legend = ["val points"], alpha = 0.2) gl.set_fontSizes(ax = [ax1,ax2], title = 20, xlabel = 20, ylabel = 20, legend = 20, xticks = 12, yticks = 12) gl.plot (x_grid, y_grid, legend = ["training line"]) gl.savefig(folder_images +'Training_Example_Data.png', dpi = 100, sizeInches = [14, 4])
def plot_evolution_RMSE(tr_loss, val_loss, cf_a, folder_images): gl.init_figure() ax1 = gl.plot([], tr_loss, lw=3, labels=[ "RMSE loss and parameters. Learning rate: %.3f" % cf_a.lr, "", "RMSE" ], legend=["train"]) gl.plot([], val_loss, lw=3, legend=["validation"]) gl.set_fontSizes(ax=[ax1], title=20, xlabel=20, ylabel=20, legend=20, xticks=12, yticks=12) gl.savefig(folder_images + 'Training_Example_Parameters.png', dpi=100, sizeInches=[14, 7])
def create_Bayesian_analysis_charts_simplified(model, train_dataset, validation_dataset, tr_loss, val_loss, KL_loss, folder_images, epoch_i=None): # Configurations of the plots alpha_points = 0.2 color_points_train = "dark navy blue" color_points_val = "amber" color_train_loss = "cobalt blue" color_val_loss = "blood" color_truth = "k" color_mean = "b" color_most_likey = "y" ################################ Divide in plots ############################## gl.init_figure() ax1 = gl.subplot2grid((6, 3), (0, 0), rowspan=3, colspan=1) ax2 = gl.subplot2grid((6, 3), (3, 0), rowspan=3, colspan=1, sharex=ax1, sharey=ax1) ax3 = gl.subplot2grid((6, 3), (0, 1), rowspan=2, colspan=1) ax4 = gl.subplot2grid((6, 3), (2, 1), rowspan=2, colspan=1, sharex=ax3) ax5 = gl.subplot2grid((6, 3), (4, 1), rowspan=2, colspan=1, sharex=ax3) ax6 = gl.subplot2grid((6, 3), (0, 2), rowspan=3, colspan=1) ax7 = gl.subplot2grid((6, 3), (3, 2), rowspan=3, colspan=1, sharex=ax6) ####### ax1, ax2: Get confusion matrices ########## labels_classes, confusion = model.get_confusion_matrix(train_dataset) plot_confusion_matrix(confusion, labels_classes, ax1) labels_classes, confusion = model.get_confusion_matrix(validation_dataset) plot_confusion_matrix(confusion, labels_classes, ax2) ############## ax3 ax4 ax5: Loss Evolution !! ###################### ## ax3: Evolutoin of the data loss gl.plot([], tr_loss, ax=ax3, lw=3, labels=["Losses", "", "Data loss (MSE)"], legend=["train"], color=color_train_loss) gl.plot([], val_loss, ax=ax3, lw=3, legend=["validation"], color=color_val_loss, AxesStyle="Normal - No xaxis") ## ax4: The evolution of the KL loss gl.plot([], KL_loss, ax=ax4, lw=3, labels=["", "", "KL loss"], legend=["Bayesian Weights"], AxesStyle="Normal - No xaxis", color="k") ## ax5: Evolutoin of the total loss gl.plot([], tr_loss, ax=ax5, lw=3, labels=["", "epoch", "Total Loss (Bayes)"], legend=["train"], color=color_train_loss) gl.plot([], val_loss, ax=ax5, lw=3, legend=["validation"], color=color_val_loss) ############## ax6 ax7: Variational Weights !! ###################### create_plot_variational_weights(model, ax6, ax7) gl.set_zoom(ax=ax6, ylim=[-0.1, 10]) gl.set_zoom(ax=ax7, xlim=[-2.5, 2.5], ylim=[-0.1, 0.5]) # Set final properties and save figure gl.set_fontSizes(ax=[ax1, ax2, ax3, ax4, ax5, ax6, ax7], title=20, xlabel=20, ylabel=20, legend=10, xticks=12, yticks=12) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.30, hspace=0.10) if (type(epoch_i) == type(None)): gl.savefig(folder_images + 'Training_Example_Data_Bayesian.png', dpi=100, sizeInches=[20, 10]) else: gl.savefig(folder_images + '%i.png' % epoch_i, dpi=100, sizeInches=[20, 10], close=True, bbox_inches="tight")
ls="--") gl.plot(x_values, start_acc_validation, ax=ax3, legend=["start span DEV"], color=val_color) gl.plot(x_values, end_acc_validation, legend=["end span DEV"], color=val_color, ls="--") gl.set_fontSizes(ax=[ax1, ax2, ax3], title=20, xlabel=18, ylabel=18, legend=15, xticks=14, yticks=14) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.20, hspace=0.10) gl.format_yaxis(ax=ax3, Nticks=10) gl.format_xaxis(ax=ax3, Nticks=len(em_train)) gl.savefig(folder_images + images_prefix + "Accuracies_epoch.png", dpi=100, sizeInches=[20, 5], close=False,
l = float(L)/N if (N == 2): nu_values = nu else: # nu = np.array([-0.5*m*g/np.sqrt(-1 +4*l*l/(h*h)) , -m*g ]) nu_values = scipy.optimize.fsolve(get_error, nu) # nu_values = nu nu_values[0] = -np.abs(nu_values[0]) ax1 = print_chain(nu_values) print (" For N=%i: nu_guess = "%N,nu,", nu_final: ", nu_values) print("Costate vector: ",get_costate_value(nu_values,0) ) gl.set_fontSizes(ax = [ax1], title = 20, xlabel = 20, ylabel = 20, legend = 15, xticks = 12, yticks = 12) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.30, hspace=0.10) gl.savefig("P2_2.png", dpi = 100, sizeInches = [12, 7], close = False, bbox_inches = "tight") """ ################################ QUESTION 5 ############################## """ print (" ----------------- QUESTION 5 ----------------") def get_half_angle(i, nu_z): theta_i = np.arctan((m*g*(N/2-0.5-i) )/nu_z) return theta_i
def create_Bayesian_analysis_charts(model, X_data_tr, X_data_val, tr_data_loss, val_data_loss, KL_loss_tr, KL_loss_val, final_loss_tr, final_loss_val, folder_images, epoch_i=None): # Configurations of the plots alpha_points = 0.2 color_points_train = "dark navy blue" color_points_val = "amber" color_truth = "k" color_mean = "b" color_most_likey = "y" ################################ Divide in plots ############################## gl.init_figure() ax1 = gl.subplot2grid((6, 3), (0, 0), rowspan=3, colspan=1) ax2 = gl.subplot2grid((6, 3), (3, 0), rowspan=3, colspan=1, sharex=ax1) ax3 = gl.subplot2grid((6, 3), (0, 1), rowspan=2, colspan=1) ax4 = gl.subplot2grid((6, 3), (2, 1), rowspan=2, colspan=1, sharex=ax3) ax5 = gl.subplot2grid((6, 3), (4, 1), rowspan=2, colspan=1, sharex=ax3) ax6 = gl.subplot2grid((6, 3), (0, 2), rowspan=3, colspan=1) ax7 = gl.subplot2grid((6, 3), (3, 2), rowspan=3, colspan=1, sharex=ax6) """ ############################# Data computation ####################### """ Xtrain_sample_cpu, Xtrain_reconstruction,Xtrain_reconstruction_samples = \ compute_reconstruction_data( model,X_data_tr, Nsamples = 100, sample_index = 2) plot_reconstruction_data(Xtrain_sample_cpu, Xtrain_reconstruction, Xtrain_reconstruction_samples, ax1, ax2) """ ############## ax3 ax4 ax5: Loss Evolution !! ###################### """ plot_losses_evolution_epoch(tr_data_loss, val_data_loss, KL_loss_tr, KL_loss_val, final_loss_tr, final_loss_val, ax3, ax4, ax5) """ ############## ax6 ax7: Projecitons Weights !! ###################### """ plot_projections_VAE(model, X_data_tr, ax6) ## Plot in chart 7 the acceptable mu = 2sigma -> sigma = |mu|/2sigma # gl.set_zoom (ax = ax6, ylim = [-0.1,10]) # gl.set_zoom (ax = ax7, xlim = [-2.5, 2.5], ylim = [-0.05, np.exp(model.cf_a.input_layer_prior["log_sigma2"])*(1 + 0.15)]) # gl.set_zoom (ax = ax7, xlim = [-2.5, 2.5], ylim = [-0.1,2]) # Set final properties and save figure gl.set_fontSizes(ax=[ax1, ax2, ax3, ax4, ax5, ax6, ax7], title=20, xlabel=20, ylabel=20, legend=10, xticks=12, yticks=12) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.30, hspace=0.10) if (type(epoch_i) == type(None)): gl.savefig(folder_images + "../" + 'Final_values_regression_1D_' + str(model.cf_a.eta_KL) + '.png', dpi=100, sizeInches=[20, 10]) else: gl.savefig(folder_images + '%i.png' % epoch_i, dpi=100, sizeInches=[20, 10], close=True, bbox_inches="tight")
# F1 values val x_values = 1 + np.array(range(len(f1_train))) gl.plot(x_values, f1_validation, ax=ax2, labels=["F1 score DEV", "epoch", "F1 score"], color=tr_color, legend=[List_analyses[an_i] + ": " + value]) # start span and end span values gl.set_fontSizes(ax=[ax1, ax2], title=18, xlabel=15, ylabel=15, legend=12, xticks=12, yticks=12) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.20, hspace=0.10) gl.savefig(folder_images + images_prefix + "CV_baseline_Bayesian" + List_analyses[an_i].replace(" ", "_") + ".png", dpi=100, sizeInches=[14, 5], close=False,
def create_image_weights_epoch(model, video_fotograms_folder2, epoch_i): """ Creates the image of the training and validation accuracy """ N_Bayesian_layers = len(model.VBmodels) N_Normal_layers = len(model.LinearModels) # Compute the number of squares we will need: # 1 x linear layers, 2 x LSTMS gl.init_figure(); cmap = cm.get_cmap('coolwarm', 30) all_axes = [] for i in range(N_Bayesian_layers): layer = model.VBmodels[i] # if (layer.type_layer == "linear"): if ("linear" in type(layer).__name__.lower()): ax = gl.subplot2grid((1,N_Bayesian_layers + N_Normal_layers), (0,i), rowspan=1, colspan=1) weights = layer.weight.detach().cpu().numpy() biases = layer.bias.detach().cpu().numpy().reshape(-1,1) neurons = np.concatenate((weights, biases), axis = 1) cax = ax.imshow(neurons, interpolation="nearest", cmap=cmap, vmin=-2, vmax=2) all_axes.append(ax) else: ax = gl.subplot2grid((1,N_Bayesian_layers + N_Normal_layers), (0,i), rowspan=1, colspan=1) weights_ih = layer.weight_ih.detach().cpu().numpy() biases_ih = layer.bias_ih.detach().cpu().numpy().reshape(-1,1) weights_hh = layer.weight_hh.detach().cpu().numpy() biases_hh = layer.bias_hh.detach().cpu().numpy().reshape(-1,1) weights = np.concatenate((weights_ih,weights_hh),axis = 1) biases = np.concatenate((biases_ih,biases_hh),axis = 1) neurons = np.concatenate((weights, biases), axis = 1) cax = ax.imshow(neurons, interpolation="nearest", cmap=cmap, vmin=-2, vmax=2) all_axes.append(ax) for i in range(N_Normal_layers): layer = model.LinearModels[i] if ("linear" in type(layer).__name__.lower()): ax = gl.subplot2grid((1,N_Bayesian_layers + N_Normal_layers), (0,N_Bayesian_layers +i), rowspan=1, colspan=1) weights = layer.weight.detach().cpu().numpy() biases = layer.bias.detach().cpu().numpy().reshape(-1,1) neurons = np.concatenate((weights, biases), axis = 1) cax = ax.imshow(neurons, interpolation="nearest", cmap=cmap, vmin=-2, vmax=2) all_axes.append(ax) else: ax = gl.subplot2grid((1,N_Bayesian_layers + N_Normal_layers), (0,N_Bayesian_layers +i), rowspan=1, colspan=1) weights_ih = layer.weight_ih.detach().cpu().numpy() biases_ih = layer.bias_ih.detach().cpu().numpy().reshape(-1,1) weights_hh = layer.weight_hh.detach().cpu().numpy() biases_hh = layer.bias_hh.detach().cpu().numpy().reshape(-1,1) weights = np.concatenate((weights_ih,weights_hh),axis = 1) biases = np.concatenate((biases_ih,biases_hh),axis = 1) neurons = np.concatenate((weights, biases), axis = 1) cax = ax.imshow(neurons, interpolation="nearest", cmap=cmap, vmin=-2, vmax=2) all_axes.append(ax) # plt.xticks(range(data_df_train.shape[1]), data_df_train.columns, rotation='vertical') # plt.yticks(range(data_df_train.shape[1]), data_df_train.columns, rotation='horizontal') plt.colorbar(cax) # plt.colorbar(cax2) # ax1.set_xticks(data_df_train.columns) # , rotation='vertical' # ax1.grid(True) plt.title('Weights ') # labels=[str(x) for x in range(Nshow )] # ax1.set_xticklabels(labels,fontsize=20) # ax1.set_yticklabels(labels,fontsize=20) # Add colorbar, make sure to specify tick locations to match desired ticklabels plt.show() gl.set_fontSizes(ax = [all_axes], title = 20, xlabel = 20, ylabel = 20, legend = 20, xticks = 12, yticks = 12) # Set final properties and save figure gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.30, hspace=0.30) gl.savefig(video_fotograms_folder2 +'%i.png'%epoch_i, dpi = 100, sizeInches = [14, 10], close = True, bbox_inches = None)
for i in range(300): Xtrain_reconstruction = myVAE.forward( Xtrain_sample)[0].detach().cpu().numpy() gl.plot(x, Xtrain_reconstruction, ax=ax2, legend=[], color="b", alpha=alpha, labels=[title, "", r"Rate"], AxesStyle="Normal - No xaxis") gl.set_fontSizes(ax=[ax1], title=20, xlabel=20, ylabel=20, legend=10, xticks=10, yticks=10) image_name = "reconstrunction" gl.savefig(folder_images + image_name, dpi=100, sizeInches=[20, 7]) """ ###################### SAVE MODEL #################### """ myVAE.save(folder_model + "model_parameters_epoch:%i.pk" % i) """ ##################### STATIC PLOTS ###################### """ if (create_video_training): # pf.create_video_from_images(video_fotograms_folder_training,
def create_Bayesian_analysis_charts(model, X_data_tr, Y_data_tr, X_data_val, Y_data_val, tr_loss, val_loss, KL_loss,final_loss_tr,final_loss_val, xgrid_real_func, ygrid_real_func, folder_images, epoch_i = None): # Configurations of the plots alpha_points = 0.2 color_points_train = "dark navy blue" color_points_val = "amber" color_train_loss = "cobalt blue" color_val_loss = "blood" color_truth = "k" color_mean = "b" color_most_likey = "y" ############################# Data computation ####################### if(type(X_data_tr) == type([])): pass else: if (X_data_tr.shape[1] == 1): # Regression Example x_grid, all_y_grid,most_likely_ygrid = compute_regression_1D_data( model,X_data_tr,X_data_val, Nsamples = 100) elif(X_data_tr.shape[1] == 2): # Classification Example xx,yy , all_y_grid,most_likely_ygrid = compute_classification_2D_data( model,X_data_tr,X_data_val, Nsamples = 100) else: # RNN x_grid, all_y_grid,most_likely_ygrid = compute_RNN_1D_data( model,X_data_tr,X_data_val, Nsamples = 100) ################################ Divide in plots ############################## gl.init_figure(); ax1 = gl.subplot2grid((6,3), (0,0), rowspan=3, colspan=1) ax2 = gl.subplot2grid((6,3), (3,0), rowspan=3, colspan=1, sharex = ax1, sharey = ax1) ax3 = gl.subplot2grid((6,3), (0,1), rowspan=2, colspan=1) ax4 = gl.subplot2grid((6,3), (2,1), rowspan=2, colspan=1, sharex = ax3) ax5 = gl.subplot2grid((6,3), (4,1), rowspan=2, colspan=1, sharex = ax3) ax6 = gl.subplot2grid((6,3), (0,2), rowspan=3, colspan=1) ax7 = gl.subplot2grid((6,3), (3,2), rowspan=3, colspan=1, sharex = ax6) if(type(X_data_tr) == type([])): Xtrain = [torch.tensor(X_data_tr[i],device=model.cf_a.device, dtype=model.cf_a.dtype) for i in range(len(X_data_tr))] Ytrain = torch.tensor(Y_data_tr,device=model.cf_a.device, dtype=torch.int64) Xval = [torch.tensor(X_data_val[i],device=model.cf_a.device, dtype=model.cf_a.dtype) for i in range(len(X_data_val))] Yval = torch.tensor(Y_data_val,device=model.cf_a.device, dtype=torch.int64) confusion = model.get_confusion_matrix(Xtrain, Ytrain) plot_confusion_matrix(confusion,model.languages, ax1 ) confusion = model.get_confusion_matrix(Xval, Yval) plot_confusion_matrix(confusion,model.languages, ax2 ) else: if (X_data_tr.shape[1] == 1): # Regression Example plot_data_regression_1d_2axes(X_data_tr, Y_data_tr, xgrid_real_func, ygrid_real_func, X_data_val, Y_data_val, x_grid,all_y_grid, most_likely_ygrid, alpha_points, color_points_train, color_points_val, color_most_likey,color_mean,color_truth, ax1,ax2) elif(X_data_tr.shape[1] == 2): # Classification Example plot_data_classification_2d_2axes(X_data_tr, Y_data_tr, xgrid_real_func, ygrid_real_func, X_data_val, Y_data_val, xx,yy,all_y_grid, most_likely_ygrid, alpha_points, color_points_train, color_points_val, color_most_likey,color_mean, color_truth, ax1,ax2) else: # RNN example plot_data_RNN_1d_2axes(X_data_tr, Y_data_tr, xgrid_real_func, ygrid_real_func, X_data_val, Y_data_val, x_grid,all_y_grid, most_likely_ygrid, alpha_points, color_points_train, color_points_val, color_most_likey,color_mean,color_truth, ax1,ax2) # gl.fill_between (x_grid, [mean_samples_grid + 2*std_samples_grid, mean_samples_grid - 2*std_samples_grid] # , ax = ax2, alpha = 0.10, color = "b", legend = ["Mean realizaions"]) ## ax2: The uncertainty of the prediction !! # gl.plot (x_grid, std_samples_grid, ax = ax2, labels = ["Std (%i)"%(Nsamples),"X","f(X)"], legend = [" std predictions"], fill = 1, alpha = 0.3) ############## ax3 ax4 ax5: Loss Evolution !! ###################### ## ax3: Evolutoin of the data loss gl.plot([], tr_loss, ax = ax3, lw = 3, labels = ["Losses", "","Data loss"], legend = ["train"], color = color_train_loss) gl.plot([], val_loss,ax = ax3, lw = 3, legend = ["validation"], color = color_val_loss, AxesStyle = "Normal - No xaxis") ## ax4: The evolution of the KL loss gl.plot([], KL_loss, ax = ax4, lw = 3, labels = ["", "","KL loss"], legend = ["Bayesian Weights"], AxesStyle = "Normal - No xaxis", color = "k") ## ax5: Evolutoin of the total loss gl.plot([], final_loss_tr, ax = ax5, lw = 3, labels = ["", "epoch","Total Loss (Bayes)"], legend = ["train"], color = color_train_loss) gl.plot([], final_loss_val,ax = ax5, lw = 3, legend = ["validation"], color = color_val_loss) ############## ax6 ax7: Variational Weights !! ###################### create_plot_variational_weights(model,ax6,ax7) ## Plot in chart 7 the acceptable mu = 2sigma -> sigma = |mu|/2sigma mu_grid = np.linspace(-3,3,100) y_grid = np.abs(mu_grid)/2 gl.fill_between(mu_grid, 10*np.ones(mu_grid.size), y_grid, alpha = 0.2, color = "r", ax = ax7, legend = ["95% non-significant"]) gl.set_zoom (ax = ax6, ylim = [-0.1,10]) gl.set_zoom (ax = ax7, xlim = [-2.5, 2.5], ylim = [-0.05, np.exp(model.cf_a.input_layer_prior["log_sigma2"])*(1 + 0.15)]) # gl.set_zoom (ax = ax7, xlim = [-2.5, 2.5], ylim = [-0.1,2]) # Set final properties and save figure gl.set_fontSizes(ax = [ax1,ax2,ax3,ax4,ax5,ax6,ax7], title = 20, xlabel = 20, ylabel = 20, legend = 10, xticks = 12, yticks = 12) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.30, hspace=0.10) if (type(epoch_i) == type(None)): gl.savefig(folder_images +"../"+'Final_values_regression_1D_' +str(model.cf_a.eta_KL) +'.png', dpi = 100, sizeInches = [20, 10]) else: gl.savefig(folder_images +'%i.png'%epoch_i, dpi = 100, sizeInches = [20, 10], close = True, bbox_inches = "tight")
def create_Bayesian_analysis_charts_simplified(model, train_dataset, validation_dataset, tr_loss, val_loss, KL_loss, folder_images, epoch_i = None): # Configurations of the plots alpha_points = 0.2 color_points_train = "dark navy blue" color_points_val = "amber" color_train_loss = "cobalt blue" color_val_loss = "blood" color_truth = "k" color_mean = "b" color_most_likey = "y" ################################ Divide in plots ############################## gl.init_figure(); ax1 = gl.subplot2grid((6,3), (0,0), rowspan=3, colspan=1) ax2 = gl.subplot2grid((6,3), (3,0), rowspan=3, colspan=1, sharex = ax1, sharey = ax1) ax3 = gl.subplot2grid((6,3), (0,1), rowspan=2, colspan=1) ax4 = gl.subplot2grid((6,3), (2,1), rowspan=2, colspan=1, sharex = ax3) ax5 = gl.subplot2grid((6,3), (4,1), rowspan=2, colspan=1, sharex = ax3) ax6 = gl.subplot2grid((6,3), (0,2), rowspan=3, colspan=1) ax7 = gl.subplot2grid((6,3), (3,2), rowspan=3, colspan=1, sharex = ax6) ####### ax1, ax2: Get confusion matrices ########## labels_classes, confusion = model.get_confusion_matrix(train_dataset) plot_confusion_matrix(confusion,labels_classes, ax1 ) labels_classes, confusion = model.get_confusion_matrix(validation_dataset) plot_confusion_matrix(confusion,labels_classes, ax2 ) ############## ax3 ax4 ax5: Loss Evolution !! ###################### ## ax3: Evolutoin of the data loss gl.plot([], tr_loss, ax = ax3, lw = 3, labels = ["Losses", "","Data loss (MSE)"], legend = ["train"], color = color_train_loss) gl.plot([], val_loss,ax = ax3, lw = 3, legend = ["validation"], color = color_val_loss, AxesStyle = "Normal - No xaxis") ## ax4: The evolution of the KL loss gl.plot([], KL_loss, ax = ax4, lw = 3, labels = ["", "","KL loss"], legend = ["Bayesian Weights"], AxesStyle = "Normal - No xaxis", color = "k") ## ax5: Evolutoin of the total loss gl.plot([], tr_loss, ax = ax5, lw = 3, labels = ["", "epoch","Total Loss (Bayes)"], legend = ["train"], color = color_train_loss) gl.plot([], val_loss,ax = ax5, lw = 3, legend = ["validation"], color = color_val_loss) ############## ax6 ax7: Variational Weights !! ###################### create_plot_variational_weights(model,ax6,ax7) gl.set_zoom (ax = ax6, ylim = [-0.1,10]) gl.set_zoom (ax = ax7, xlim = [-2.5, 2.5], ylim = [-0.1,0.5]) # Set final properties and save figure gl.set_fontSizes(ax = [ax1,ax2,ax3,ax4,ax5,ax6,ax7], title = 20, xlabel = 20, ylabel = 20, legend = 10, xticks = 12, yticks = 12) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.30, hspace=0.10) if (type(epoch_i) == type(None)): gl.savefig(folder_images +'Training_Example_Data_Bayesian.png', dpi = 100, sizeInches = [20, 10]) else: gl.savefig(folder_images +'%i.png'%epoch_i, dpi = 100, sizeInches = [20, 10], close = True, bbox_inches = "tight")
ax1 = gl.subplot2grid((5,1), (0,0), rowspan=2, colspan=1) gl.tradingBarChart(Cartera.get_timeData(symbolIDs[symbol_ID_indx1],periods[0]), ax = ax1, dataTransform = dataTransform, AxesStyle = "Normal - No xaxis", labels = [title,"",symbolIDs[symbol_ID_indx1] +"(" +str(periods[0])+")"]) ax2 = gl.subplot2grid((5,1), (2,0), rowspan=1, colspan=1, sharex = ax1) gl.stem(dates, volume, ax = ax2, dataTransform = dataTransform, AxesStyle = "Normal - No xaxis - Ny:4", labels = ["","",symbolIDs[0] +"("+ str(periods[0])+ "M)"], legend = [ "Volume"]) ax3 = gl.subplot2grid((5,1), (3,0), rowspan=2, colspan=1, sharex = ax1) gl.stem(dates, ret1, ax = ax3, dataTransform = dataTransform, AxesStyle = "Normal", labels = ["","",symbolIDs[0] +"("+ str(periods[0])+ "M)"], legend = ["Return"]) # gl.set_fontSizes(ax = [ax1,ax2,ax3], title = 20, xlabel = 20, ylabel = 20, legend = 20, xticks = 10, yticks = 10) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.01, hspace=0.01) gl.savefig(folder_images +'PriceAndReturns1Symbol_EM.png', dpi = 100, sizeInches = [22, 12]) ########################################################################## ################# PREPROCESS DATA ###################################### ########################################################################## ## Set GAP return as NAN if (remove_gap_return): """ We usually would like to remove the return of gaps if we are dealing with intraday data since they are ouliers for this distribution,
labels=["", "", symbolIDs[0] + "(" + str(periods[0]) + "M)"], legend=["Volume"]) ax3 = gl.subplot2grid((5, 1), (3, 0), rowspan=2, colspan=1, sharex=ax1) gl.stem(dates, ret1, ax=ax3, dataTransform=dataTransform, AxesStyle="Normal", labels=["", "", symbolIDs[0] + "(" + str(periods[0]) + "M)"], legend=["Return"]) # gl.set_fontSizes(ax=[ax1, ax2, ax3], title=20, xlabel=20, ylabel=20, legend=20, xticks=10, yticks=10) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.01, hspace=0.01) gl.savefig(folder_images + 'PriceAndReturns1Symbol_EM.png', dpi=100, sizeInches=[22, 12])
# Create the signal X = mean_function(tgrid, f1=1, f2=5, a1=0.4, a2=0.1, phi2=2 * np.pi / 7, m=0.1) if (plot_mean_signal and plot_flag): ## Plot the orginal function gl.scatter(tgrid, X, lw=1, alpha=0.9, color="k", nf=1, labels=["The true determinist signal mu(t)", "t", "mu(t)"]) gl.plot(tgrid, X, lw=2, color="k", ls="--", legend=["True signal"]) gl.set_fontSizes(title=20, xlabel=20, ylabel=20, legend=20, xticks=20, yticks=20) gl.savefig(folder_images + 'GP_mean.png', dpi=100, sizeInches=[2 * 8, 2 * 2]) ########################################################################### ############### Generate the structural noise ############################# ########################################################################### """ Now we generate the stocastic process that we add to X(t), generating noisy signal Y(t) = X(t) + e(t) Where we will assume e(t) is Gaussian with mean 0 e(t) \sim N(0,\sigma_t) So we have a Gaussian process, since each set of samples forms a jointly gaussian distribution. The relation between the noises will be given by the covariance matrix C. This will tell how big the noises are and how they relate
def update_plotting_chart(self, extraInfo = None): """ This function aims to udate the values in the chart using the new information contained in the "information" input structure. This function is called by the Task Scheduler, """ self.plot_lock.acquire() desired_value = self.Monitor.desired_value # The desired value of the sensor range_warning = self.Monitor.range_warning # The range which if crosses we send email range_stop = self.Monitor.range_stop # The range which if crosses we stop # print ("Gonna plot") self.data_lock.acquire() data, time = np.array(self.data_buffer), np.array(self.time_buffer) self.data_lock.release() if (type(data) == type(None)): self.plot_lock.release() return True ; ## For the task manager ? if (type(time) == type(None)): self.plot_lock.release() return True ; ## For the task manager ? ## Select the start and end index to plot s_indx = max([data.size - self.show_window, 0]) e_indx = data.size -1 if(self.first_plot_flag): ## Remove the text box self.initial_text_data.set_visible(False) if(len(data) < 2): # Plot 2 data minimum self.plot_lock.release() return True ; ## For the task manager ? self.first_plot_flag = False ##------------------------------------------ #### Warning bands ax_aux, plots_data_upper_warning_band = gl.plot([time[s_indx],time[e_indx]], [desired_value + range_warning, desired_value + range_warning], ax = self.data_axes, color = "y", lw = 3, ls="--", return_drawing_elements = True, legend = ["Warning email"], loc = "upper right"); #, legend = ["Warning area"] ax_aux, plots_data_lower_warning_band = gl.plot([time[s_indx],time[e_indx]], [desired_value - range_warning, desired_value - range_warning], ax = self.data_axes, color = "y", lw = 3, ls="--", return_drawing_elements = True); #### Error bands ax_aux, plots_data_upper_error_band = gl.plot([time[s_indx],time[e_indx]], [desired_value + range_stop, desired_value + range_stop], ax = self.data_axes, color = "r", lw = 3, ls="--", return_drawing_elements = True, legend = ["Stop"], loc = "upper right"); #, legend = ["Warning area"] ax_aux, plots_data_lower_error_band = gl.plot([time[s_indx],time[e_indx]], [desired_value - range_stop, desired_value - range_stop], ax = self.data_axes, color = "r", lw = 3, ls="--", return_drawing_elements = True); ax_aux, plot_time_series = gl.plot(time[s_indx:e_indx+1], data[s_indx:e_indx+1], ax = self.data_axes, labels = ["Cleaning Procedure: " + self.cleaning_ID, self.time_now.strftime("%B %d, %Y"), "PH"], color = "k", xaxis_mode = "intraday", return_drawing_elements = True, loc = "upper right"); gl.set_fontSizes(ax = self.data_axes, title = 25, xlabel = 20, ylabel = 20, legend = 15, xticks = 15, yticks = 15) ## Save the elements so that we can modify them later self.plots_data = [plot_time_series[0], plots_data_upper_warning_band[0], plots_data_lower_warning_band[0], plots_data_upper_error_band[0], plots_data_lower_error_band[0]] else: # print self.plots_data self.plots_data[0].set_xdata(time[s_indx:e_indx+1]) self.plots_data[0].set_ydata(data[s_indx:e_indx+1]) ## Warning bands self.plots_data[1].set_xdata([time[s_indx],time[e_indx]]) self.plots_data[1].set_ydata([desired_value + range_warning, desired_value + range_warning]) self.plots_data[2].set_xdata([time[s_indx],time[e_indx]]) self.plots_data[2].set_ydata([desired_value - range_warning, desired_value - range_warning]) ## Error bands self.plots_data[3].set_xdata([time[s_indx],time[e_indx]]) self.plots_data[3].set_ydata([desired_value + range_stop, desired_value + range_stop]) self.plots_data[4].set_xdata([time[s_indx],time[e_indx]]) self.plots_data[4].set_ydata([desired_value - range_stop, desired_value - range_stop]) # gl.set_xlim(ax = self.data_axes, X = time[s_indx:e_indx+1], xmin = np.min(time[s_indx:e_indx+1]), xmax = np.max(time[s_indx:e_indx+1])) # gl.set_ylim(ax = self.data_axes, Y = data[s_indx:e_indx+1], ymin =np.min(data[s_indx:e_indx+1]),ymax = np.max(data[s_indx:e_indx+1])) # gl.set_zoom(X = time[s_indx:e_indx+1],Y = data[s_indx:e_indx+1],xlimPad = [0.2,0.2] ,ylimPad = [0.1, 0.1]) # gl.set_zoom(X = time[s_indx:e_indx+1],Y = data[s_indx:e_indx+1],xlimPad = [0.2,0.2] ,ylimPad = [0.1, 0.1]) # gl.set_zoom(X = time[s_indx:e_indx+1],Y = data[s_indx:e_indx+1],xlimPad = [0.2,0.2] ,ylim = [0, 14]) gl.set_zoom(X = time[s_indx:e_indx+1],Y = data[s_indx:e_indx+1],xlimPad = [0.2,0.2] ,ylim = [0, 10]) pass # self.data_axes.update() # self.data_axes.draw(self.plots_data[0]) plt.draw() # l.set_ydata(ydata) # ax.set_ylim(np.min(ydata), np.max(ydata)) # plt.draw() # self.fig.canvas.draw() # #### RETOQUES ######## # if (len(self.data_buffer) > 1): # gl.set_zoom(X = time[s_indx:e_indx+1],Y = data[s_indx:e_indx+1],xlimPad = [0.2,0.2] ,ylimPad = [0.1, 0.1]) # # if (update_data.index == 1000): # rt.stop() # information.serial.close() self.check_monitoring(data[s_indx:e_indx+1]) self.plot_lock.release() return True ; ## For the task manager ?
legend=["95% non-significant"]) gl.set_zoom(ax=ax2, xlim=[-2.5, 2.5], ylim=[-0.05, model.linear1.prior.sigma1 * (1 + 0.30)]) eta_KL = eta_values[i] ax1.set_title("Model estimations for $\zeta = " + str(eta_KL) + "$") ax2.set_title("Variational Weights for $\zeta = " + str(eta_KL) + "$") # gl.set_zoom (ax = ax7, xlim = [-2.5, 2.5], ylim = [-0.1,2]) # Set final properties and save figure gl.set_fontSizes(ax=all_axes, title=14, xlabel=16, ylabel=16, legend=10, xticks=10, yticks=10) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.25, hspace=0.25) gl.savefig( "../pics/Pytorch/BasicExamples_Bayesian/MLP/Introudction_example.png", dpi=100, sizeInches=[14, 9],
ax2 = gl.subplot2grid((4,1), (1,0), rowspan=1, colspan=1, sharex = ax1) gl.tradingBarChart(Cartera.get_timeData(symbolIDs[symbol_ID_indx2],periods[0]), ax = ax2, dataTransform = dataTransform, AxesStyle = "Normal - No xaxis", color = "k", labels = ["","",symbolIDs[symbol_ID_indx2] +"(" +str(periods[0])+"M)"]) ax3 = gl.subplot2grid((4,1), (2,0), rowspan=1, colspan=1) gl.stem(dates, ret1, ax = ax3, dataTransform = dataTransform, AxesStyle = "Normal", labels = ["","",symbolIDs[symbol_ID_indx1] +"("+ str(periods[0])+ "M)"], legend = ["Return"]) ax4 = gl.subplot2grid((4,1), (3,0), rowspan=1, colspan=1, sharex = ax1, sharey = ax3) gl.stem(dates, ret2, ax = ax4, dataTransform = dataTransform, AxesStyle = "Normal", labels = ["","",symbolIDs[symbol_ID_indx2] +"("+ str(periods[0])+ "M)"], legend = ["Return"]) # gl.set_fontSizes(ax = [ax1,ax2,ax3,ax4], title = 20, xlabel = 20, ylabel = 20, legend = 20, xticks = 10, yticks = 10) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.01, hspace=0.01) gl.savefig(folder_images +'PriceAndReturns2Symbol_EM.png', dpi = 100, sizeInches = [22, 12]) ########################################################################## ################# PREPROCESS DATA ###################################### ########################################################################## ## Set GAP return as NAN remove_gap_return = 1 if (remove_gap_return): """ We usually would like to remove the return of gaps if we are dealing
Network.save_variables() loss_tr, accuracy_tr = Network.get_loss_accuracy(X_data_tr, Y_data_tr) print ("The final loss:" , loss_tr) print ("The final variables:" , W_values,b_values,s_value) ## Plot the loss function against the parameters !! ## Get the surface for the loss ####### PLOT THE EVOLUTION OF RMSE AND PARAMETERS ############ gl.init_figure() ax1 = gl.scatter(X_data_tr, Y_data_tr, lw = 3,legend = ["tr points"], labels = ["Data", "X","Y"]) ax2 = gl.scatter(X_data_val, Y_data_val, lw = 3,legend = ["val points"]) gl.set_fontSizes(ax = [ax1,ax2], title = 20, xlabel = 20, ylabel = 20, legend = 20, xticks = 12, yticks = 12) x_grid = np.linspace(np.min([X_data_tr]) -1, np.max([X_data_val]) +1, 100) y_grid = x_grid * W_values + b_values gl.plot (x_grid, y_grid, legend = ["training line"]) gl.savefig(folder_images +'Training_Example_Data.png', dpi = 100, sizeInches = [14, 4]) ####### PLOT THE EVOLUTION OF RMSE AND PARAMETERS ############ gl.set_subplots(2,1) ax1 = gl.plot([], tr_loss, nf = 1, lw = 3, labels = ["RMSE loss and parameters. Learning rate: %.3f"%train_config.lr, "","RMSE"], legend = ["train"]) gl.plot([], val_loss, lw = 3, legend = ["validation"]) ax2 = gl.plot([], W_list, nf = 1, lw = 3, sharex = ax1, labels = ["", "","Parameters"], legend = ["W"], color ="b")
lw=28, AxesStyle="Normal - No xaxis - No yaxis", loc="center left") gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.2, hspace=0.01) image_name = "EM_all_subjects_trained_togehter" # gl.set_fontSizes(ax=ax_i, title=20, xlabel=20, ylabel=20, legend=35, xticks=25, yticks=10) gl.set_fontSizes(ax=ax_ii, title=20, xlabel=20, ylabel=20, legend=30, xticks=20, yticks=10) gl.set_zoom(xlim=[10, 10.50]) gl.savefig(folder_images + image_name, dpi=100, sizeInches=[30, 12]) ## Save to disk the clusters
# Create the t- values tgrid = np.linspace(t0,tf, int(float(tf-t0)/delta_t)) tgrid = tgrid.reshape(tgrid.size,1) N = tgrid.size # Create the signal X = mean_function(tgrid, f1 = 1, f2 = 5, a1 = 0.4, a2 = 0.1, phi2 = 2*np.pi/7, m = 0.1 ) if (plot_mean_signal and plot_flag): ## Plot the orginal function gl.scatter(tgrid,X, lw = 1, alpha = 0.9, color = "k", nf = 1, labels = ["The true determinist signal mu(t)", "t", "mu(t)" ]) gl.plot(tgrid,X, lw = 2, color = "k", ls = "--", legend = ["True signal"]) gl.set_fontSizes( title = 20, xlabel = 20, ylabel = 20, legend = 20, xticks = 20, yticks = 20) gl.savefig(folder_images +'GP_mean.png', dpi = 100, sizeInches = [2*8, 2*2]) ########################################################################### ############### Generate the structural noise ############################# ########################################################################### """ Now we generate the stocastic process that we add to X(t), generating noisy signal Y(t) = X(t) + e(t) Where we will assume e(t) is Gaussian with mean 0 e(t) \sim N(0,\sigma_t) So we have a Gaussian process, since each set of samples forms a jointly gaussian distribution. The relation between the noises will be given by the covariance matrix C. This will tell how big the noises are and how they relate to each other.
gl.init_figure(); ax1 = gl.subplot2grid((1,4), (0,0), rowspan=1, colspan=1) ax2 = gl.subplot2grid((1,4), (0,1), rowspan=1, colspan=1, sharex = ax1) ax3 = gl.subplot2grid((1,4), (0,2), rowspan=1, colspan=1, sharex = ax1) ax4 = gl.subplot2grid((1,4), (0,3), rowspan=1, colspan=1, sharex = ax1) ax1 = gl.plot([], f_values, ax = ax1, lw = 3, labels = ["Objective function", "iterations","f(X)"], legend = ["f(x)"], AxesStyle = "Normal ", color = "k") ax2 = gl.plot([], t_values, ax = ax2, lw = 3, labels = ["Optimal learning step", "iterations","t"], legend = ["t"], AxesStyle = "Normal ", color = "k") ax3 = gl.plot([], x1_values, ax = ax3, lw = 3, labels = ["Input variables", "iterations","Variables"], legend = ["x1"], AxesStyle = "Normal ", color = "k") ax3 = gl.plot([], x2_values, ax = ax3, lw = 3, legend = ["x2"], AxesStyle = "Normal ", color = "b") ax4 = gl.plot([], m1_values, ax = ax4, lw = 3, labels = ["Gradients", "iterations","Gradients"], legend = ["m1"], AxesStyle = "Normal ", color = "k") ax4 = gl.plot([], m2_values, ax = ax4, lw = 3, legend = ["m2"], AxesStyle = "Normal ", color = "b") #gl.set_zoom (ax = ax7, xlim = [-3, 3], ylim = [-0.1,2]) # Set final properties and save figure gl.set_fontSizes(ax = [ax1,ax2,ax3,ax4], title = 20, xlabel = 20, ylabel = 20, legend = 10, xticks = 12, yticks = 12) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.30, hspace=0.10) gl.savefig("P1_PardD.png", dpi = 100, sizeInches = [25, 7], close = False, bbox_inches = "tight")
# Value: value = str( List_f_analyses[an_i](cf_a)) # F1 values train x_values = 1 + np.array(range(len(f1_train))) gl.plot(x_values, f1_train, ax = ax1, labels = ["F1 score TR","epoch", "F1 score"],color = tr_color, legend = [List_analyses[an_i] +": " + value]) # F1 values val x_values = 1 + np.array(range(len(f1_train))) gl.plot(x_values, f1_validation, ax = ax2, labels = ["F1 score DEV","epoch", "F1 score"],color = tr_color, legend = [List_analyses[an_i] +": " + value]) # start span and end span values gl.set_fontSizes(ax = [ax1,ax2], title = 18, xlabel = 15, ylabel = 15, legend = 12, xticks = 12, yticks = 12) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.20, hspace=0.10) gl.savefig(folder_images + images_prefix + "CV_baseline_Bayesian"+List_analyses[an_i].replace(" ","_") + ".png", dpi = 100, sizeInches = [14, 5], close = False, bbox_inches = "tight") if (an_i == N_analyses-1): # Compute the std of the final F1. last_values_tr = [] last_values_val = [] for i in range(Nselected_models): training_logger = Selected_training_logger_list[i] last_values_tr.append(100*training_logger["train"]["f1"][-1]) last_values_val.append(100*training_logger["validation"]["f1"][-1]) print("Baseline models")
sharey=ax3) gl.stem(dates, ret2, ax=ax4, dataTransform=dataTransform, AxesStyle="Normal", labels=[ "", "", symbolIDs[symbol_ID_indx2] + "(" + str(periods[0]) + "M)" ], legend=["Return"]) # gl.set_fontSizes(ax=[ax1, ax2, ax3, ax4], title=20, xlabel=20, ylabel=20, legend=20, xticks=10, yticks=10) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.01, hspace=0.01) gl.savefig(folder_images + 'PriceAndReturns2Symbol_EM.png', dpi=100, sizeInches=[22, 12])
def create_Bayesian_analysis_charts(model, X_data_tr, Y_data_tr, X_data_val, Y_data_val, tr_loss, val_loss, KL_loss, final_loss_tr, final_loss_val, xgrid_real_func, ygrid_real_func, folder_images, epoch_i=None): # Configurations of the plots alpha_points = 0.2 color_points_train = "dark navy blue" color_points_val = "amber" color_train_loss = "cobalt blue" color_val_loss = "blood" color_truth = "k" color_mean = "b" color_most_likey = "y" ############################# Data computation ####################### if (type(X_data_tr) == type([])): pass else: if (X_data_tr.shape[1] == 1): # Regression Example x_grid, all_y_grid, most_likely_ygrid = compute_regression_1D_data( model, X_data_tr, X_data_val, Nsamples=100) elif (X_data_tr.shape[1] == 2): # Classification Example xx, yy, all_y_grid, most_likely_ygrid = compute_classification_2D_data( model, X_data_tr, X_data_val, Nsamples=100) else: # RNN x_grid, all_y_grid, most_likely_ygrid = compute_RNN_1D_data( model, X_data_tr, X_data_val, Nsamples=100) ################################ Divide in plots ############################## gl.init_figure() ax1 = gl.subplot2grid((6, 3), (0, 0), rowspan=3, colspan=1) ax2 = gl.subplot2grid((6, 3), (3, 0), rowspan=3, colspan=1, sharex=ax1, sharey=ax1) ax3 = gl.subplot2grid((6, 3), (0, 1), rowspan=2, colspan=1) ax4 = gl.subplot2grid((6, 3), (2, 1), rowspan=2, colspan=1, sharex=ax3) ax5 = gl.subplot2grid((6, 3), (4, 1), rowspan=2, colspan=1, sharex=ax3) ax6 = gl.subplot2grid((6, 3), (0, 2), rowspan=3, colspan=1) ax7 = gl.subplot2grid((6, 3), (3, 2), rowspan=3, colspan=1, sharex=ax6) if (type(X_data_tr) == type([])): Xtrain = [ torch.tensor(X_data_tr[i], device=model.cf_a.device, dtype=model.cf_a.dtype) for i in range(len(X_data_tr)) ] Ytrain = torch.tensor(Y_data_tr, device=model.cf_a.device, dtype=torch.int64) Xval = [ torch.tensor(X_data_val[i], device=model.cf_a.device, dtype=model.cf_a.dtype) for i in range(len(X_data_val)) ] Yval = torch.tensor(Y_data_val, device=model.cf_a.device, dtype=torch.int64) confusion = model.get_confusion_matrix(Xtrain, Ytrain) plot_confusion_matrix(confusion, model.languages, ax1) confusion = model.get_confusion_matrix(Xval, Yval) plot_confusion_matrix(confusion, model.languages, ax2) else: if (X_data_tr.shape[1] == 1): # Regression Example plot_data_regression_1d_2axes( X_data_tr, Y_data_tr, xgrid_real_func, ygrid_real_func, X_data_val, Y_data_val, x_grid, all_y_grid, most_likely_ygrid, alpha_points, color_points_train, color_points_val, color_most_likey, color_mean, color_truth, ax1, ax2) elif (X_data_tr.shape[1] == 2): # Classification Example plot_data_classification_2d_2axes( X_data_tr, Y_data_tr, xgrid_real_func, ygrid_real_func, X_data_val, Y_data_val, xx, yy, all_y_grid, most_likely_ygrid, alpha_points, color_points_train, color_points_val, color_most_likey, color_mean, color_truth, ax1, ax2) else: # RNN example plot_data_RNN_1d_2axes(X_data_tr, Y_data_tr, xgrid_real_func, ygrid_real_func, X_data_val, Y_data_val, x_grid, all_y_grid, most_likely_ygrid, alpha_points, color_points_train, color_points_val, color_most_likey, color_mean, color_truth, ax1, ax2) # gl.fill_between (x_grid, [mean_samples_grid + 2*std_samples_grid, mean_samples_grid - 2*std_samples_grid] # , ax = ax2, alpha = 0.10, color = "b", legend = ["Mean realizaions"]) ## ax2: The uncertainty of the prediction !! # gl.plot (x_grid, std_samples_grid, ax = ax2, labels = ["Std (%i)"%(Nsamples),"X","f(X)"], legend = [" std predictions"], fill = 1, alpha = 0.3) ############## ax3 ax4 ax5: Loss Evolution !! ###################### ## ax3: Evolutoin of the data loss gl.plot([], tr_loss, ax=ax3, lw=3, labels=["Losses", "", "Data loss"], legend=["train"], color=color_train_loss) gl.plot([], val_loss, ax=ax3, lw=3, legend=["validation"], color=color_val_loss, AxesStyle="Normal - No xaxis") ## ax4: The evolution of the KL loss gl.plot([], KL_loss, ax=ax4, lw=3, labels=["", "", "KL loss"], legend=["Bayesian Weights"], AxesStyle="Normal - No xaxis", color="k") ## ax5: Evolutoin of the total loss gl.plot([], final_loss_tr, ax=ax5, lw=3, labels=["", "epoch", "Total Loss (Bayes)"], legend=["train"], color=color_train_loss) gl.plot([], final_loss_val, ax=ax5, lw=3, legend=["validation"], color=color_val_loss) ############## ax6 ax7: Variational Weights !! ###################### create_plot_variational_weights(model, ax6, ax7) ## Plot in chart 7 the acceptable mu = 2sigma -> sigma = |mu|/2sigma mu_grid = np.linspace(-3, 3, 100) y_grid = np.abs(mu_grid) / 2 gl.fill_between(mu_grid, 10 * np.ones(mu_grid.size), y_grid, alpha=0.2, color="r", ax=ax7, legend=["95% non-significant"]) gl.set_zoom(ax=ax6, ylim=[-0.1, 10]) gl.set_zoom(ax=ax7, xlim=[-2.5, 2.5], ylim=[ -0.05, np.exp(model.cf_a.input_layer_prior["log_sigma2"]) * (1 + 0.15) ]) # gl.set_zoom (ax = ax7, xlim = [-2.5, 2.5], ylim = [-0.1,2]) # Set final properties and save figure gl.set_fontSizes(ax=[ax1, ax2, ax3, ax4, ax5, ax6, ax7], title=20, xlabel=20, ylabel=20, legend=10, xticks=12, yticks=12) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.30, hspace=0.10) if (type(epoch_i) == type(None)): gl.savefig(folder_images + "../" + 'Final_values_regression_1D_' + str(model.cf_a.eta_KL) + '.png', dpi=100, sizeInches=[20, 10]) else: gl.savefig(folder_images + '%i.png' % epoch_i, dpi=100, sizeInches=[20, 10], close=True, bbox_inches="tight")
legend = list_all_labels[i] if(i == 1): shape_weights = (400,200) plots_weights_layer(mu_W.reshape(shape_weights)[:200,:].flatten(), sigma_W.reshape(shape_weights)[:200,:].flatten(), mu_b[:200], sigma_b[:200], ax1, "All weights", legend = [legend + " G(x)"], alpha = 0.1) plots_weights_layer(mu_W.reshape(shape_weights)[200:,:].flatten(), sigma_W.reshape(shape_weights)[200:,:].flatten(), mu_b[200:], sigma_b[200:], ax1, "All weights", legend = [legend + " H(x)"], alpha = 0.1) else: plots_weights_layer(mu_W, sigma_W,mu_b, sigma_b, ax1, "All weights", legend = [legend], alpha = 0.1) list_all_axes.append(ax1) max_mu,min_mu,max_std,min_std,max_abs = compute_all_boundaries(list_all_weights) plot_signifant_region(ax1, max_mu,min_mu,max_std,min_std,max_abs) gl.set_zoom (ax = ax1, xlimPad = [0.1, 0.1], ylimPad = [0.1,0.1], X = np.array([min_mu,max_mu]), Y = np.array([min_std,max_std]) ) gl.set_fontSizes(ax = list_all_axes, title = 15, xlabel = 15, ylabel = 15, legend = 10, xticks = 12, yticks = 12) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.20, hspace=0.22) for ax in list_all_axes: ax.ticklabel_format(style='sci',scilimits=(0,0),axis='both') gl.savefig(folder_images +'Bayesian_weights_biday.png', dpi = 100, sizeInches = [18, 12]) if(1): """ PLOT OF THE SPAN INPUT WEIGHTS BY TYPE OF THE WEIGHTS !! """
def create_image_weights_epoch(model, video_fotograms_folder2, epoch_i): """ Creates the image of the training and validation accuracy """ N_Bayesian_layers = len(model.VBmodels) N_Normal_layers = len(model.LinearModels) # Compute the number of squares we will need: # 1 x linear layers, 2 x LSTMS gl.init_figure() cmap = cm.get_cmap('coolwarm', 30) all_axes = [] for i in range(N_Bayesian_layers): layer = model.VBmodels[i] # if (layer.type_layer == "linear"): if ("linear" in type(layer).__name__.lower()): ax = gl.subplot2grid((1, N_Bayesian_layers + N_Normal_layers), (0, i), rowspan=1, colspan=1) weights = layer.weight.detach().cpu().numpy() biases = layer.bias.detach().cpu().numpy().reshape(-1, 1) neurons = np.concatenate((weights, biases), axis=1) cax = ax.imshow(neurons, interpolation="nearest", cmap=cmap, vmin=-2, vmax=2) all_axes.append(ax) else: ax = gl.subplot2grid((1, N_Bayesian_layers + N_Normal_layers), (0, i), rowspan=1, colspan=1) weights_ih = layer.weight_ih.detach().cpu().numpy() biases_ih = layer.bias_ih.detach().cpu().numpy().reshape(-1, 1) weights_hh = layer.weight_hh.detach().cpu().numpy() biases_hh = layer.bias_hh.detach().cpu().numpy().reshape(-1, 1) weights = np.concatenate((weights_ih, weights_hh), axis=1) biases = np.concatenate((biases_ih, biases_hh), axis=1) neurons = np.concatenate((weights, biases), axis=1) cax = ax.imshow(neurons, interpolation="nearest", cmap=cmap, vmin=-2, vmax=2) all_axes.append(ax) for i in range(N_Normal_layers): layer = model.LinearModels[i] if ("linear" in type(layer).__name__.lower()): ax = gl.subplot2grid((1, N_Bayesian_layers + N_Normal_layers), (0, N_Bayesian_layers + i), rowspan=1, colspan=1) weights = layer.weight.detach().cpu().numpy() biases = layer.bias.detach().cpu().numpy().reshape(-1, 1) neurons = np.concatenate((weights, biases), axis=1) cax = ax.imshow(neurons, interpolation="nearest", cmap=cmap, vmin=-2, vmax=2) all_axes.append(ax) else: ax = gl.subplot2grid((1, N_Bayesian_layers + N_Normal_layers), (0, N_Bayesian_layers + i), rowspan=1, colspan=1) weights_ih = layer.weight_ih.detach().cpu().numpy() biases_ih = layer.bias_ih.detach().cpu().numpy().reshape(-1, 1) weights_hh = layer.weight_hh.detach().cpu().numpy() biases_hh = layer.bias_hh.detach().cpu().numpy().reshape(-1, 1) weights = np.concatenate((weights_ih, weights_hh), axis=1) biases = np.concatenate((biases_ih, biases_hh), axis=1) neurons = np.concatenate((weights, biases), axis=1) cax = ax.imshow(neurons, interpolation="nearest", cmap=cmap, vmin=-2, vmax=2) all_axes.append(ax) # plt.xticks(range(data_df_train.shape[1]), data_df_train.columns, rotation='vertical') # plt.yticks(range(data_df_train.shape[1]), data_df_train.columns, rotation='horizontal') plt.colorbar(cax) # plt.colorbar(cax2) # ax1.set_xticks(data_df_train.columns) # , rotation='vertical' # ax1.grid(True) plt.title('Weights ') # labels=[str(x) for x in range(Nshow )] # ax1.set_xticklabels(labels,fontsize=20) # ax1.set_yticklabels(labels,fontsize=20) # Add colorbar, make sure to specify tick locations to match desired ticklabels plt.show() gl.set_fontSizes(ax=[all_axes], title=20, xlabel=20, ylabel=20, legend=20, xticks=12, yticks=12) # Set final properties and save figure gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.30, hspace=0.30) gl.savefig(video_fotograms_folder2 + '%i.png' % epoch_i, dpi=100, sizeInches=[14, 10], close=True, bbox_inches=None)
I dont remember, just a test to plot stuff """ olmo = 33 X = ["uno","dos","ocho","cinco"]; X = np.array(X) dataHLOC = timeData.get_timeSeries(["High","Low"]) Y4 = dataHLOC[:X.size,:] Y1 = dataHLOC[:X.size,1] dates = timeData.get_dates() gl.plot (X,Y4, labels = [r"Curca $y={2}x + {%i}\alpha \pi $"%56, r"$\alpha \pi$", "Pene"], legend = ["retarded"]) # gl.plot (dates,dataHLOC, labels = [r"Curca $y={2}x + {%i}\alpha \pi $"%56, r"$\alpha \pi$", "Pene"], # legend = ["retarded"], xaxis_mode = "dayly") gl.set_fontSizes(title = 40, xlabel = 30, ylabel = 30, xticks = 20, yticks = 20, legend = 40) ############## Other graphical Properties ################################# if (plot_gaps_scattering == 1): """ Very old trial to plot the gaps to try to see some patterns """ gl.set_subplots(1,2) timeData.scatter_deltaDailyMagic() ############## Velero Graphs ################################# if (Candlestick_f == 1): """ Plot the Candlestick charts """
## Bias !! for i in range(len(list_all_labels)): gl.plot(trim_mu_sigma, list_final_pcts_b, ax=ax3, legend=[ list_all_labels[i] + " (" + str(weights_statistics_list[0][2][i]) + ")" ], labels=["", r"$|\mu_w|/\sigma_w$", "Biases"]) # Set final properties and save figure gl.set_fontSizes(ax=[ax1, ax2, ax3], title=20, xlabel=20, ylabel=20, legend=10, xticks=15, yticks=15) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.30, hspace=0.10) gl.savefig(folder_images + images_prefix + "Trimming_accuracies.png", dpi=100, sizeInches=[18, 6], close=False, bbox_inches="tight")
alpha=0.1) list_all_axes.append(ax1) max_mu, min_mu, max_std, min_std, max_abs = compute_all_boundaries( list_all_weights) plot_signifant_region(ax1, max_mu, min_mu, max_std, min_std, max_abs) gl.set_zoom(ax=ax1, xlimPad=[0.1, 0.1], ylimPad=[0.1, 0.1], X=np.array([min_mu, max_mu]), Y=np.array([min_std, max_std])) gl.set_fontSizes(ax=list_all_axes, title=15, xlabel=15, ylabel=15, legend=10, xticks=12, yticks=12) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.20, hspace=0.22) for ax in list_all_axes: ax.ticklabel_format(style='sci', scilimits=(0, 0), axis='both') gl.savefig(folder_images + 'Bayesian_weights_biday.png', dpi=100, sizeInches=[18, 12])
plt.ylabel('CE') plt.grid() plt.show() gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.05, hspace=0.2) gl.set_fontSizes(ax=[ ax1, ax2, ], title=20, xlabel=20, ylabel=20, legend=15, xticks=18, yticks=12) gl.savefig(folder_images + 'Classifiers_performance.png', dpi=100, sizeInches=[3 * 8, 3 * 2]) if (plot_results): key_classifier = "QDA" # QDA # GNB RF classifier = cl_d[key_classifier] # Compute how well we have done in each sample using cross entropy Ypredict_test_proba = classifier.predict_proba(
gl.plot(x_values, f1_train, ax = ax2, labels = ["F1 score","epoch", "F1 score"],color = tr_color, legend = ["F1 TR"]) gl.plot(x_values, f1_validation, legend = ["F1 DEV"], color = val_color) # start span and end span values x_values = 1 + np.array(range(len(start_acc_train))) gl.plot(x_values, start_acc_train, ax = ax3, labels = ["Start and End span Acc","epoch", "score"], legend = ["start span TR"], color = tr_color) gl.plot(x_values, end_acc_train, legend = ["end span TR"], color = tr_color, ls = "--") gl.plot(x_values, start_acc_validation, ax = ax3, legend = ["start span DEV"], color = val_color) gl.plot(x_values, end_acc_validation, legend = ["end span DEV"], color = val_color, ls = "--") gl.set_fontSizes(ax = [ax1,ax2,ax3], title = 20, xlabel = 18, ylabel = 18, legend = 15, xticks = 14, yticks = 14) gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.20, hspace=0.10) gl.format_yaxis ( ax = ax3, Nticks = 10); gl.format_xaxis ( ax = ax3, Nticks = len(em_train)); gl.savefig(folder_images + images_prefix + "Accuracies_epoch.png", dpi = 100, sizeInches = [20, 5], close = False, bbox_inches = "tight") if (Batch_related): """ ############################################# Batch plots """ data_loss_batch = training_logger["train"]["loss_batch"] em_train_batches = 100*np.array(training_logger["train"]["em_batch"]) f1_train_batches = 100*np.array(training_logger["train"]["f1_batch"])