Exemple #1
0
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 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 redraw_chart(myTimeData):

    dataTransform = ["intraday", opentime, closetime]
    period = periods[0]
    myTimeData = mySymbol.get_timeData(period)
    AxesStyle = ""
    title = "Bar Chart. " + str(symbolID) + r" . Price ($\$$)"
    ylabel = ul5.period_dic[myTimeData.period]

    #    fig = gl.init_figure()
    ax = gl.tradingBarChart(myTimeData,
                            legend=["Close price"],
                            color="k",
                            nf=1,
                            labels=[title, "", ylabel],
                            AxesStyle="Normal" + AxesStyle,
                            dataTransform=dataTransform)
    gl.subplots_adjust(left=.09,
                       bottom=.10,
                       right=.90,
                       top=.95,
                       wspace=.20,
                       hspace=0)
Exemple #4
0
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")
Exemple #5
0
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")
Exemple #6
0
                             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,
        they belong to a distribution with more time
    """
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)
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")
                  AxesStyle="Normal - No xaxis")
    # Axes with the EMAs
    legend = ["Price"]
    EMAs.insert(0, price)
    legend.extend(["EMA(" + str(x) + ")" for x in nEMAs])
    gl.plot(dates,
            EMAs,
            nf=1,
            sharex=ax1,
            labels=["", "", r"Price ($\$$)"],
            legend=legend,
            AxesStyle="Normal")

    gl.subplots_adjust(left=.09,
                       bottom=.10,
                       right=.90,
                       top=.95,
                       wspace=.20,
                       hspace=0)

    gl.savefig(folder_images + 'lagsMAs.png', dpi=100, sizeInches=[16, 9])

if (viewing_SEW_windows):
    # Some basic indicators.
    price = timeData.get_timeSeries(["Close"])
    dates = timeData.get_dates()

    nMA1 = 10
    nMA2 = 20

    SMAw = indl.get_SMA(bMA.delta(nMA1), nMA1, cval=1)
    EMAw = indl.get_EMA(bMA.delta(nMA1), nMA1, cval=1)
Exemple #10
0
def generate_images_iterations_ll(Xs, mus, covs, Ks, myDManager, logl,
                                  theta_list, model_theta_list,
                                  folder_images_gif):
    #    os.remove(folder_images_gif) # Remove previous images if existing
    """
    WARNING: MEANT FOR ONLY 3 Distributions due to the color RGB
    """
    import shutil
    ul.create_folder_if_needed(folder_images_gif)
    shutil.rmtree(folder_images_gif)
    ul.create_folder_if_needed(folder_images_gif)
    ######## Plot the original data #####

    Xdata = np.concatenate(Xs, axis=1).T
    colors = ["r", "b", "g"]
    K_G, K_W, K_vMF = Ks

    ### FOR EACH ITERATION
    for i in range(len(theta_list)):  # theta_list
        indx = i
        gl.init_figure()
        ax1 = gl.subplot2grid((1, 2), (0, 0), rowspan=1, colspan=1)

        ## Get the relative ll of the Gaussian denoising cluster.
        ll = myDManager.pdf_log_K(Xdata, theta_list[indx])
        N, K = ll.shape
        #        print ll.shape
        for j in range(N):  # For every sample
            #TODO: Can this not be done without a for ?
            # Normalize the probability of the sample being generated by the clusters
            Marginal_xi_probability = gf.sum_logs(ll[j, :])
            ll[j, :] = ll[j, :] - Marginal_xi_probability

            ax1 = gl.scatter(
                Xdata[j, 0],
                Xdata[j, 1],
                labels=[
                    'EM Evolution. Kg:' + str(K_G) + ', Kw:' + str(K_W) +
                    ', K_vMF:' + str(K_vMF), "X1", "X2"
                ],
                color=(np.exp(ll[j, 1]), np.exp(ll[j, 0]),
                       np.exp(ll[j, 2])),  ###  np.exp(ll[j,2])
                alpha=1,
                nf=0)

        # Only doable if the clusters dont die
        for k_c in myDManager.clusterk_to_Dname.keys():
            k = myDManager.clusterk_to_thetak[k_c]
            distribution_name = myDManager.clusterk_to_Dname[k_c]  # G W

            if (distribution_name == "Gaussian"):
                ## Plot the ecolution of the mu
                #### Plot the Covariance of the clusters !
                mean, w, h, theta = bMA.get_gaussian_ellipse_params(
                    mu=theta_list[indx][k][0],
                    Sigma=theta_list[indx][k][1],
                    Chi2val=2.4477)
                r_ellipse = bMA.get_ellipse_points(mean, w, h, theta)
                gl.plot(r_ellipse[:, 0],
                        r_ellipse[:, 1],
                        ax=ax1,
                        ls="-.",
                        lw=3,
                        AxesStyle="Normal2",
                        legend=[
                            "Kg(%i). pi:%0.2f" %
                            (k, float(model_theta_list[indx][0][0, k]))
                        ])

            elif (distribution_name == "Watson"):
                #### Plot the pdf of the distributino !
                ## Distribution parameters for Watson
                kappa = float(theta_list[indx][k][1])
                mu = theta_list[-1][k][0]
                Nsa = 1000
                # Draw 2D samples as transformation of the angle
                Xalpha = np.linspace(0, 2 * np.pi, Nsa)
                Xgrid = np.array([np.cos(Xalpha), np.sin(Xalpha)])

                probs = []  # Vector with probabilities
                for i in range(Nsa):
                    probs.append(
                        np.exp(Wad.Watson_pdf_log(Xgrid[:, i], [mu, kappa])))

                probs = np.array(probs)
                # Plot it in polar coordinates
                X1_w = (1 + probs) * np.cos(Xalpha)
                X2_w = (1 + probs) * np.sin(Xalpha)

                gl.plot(X1_w,
                        X2_w,
                        alpha=1,
                        lw=3,
                        ls="-.",
                        legend=[
                            "Kw(%i). pi:%0.2f" %
                            (k, float(model_theta_list[indx][0][0, k]))
                        ])

            elif (distribution_name == "vonMisesFisher"):
                #### Plot the pdf of the distributino !
                ## Distribution parameters for Watson
                kappa = float(theta_list[indx][k][1])
                mu = theta_list[indx][k][0]
                Nsa = 1000
                # Draw 2D samples as transformation of the angle
                Xalpha = np.linspace(0, 2 * np.pi, Nsa)
                Xgrid = np.array([np.cos(Xalpha), np.sin(Xalpha)])

                probs = []  # Vector with probabilities
                for i in range(Nsa):
                    probs.append(
                        np.exp(
                            vMFd.vonMisesFisher_pdf_log(
                                Xgrid[:, i], [mu, kappa])))

                probs = np.array(probs)
                probs = probs.reshape((probs.size, 1)).T
                # Plot it in polar coordinates
                X1_w = (1 + probs) * np.cos(Xalpha)
                X2_w = (1 + probs) * np.sin(Xalpha)

                #            print X1_w.shape, X2_w.shape
                gl.plot(X1_w,
                        X2_w,
                        alpha=1,
                        lw=3,
                        ls="-.",
                        legend=[
                            "Kvmf(%i). pi:%0.2f" %
                            (k, float(model_theta_list[indx][0][0, k]))
                        ])

        gl.set_zoom(xlim=[-6, 6], ylim=[-6, 6], ax=ax1)
        ax2 = gl.subplot2grid((1, 2), (0, 1), rowspan=1, colspan=1)
        if (indx == 0):
            gl.add_text(positionXY=[0.1, .5],
                        text=r' Initilization Incomplete LogLike: %.2f' %
                        (logl[0]),
                        fontsize=15)
            pass
        elif (indx >= 1):

            gl.plot(
                range(1,
                      np.array(logl).flatten()[1:].size + 1),
                np.array(logl).flatten()[1:(indx + 1)],
                ax=ax2,
                legend=["Iteration %i, Incom LL: %.2f" % (indx, logl[indx])],
                labels=[
                    "Convergence of LL with generated data", "Iterations", "LL"
                ],
                lw=2)
            gl.scatter(1, logl[1], lw=2)
            pt = 0.05
            gl.set_zoom(xlim=[0, len(logl)],
                        ylim=[
                            logl[1] - (logl[-1] - logl[1]) * pt,
                            logl[-1] + (logl[-1] - logl[1]) * pt
                        ],
                        ax=ax2)

        gl.subplots_adjust(left=.09,
                           bottom=.10,
                           right=.90,
                           top=.95,
                           wspace=.2,
                           hspace=0.01)

        gl.savefig(folder_images_gif + 'gif_' + str(indx) + '.png',
                   dpi=100,
                   sizeInches=[16, 8],
                   close="yes",
                   bbox_inches=None)

        gl.close("all")
Exemple #11
0
def plot_multiple_iterations(Xs, mus, covs, Ks, myDManager, logl, theta_list,
                             model_theta_list, folder_images):
    ######## Plot the original data #####
    gl.init_figure()
    gl.set_subplots(2, 3)
    Ngraph = 6

    colors = ["r", "b", "g"]
    K_G, K_W, K_vMF = Ks

    for i in range(Ngraph):
        indx = int(i * ((len(theta_list) - 1) / float(Ngraph - 1)))
        nf = 1
        for xi in range(len(Xs)):
            ## First cluster
            labels = [
                'EM Evolution. Kg:' + str(K_G) + ', Kw:' + str(K_W) +
                ', K_vMF:' + str(K_vMF), "X1", "X2"
            ]
            ax1 = gl.scatter(Xs[xi][0, :],
                             Xs[xi][1, :],
                             labels=["", "", ""],
                             color=colors[xi],
                             alpha=0.2,
                             nf=nf)
            nf = 0
            mean, w, h, theta = bMA.get_gaussian_ellipse_params(mu=mus[xi],
                                                                Sigma=covs[xi],
                                                                Chi2val=2.4477)
            r_ellipse = bMA.get_ellipse_points(mean, w, h, theta)
            gl.plot(r_ellipse[:, 0],
                    r_ellipse[:, 1],
                    ax=ax1,
                    ls="--",
                    lw=2,
                    AxesStyle="Normal2",
                    color=colors[xi],
                    alpha=0.7)

        # Only doable if the clusters dont die
        for k_c in myDManager.clusterk_to_Dname.keys():
            k = myDManager.clusterk_to_thetak[k_c]
            distribution_name = myDManager.clusterk_to_Dname[k_c]  # G W

            if (distribution_name == "Gaussian"):
                ## Plot the ecolution of the mu
                #### Plot the Covariance of the clusters !
                mean, w, h, theta = bMA.get_gaussian_ellipse_params(
                    mu=theta_list[indx][k][0],
                    Sigma=theta_list[indx][k][1],
                    Chi2val=2.4477)
                r_ellipse = bMA.get_ellipse_points(mean, w, h, theta)
                gl.plot(r_ellipse[:, 0],
                        r_ellipse[:, 1],
                        ax=ax1,
                        ls="-.",
                        lw=3,
                        AxesStyle="Normal2",
                        legend=[
                            "Kg(%i). pi:%0.2f" %
                            (k, float(model_theta_list[indx][0][0, k]))
                        ])

            elif (distribution_name == "Watson"):
                #### Plot the pdf of the distributino !
                ## Distribution parameters for Watson
                kappa = float(theta_list[indx][k][1])
                mu = theta_list[indx][k][0]

                Nsa = 1000
                # Draw 2D samples as transformation of the angle
                Xalpha = np.linspace(0, 2 * np.pi, Nsa)
                Xgrid = np.array([np.cos(Xalpha), np.sin(Xalpha)])

                probs = []  # Vector with probabilities
                for i in range(Nsa):
                    probs.append(
                        np.exp(Wad.Watson_pdf_log(Xgrid[:, i], [mu, kappa])))

                probs = np.array(probs)
                # Plot it in polar coordinates
                X1_w = (1 + probs) * np.cos(Xalpha)
                X2_w = (1 + probs) * np.sin(Xalpha)

                gl.plot(X1_w,
                        X2_w,
                        alpha=1,
                        lw=3,
                        ls="-.",
                        legend=[
                            "Kw(%i). pi:%0.2f" %
                            (k, float(model_theta_list[indx][0][0, k]))
                        ])

            elif (distribution_name == "vonMisesFisher"):
                #### Plot the pdf of the distributino !
                ## Distribution parameters for Watson
                kappa = float(theta_list[indx][k][1])
                mu = theta_list[indx][k][0]
                Nsa = 1000
                # Draw 2D samples as transformation of the angle
                Xalpha = np.linspace(0, 2 * np.pi, Nsa)
                Xgrid = np.array([np.cos(Xalpha), np.sin(Xalpha)])

                probs = []  # Vector with probabilities
                for i in range(Nsa):
                    probs.append(
                        np.exp(
                            vMFd.vonMisesFisher_pdf_log(
                                Xgrid[:, i], [mu, kappa])))

                probs = np.array(probs)
                probs = probs.reshape((probs.size, 1)).T
                # Plot it in polar coordinates
                X1_w = (1 + probs) * np.cos(Xalpha)
                X2_w = (1 + probs) * np.sin(Xalpha)

                #            print X1_w.shape, X2_w.shape
                gl.plot(X1_w,
                        X2_w,
                        alpha=1,
                        lw=3,
                        ls="-.",
                        legend=[
                            "Kvmf(%i). pi:%0.2f" %
                            (k, float(model_theta_list[indx][0][0, k]))
                        ])

        ax1.axis('equal')
    gl.subplots_adjust(left=.09,
                       bottom=.10,
                       right=.90,
                       top=.95,
                       wspace=.2,
                       hspace=0.01)
    gl.savefig(folder_images + 'Final_State2. K_G:' + str(K_G) + ', K_W:' +
               str(K_W) + '.png',
               dpi=100,
               sizeInches=[18, 8])
Exemple #12
0
    def init_figure(self):
        """
        This function initializes the chart, with its widgets and everything
        
        """
        
        button_height = 0.030;
        textbox_length0 = 0.02
        textbox_length1 = 0.04
        textbox_length2 = 0.05
         
        fig = gl.init_figure();
        ## Set the image to full screen
        fig_manager = plt.get_current_fig_manager()
        if hasattr(fig_manager, 'window'):
            fig_manager.window.showMaximized()
    
        data_axes = gl.subplot2grid((1,4), (0,0), rowspan=1, colspan=3)
        
        self.fig = fig; self.data_axes = data_axes;
        
        #### Logo Images !!
        logo_path =  self.output_folder + "images_IoTubes/IoTubes_logo.png"
        image = mpimg.imread(logo_path)
        ax_img = plt.axes([0.725, 0.75, 0.2, 0.2])
        ax_img.imshow(image)
        ax_img.axis("off")
        
        ################## Widgets Axes #####################
        
       
        widgets_x = 0.76
        widgets_x2 = 0.85
        widgets_x3 = 0.90
        
        w1_x, w2_x, w3_x = 0.73, 0.8,0.87
        
        base_y = 0.69
        
        administration_y = base_y
        monitoring_y = administration_y - 0.12
        chart_s_y = monitoring_y - 0.12
        chart_s_y2 = chart_s_y -0.05
        chart_start_stop_y = chart_s_y2 - 0.05
        
        output_y = chart_start_stop_y - 0.12

        
        diff_headline_content =  0.052
        ## Administration ! 
        headlines_x = 0.705
        text = self.fig.text(headlines_x, administration_y + diff_headline_content, 'Administration:', size=20) # ha='center', va='center', size=20)
        
        axbox_machineID = plt.axes([widgets_x, administration_y, textbox_length1, button_height])
        axbox_pipingID = plt.axes([widgets_x2, administration_y, textbox_length1, button_height])

        ### Monitoring
        text = self.fig.text(headlines_x, monitoring_y + diff_headline_content, 'PH Monitoring:', size=20) # ha='center', va='center', size=20)
        axbox_desired_value = plt.axes([widgets_x, monitoring_y, textbox_length0, button_height])
        axbox_range_warning = plt.axes([widgets_x2, monitoring_y, textbox_length0, button_height])
        
        ## Sampling and plotting
        text = self.fig.text(headlines_x, output_y + diff_headline_content, 'Output Generation:', size=20) # ha='center', va='center', size=20)
        axbox_sample_period = plt.axes([widgets_x, chart_s_y, textbox_length1, button_height])
        axbox_plot_period = plt.axes([widgets_x2, chart_s_y, textbox_length1, button_height])
        axbox_Nsamples_show = plt.axes([widgets_x, chart_s_y2, textbox_length1, button_height])
        
        ax_start = plt.axes([widgets_x,chart_start_stop_y, 0.04, button_height])
        ax_stop = plt.axes([widgets_x2, chart_start_stop_y, 0.04, button_height])
        
        ## Output
        text = self.fig.text(headlines_x, chart_s_y + diff_headline_content, 'Sampling and plotting:', size=20) # ha='center', va='center', size=20)
        axsave_disk = plt.axes([w1_x, output_y, 0.055, button_height])
        axsave_DDBB = plt.axes([w2_x, output_y, 0.055, button_height])
        axreport = plt.axes([w3_x, output_y, 0.055, button_height])

        
        ################## Add functionalities ###########################
        
        ################ Chart AXES ################:
        bstop = Button(ax_stop, 'Stop')
        bstop.on_clicked(self.stop_reading_data)
        
        bstart = Button(ax_start, 'Start')
        bstart.on_clicked(self.start_reading_data)
#        bprev.on_clicked(self.auto_update_test)
        
        #### Text input Period  ####
        initial_text = str(int(self.period_sampling * 1000));
        text_box_sample_period = TextBox(axbox_sample_period, 'Sample(ms) ', initial=initial_text)
        text_box_sample_period.on_submit(self.submit_sample_period)
        
        initial_text = str(int(self.period_plotting * 1000));
        text_box_plotting_period = TextBox(axbox_plot_period, 'Plot(ms) ', initial=initial_text)
        text_box_plotting_period.on_submit(self.submit_plotting_period)
        
        #### Text input N samples ####
        initial_text = str(int(self.show_window));
        text_Nsamples_show = TextBox(axbox_Nsamples_show, 'Samples Chart ', initial=initial_text)
        text_Nsamples_show.on_submit(self.submit_show_window)
        
        ################ Data generation widgets ################
        bpsave_disk = Button(axsave_disk, 'Save Disk')
        bpsave_disk.on_clicked(self.save_to_disk)
        
        bpsave_DDBB = Button(axsave_DDBB, 'Save DDBB')
        bpsave_DDBB.on_clicked(self.send_buffer_to_DDBB)
        
        bpsave_report = Button(axreport, 'Report')
        bpsave_report.on_clicked(self.generate_report)
        
        ################ Cleaning input widgets ################
        ## Text input MAchine ID
        initial_text = self.machine_ID
        text_box_machine = TextBox(axbox_machineID, 'Machine ID ', initial=initial_text)
        text_box_machine.on_submit(self.submit_machineID)
    
        initial_text = self.piping_ID
        text_box_piping = TextBox(axbox_pipingID, 'Piping ID ', initial=initial_text)
        text_box_piping.on_submit(self.submit_pipingID)
        
    
        
        ################ MONITORING variables ################
        initial_text = str(self.Monitor.desired_value);
        text_desired_value = TextBox(axbox_desired_value, 'Desired PH ', initial=initial_text)
        text_desired_value.on_submit(self.submit_desired_value)
        
        initial_text = str(self.Monitor.range_warning);
        text_range_warning = TextBox(axbox_range_warning, 'Warning Range ', initial=initial_text)
        text_range_warning.on_submit(self.submit_range_warning)
        
        
        # I think we needed to keep them in memory of they would die
        self.buttons = [bstart, bstop, bpsave_disk,bpsave_DDBB,text_box_machine,
                        text_box_sample_period,text_box_plotting_period,
                        text_Nsamples_show,
                        text_desired_value, text_range_warning,bpsave_report, text_box_piping]


        
        self.initial_text_data = gl.add_text(positionXY = [0.35,0.5], text = r'Waiting for data',fontsize = 30, ax = data_axes)
        
        gl.subplots_adjust(left=.09, bottom=.20, right=.90, top=.90, wspace=.20, hspace=0)
        
        self.monitoring_y = monitoring_y
Exemple #13
0
    legend.extend([ "SMA(" +str(x) +")" for x in nSMAs])
    SMAs.insert(0, price)
    
    title = "Influence of L in the lag. " + str(symbols[0]) + "(" + ul.period_dic[timeData.period]+ ")"
    ax1 = gl.plot(dates,  SMAs , nf = 1,
            labels = [title,"",r"Price ($\$$)"],
            legend = legend,  AxesStyle = "Normal - No xaxis")
    # Axes with the EMAs
    legend = ["Price"]
    EMAs.insert(0, price)
    legend.extend([ "EMA(" +str(x) +")"for x in nEMAs])
    gl.plot(dates, EMAs, nf = 1, sharex= ax1,
            labels = ["","",r"Price ($\$$)"],
            legend = legend,  AxesStyle = "Normal")

    gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.20, hspace=0)

    gl.savefig(folder_images +'lagsMAs.png', 
               dpi = 100, sizeInches = [16, 9])
               
if (viewing_SEW_windows):
    # Some basic indicators.
    price = timeData.get_timeSeries(["Close"]);
    dates = timeData.get_dates()

    nMA1 = 10
    nMA2 = 20

    SMAw = indl.get_SMA(bMA.delta(nMA1), nMA1, cval = 1)
    EMAw = indl.get_EMA(bMA.delta(nMA1), nMA1, cval = 1)
    WMAw = indl.get_WMA(bMA.delta(nMA1), nMA1, cval = 1)
Exemple #14
0
def visualize_attention_matrix(question_tokens, passage_tokens, attention_matrix,
                               image_path):
        """
            Text to visualze attention map for.a given exmaple.
            
            question_tokens: List of tokens of the question
            passage_tokens: List of tokens of the passage
            attention_matrix: len(passage) x len(question) matrix with the probabilities 
        """
        
        f = gl.init_figure()
        ax = f.add_axes([0.1, 0.3, 0.8, 0.5])
        ax_attention_words = f.add_axes([0.1, 0.70, 0.8, 0.15])
        ax_attention_words.axis('off')
        
        
        # add image
        cmap = "binary" #cm.get_cmap('coolwarm', 30)
        i = ax.imshow(attention_matrix, interpolation='nearest', cmap=cmap,vmin=0, vmax=1)

        # add colorbar
        cbaxes = f.add_axes([0.95, 0.3, 0.02, 0.5])
        cbar = f.colorbar(i, cax=cbaxes, orientation='vertical')
        cbar.ax.set_xlabel('Probability', labelpad=6)

        # add labels
        ax.set_yticks(range(len(question_tokens)))
        ax.set_yticklabels(question_tokens)
        
        ax.set_xticks(range(len(passage_tokens)))
        ax.set_xticklabels(passage_tokens, rotation=80)
        
        ax.set_xlabel('Passage')
        ax.set_ylabel('Question')
        
        ###########  GET THE MOST ATTENTION WORDS ########
        Nmax_attention_words = 3
        z = (-attention_matrix).argsort(axis = 1)[:,:]
        
        attentioned_passage_words = []
        for i in range (len(question_tokens)):
            attentioned_passage_words.append([])
            for j in range(Nmax_attention_words):
                attentioned_passage_words[-1].append(passage_tokens[z[i,j]] + "(%.1f%%)"%(attention_matrix[i,z[i,j]]*100))
            attentioned_passage_words[-1] = ", ".join(attentioned_passage_words[-1])

        
        text_correspondance = ""
        for i in range (len(question_tokens)):
            text_correspondance += question_tokens[i] + " ---> " + attentioned_passage_words[i] + "\n"
        
        ax_attention_words.text(0,0,text_correspondance)
#        ax2.yaxis.tick_right()
#        ax2.yaxis.set_label_position("right")
        
        f.show()
#        gl.set_fontSizes(ax = [ax1,ax2], title = 20, xlabel = 15, ylabel = 18, 
#                          legend = 12, xticks = 14, yticks = 14)
        gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.20, hspace=0.10)
        
        gl.savefig(image_path,  dpi = 100, sizeInches = [10, 6], close = False, bbox_inches = "tight") 
Exemple #15
0
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")
Exemple #16
0
    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 get_half_final_position (nu_z, x0):
Exemple #17
0
            alpha=0.7,
            labels=["Sine chart", "Time (s)", "Voltage(V)"],
            legend=["Rolling measurement"])
    #
    ax2 = gl.subplot2grid((1, 4), (0, 3), rowspan=1, colspan=1)
    gl.plot(X2,
            Y2,
            nf=0,
            color="k",
            lw=2,
            alpha=0.7,
            labels=["Discrete window", "Sample (k)", "Amplitud"],
            legend=["Window values"])
    gl.subplots_adjust(left=.1,
                       bottom=.1,
                       right=.90,
                       top=.95,
                       wspace=.40,
                       hspace=5)
    gl.savefig(folder_images + 'subplot2.png', dpi=dpi, sizeInches=sizeInches)
# Subplot Type 3
if (type_graph == 3):
    ax1 = gl.create_axes(position=[0.2, 0.2, 0.6, 0.4])
    gl.plot(X,
            Y,
            nf=0,
            color="k",
            lw=5,
            alpha=0.7,
            labels=["Sine chart", "Time (s)", "Voltage(V)"],
            legend=["Rolling measurement"])
def visualize_attention_matrix(question_tokens, passage_tokens,
                               attention_matrix, image_path):
    """
            Text to visualze attention map for.a given exmaple.
            
            question_tokens: List of tokens of the question
            passage_tokens: List of tokens of the passage
            attention_matrix: len(passage) x len(question) matrix with the probabilities 
        """

    f = gl.init_figure()
    ax = f.add_axes([0.1, 0.3, 0.8, 0.5])
    ax_attention_words = f.add_axes([0.1, 0.70, 0.8, 0.15])
    ax_attention_words.axis('off')

    # add image
    cmap = "binary"  #cm.get_cmap('coolwarm', 30)
    i = ax.imshow(attention_matrix,
                  interpolation='nearest',
                  cmap=cmap,
                  vmin=0,
                  vmax=1)

    # add colorbar
    cbaxes = f.add_axes([0.95, 0.3, 0.02, 0.5])
    cbar = f.colorbar(i, cax=cbaxes, orientation='vertical')
    cbar.ax.set_xlabel('Probability', labelpad=6)

    # add labels
    ax.set_yticks(range(len(question_tokens)))
    ax.set_yticklabels(question_tokens)

    ax.set_xticks(range(len(passage_tokens)))
    ax.set_xticklabels(passage_tokens, rotation=80)

    ax.set_xlabel('Passage')
    ax.set_ylabel('Question')

    ###########  GET THE MOST ATTENTION WORDS ########
    Nmax_attention_words = 3
    z = (-attention_matrix).argsort(axis=1)[:, :]

    attentioned_passage_words = []
    for i in range(len(question_tokens)):
        attentioned_passage_words.append([])
        for j in range(Nmax_attention_words):
            attentioned_passage_words[-1].append(
                passage_tokens[z[i, j]] + "(%.1f%%)" %
                (attention_matrix[i, z[i, j]] * 100))
        attentioned_passage_words[-1] = ", ".join(
            attentioned_passage_words[-1])

    text_correspondance = ""
    for i in range(len(question_tokens)):
        text_correspondance += question_tokens[
            i] + " ---> " + attentioned_passage_words[i] + "\n"

    ax_attention_words.text(0, 0, text_correspondance)
    #        ax2.yaxis.tick_right()
    #        ax2.yaxis.set_label_position("right")

    f.show()
    #        gl.set_fontSizes(ax = [ax1,ax2], title = 20, xlabel = 15, ylabel = 18,
    #                          legend = 12, xticks = 14, yticks = 14)
    gl.subplots_adjust(left=.09,
                       bottom=.10,
                       right=.90,
                       top=.95,
                       wspace=.20,
                       hspace=0.10)

    gl.savefig(image_path,
               dpi=100,
               sizeInches=[10, 6],
               close=False,
               bbox_inches="tight")
Exemple #19
0
                "", "",
                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):
Exemple #20
0
    gl.savefig(folder_images +'subplot1.png', 
               dpi = dpi, sizeInches = sizeInches)
# Subplot Type 2
if (type_graph == 2):
    ax1 = gl.subplot2grid((1,4), (0,0), rowspan=1, colspan=3)
    gl.plot(X,Y, nf = 0,
            color = "k", lw = 5, alpha = 0.7,
            labels = ["Sine chart","Time (s)", "Voltage(V)"],
            legend = ["Rolling measurement"])
#        
    ax2 = gl.subplot2grid((1,4), (0,3), rowspan=1, colspan=1)
    gl.plot(X2,Y2, nf = 0,
            color = "k", lw = 2, alpha = 0.7,
            labels = ["Discrete window","Sample (k)", "Amplitud"],
            legend = ["Window values"])
    gl.subplots_adjust(left=.1, bottom=.1, right=.90, top=.95, 
                   wspace=.40, hspace=5)      
    gl.savefig(folder_images +'subplot2.png', 
               dpi = dpi, sizeInches = sizeInches)
# Subplot Type 3
if (type_graph == 3):
    ax1 = gl.create_axes(position = [0.2, 0.2, 0.6, 0.4])
    gl.plot(X,Y, nf = 0,
            color = "k", lw = 5, alpha = 0.7,
            labels = ["Sine chart","Time (s)", "Voltage(V)"],
            legend = ["Rolling measurement"])
        
    ax2 = gl.create_axes( position = [0.3, 0.5, 0.3, 0.3])
    gl.stem(X2,Y2, nf = 0,
            color = "k", lw = 2, alpha = 0.7,
            labels = ["Discrete window","Sample (k)", "Amplitud"],
            legend = ["Window values"])
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")
def plot_multiple_iterations(Xs,mus,covs, Ks ,myDManager, logl,theta_list,model_theta_list, folder_images):
    ######## Plot the original data #####
    gl.init_figure();
    gl.set_subplots(2,3);
    Ngraph = 6
    
    colors = ["r","b","g"]
    K_G,K_W,K_vMF = Ks
    
    for i in range(Ngraph):
        indx = int(i*((len(theta_list)-1)/float(Ngraph-1)))
        nf = 1
        for xi in range(len( Xs)):
            ## First cluster
            labels = ['EM Evolution. Kg:'+str(K_G)+ ', Kw:' + str(K_W) + ', K_vMF:' + str(K_vMF), "X1","X2"]
            ax1 = gl.scatter(Xs[xi][0,:],Xs[xi][1,:],labels = ["","",""] , 
                              color = colors[xi] ,alpha = 0.2, nf = nf)
            nf =0
            mean,w,h,theta = bMA.get_gaussian_ellipse_params( mu = mus[xi], Sigma = covs[xi], Chi2val = 2.4477)
            r_ellipse = bMA.get_ellipse_points(mean,w,h,theta)
            gl.plot(r_ellipse[:,0], r_ellipse[:,1], ax = ax1, ls = "--", lw = 2
                     ,AxesStyle = "Normal2", color = colors[xi], alpha = 0.7)
            

        # Only doable if the clusters dont die
        for k_c in myDManager.clusterk_to_Dname.keys():
            k = myDManager.clusterk_to_thetak[k_c]
            distribution_name = myDManager.clusterk_to_Dname[k_c] # G W
            
            if (distribution_name == "Gaussian"):
                ## Plot the ecolution of the mu
                #### Plot the Covariance of the clusters !
                mean,w,h,theta = bMA.get_gaussian_ellipse_params( mu = theta_list[indx][k][0], Sigma = theta_list[indx][k][1], Chi2val = 2.4477)
                r_ellipse = bMA.get_ellipse_points(mean,w,h,theta)
                gl.plot(r_ellipse[:,0], r_ellipse[:,1], ax = ax1, ls = "-.", lw = 3,
                        AxesStyle = "Normal2",
                       legend = ["Kg(%i). pi:%0.2f"%(k,  float(model_theta_list[indx][0][0,k]))]) 
            
            elif(distribution_name == "Watson"):
                #### Plot the pdf of the distributino !
                ## Distribution parameters for Watson
                kappa = float(theta_list[indx][k][1])
                mu = theta_list[indx][k][0]
    
                Nsa = 1000
                # Draw 2D samples as transformation of the angle
                Xalpha = np.linspace(0, 2*np.pi, Nsa)
                Xgrid= np.array([np.cos(Xalpha), np.sin(Xalpha)])
                
                probs = []  # Vector with probabilities
                for i in range(Nsa):
                    probs.append(np.exp(Wad.Watson_pdf_log(Xgrid[:,i],[mu,kappa]) ))
                
                probs = np.array(probs)
                # Plot it in polar coordinates
                X1_w = (1 + probs) * np.cos(Xalpha)
                X2_w = (1 + probs) * np.sin(Xalpha)
                
                gl.plot(X1_w,X2_w, 
                     alpha = 1, lw = 3, ls = "-.",legend = ["Kw(%i). pi:%0.2f"%(k,  float(model_theta_list[indx][0][0,k]))]) 
                
            elif(distribution_name == "vonMisesFisher"):
                #### Plot the pdf of the distributino !
                ## Distribution parameters for Watson
                kappa = float(theta_list[indx][k][1]); mu = theta_list[indx][k][0]
                Nsa = 1000
                # Draw 2D samples as transformation of the angle
                Xalpha = np.linspace(0, 2*np.pi, Nsa)
                Xgrid= np.array([np.cos(Xalpha), np.sin(Xalpha)])
                
                probs = []  # Vector with probabilities
                for i in range(Nsa):
                    probs.append(np.exp(vMFd.vonMisesFisher_pdf_log(Xgrid[:,i],[mu,kappa]) ))
                    
                probs = np.array(probs)
                probs = probs.reshape((probs.size,1)).T
                # Plot it in polar coordinates
                X1_w = (1 + probs) * np.cos(Xalpha)
                X2_w = (1 + probs) * np.sin(Xalpha)
                
    #            print X1_w.shape, X2_w.shape
                gl.plot(X1_w,X2_w, 
                     alpha = 1, lw = 3, ls = "-.", legend = ["Kvmf(%i). pi:%0.2f"%(k,  float(model_theta_list[indx][0][0,k]))]) 
            

        ax1.axis('equal')
    gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.2, hspace=0.01)
    gl.savefig(folder_images +'Final_State2. K_G:'+str(K_G)+ ', K_W:' + str(K_W) + '.png', 
           dpi = 100, sizeInches = [18, 8])
                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")

#elif (Experiments_generate_results_data):
#    DataSet_statistics = fill_evaluation_data(model,device, dataset_iterable,num_batches, Evaluate_Model_Results, bayesian_ensemble = bayesian_ensemble)
#    EM = 100*np.mean(DataSet_statistics["em"])
#    F1 = 100*np.mean(DataSet_statistics["f1"])

#metrics, data_loss = general_validation_runner(model)
#print ("Loss validation:",data_loss)
def generate_images_iterations_ll(Xs,mus,covs, Ks ,myDManager, logl,theta_list,model_theta_list,folder_images_gif):
#    os.remove(folder_images_gif) # Remove previous images if existing
    """
    WARNING: MEANT FOR ONLY 3 Distributions due to the color RGB
    """
    import shutil
    ul.create_folder_if_needed(folder_images_gif)
    shutil.rmtree(folder_images_gif)
    ul.create_folder_if_needed(folder_images_gif)
    ######## Plot the original data #####

    Xdata = np.concatenate(Xs,axis = 1).T
    colors = ["r","b","g"]
    K_G,K_W,K_vMF = Ks
    
    ### FOR EACH ITERATION 
    for i in range(len(theta_list)):  # theta_list
        indx = i
        gl.init_figure()
        ax1 = gl.subplot2grid((1,2), (0,0), rowspan=1, colspan=1)
        
        ## Get the relative ll of the Gaussian denoising cluster.
        ll = myDManager.pdf_log_K(Xdata,theta_list[indx])
        N,K = ll.shape
#        print ll.shape
        for j in range(N):  # For every sample
        #TODO: Can this not be done without a for ?
            # Normalize the probability of the sample being generated by the clusters
            Marginal_xi_probability = gf.sum_logs(ll[j,:])
            ll[j,:] = ll[j,:]- Marginal_xi_probability
        
            ax1 = gl.scatter(Xdata[j,0],Xdata[j,1], labels = ['EM Evolution. Kg:'+str(K_G)+ ', Kw:' + str(K_W) + ', K_vMF:' + str(K_vMF), "X1","X2"], 
                              color = (np.exp(ll[j,1]), np.exp(ll[j,0]), np.exp(ll[j,2])) ,  ###  np.exp(ll[j,2])
                              alpha = 1, nf = 0)
            
        # Only doable if the clusters dont die
        for k_c in myDManager.clusterk_to_Dname.keys():
            k = myDManager.clusterk_to_thetak[k_c]
            distribution_name = myDManager.clusterk_to_Dname[k_c] # G W
            
            if (distribution_name == "Gaussian"):
                ## Plot the ecolution of the mu
                #### Plot the Covariance of the clusters !
                mean,w,h,theta = bMA.get_gaussian_ellipse_params( mu = theta_list[indx][k][0], Sigma = theta_list[indx][k][1], Chi2val = 2.4477)
                r_ellipse = bMA.get_ellipse_points(mean,w,h,theta)
                gl.plot(r_ellipse[:,0], r_ellipse[:,1], ax = ax1, ls = "-.", lw = 3,
                        AxesStyle = "Normal2",
                       legend = ["Kg(%i). pi:%0.2f"%(k,  float(model_theta_list[indx][0][0,k]))]) 
            
            elif(distribution_name == "Watson"):
                #### Plot the pdf of the distributino !
                ## Distribution parameters for Watson
                kappa = float(theta_list[indx][k][1]);  mu = theta_list[-1][k][0]
                Nsa = 1000
                # Draw 2D samples as transformation of the angle
                Xalpha = np.linspace(0, 2*np.pi, Nsa)
                Xgrid= np.array([np.cos(Xalpha), np.sin(Xalpha)])
                
                probs = []  # Vector with probabilities
                for i in range(Nsa):
                    probs.append(np.exp(Wad.Watson_pdf_log(Xgrid[:,i],[mu,kappa]) ))
                
                probs = np.array(probs)
                # Plot it in polar coordinates
                X1_w = (1 + probs) * np.cos(Xalpha)
                X2_w = (1 + probs) * np.sin(Xalpha)
                
                gl.plot(X1_w,X2_w, 
                     alpha = 1, lw = 3, ls = "-.", legend = ["Kw(%i). pi:%0.2f"%(k,  float(model_theta_list[indx][0][0,k]))]) 
                
            elif(distribution_name == "vonMisesFisher"):
                #### Plot the pdf of the distributino !
                ## Distribution parameters for Watson
                kappa = float(theta_list[indx][k][1]); mu = theta_list[indx][k][0]
                Nsa = 1000
                # Draw 2D samples as transformation of the angle
                Xalpha = np.linspace(0, 2*np.pi, Nsa)
                Xgrid= np.array([np.cos(Xalpha), np.sin(Xalpha)])
                
                probs = []  # Vector with probabilities
                for i in range(Nsa):
                    probs.append(np.exp(vMFd.vonMisesFisher_pdf_log(Xgrid[:,i],[mu,kappa]) ))
                    
                probs = np.array(probs)
                probs = probs.reshape((probs.size,1)).T
                # Plot it in polar coordinates
                X1_w = (1 + probs) * np.cos(Xalpha)
                X2_w = (1 + probs) * np.sin(Xalpha)
                
    #            print X1_w.shape, X2_w.shape
                gl.plot(X1_w,X2_w, 
                     alpha = 1, lw = 3, ls = "-.", legend = ["Kvmf(%i). pi:%0.2f"%(k,  float(model_theta_list[indx][0][0,k]))]) 
                
            
        gl.set_zoom(xlim = [-6,6], ylim = [-6,6], ax = ax1)     
        ax2 = gl.subplot2grid((1,2), (0,1), rowspan=1, colspan=1)
        if (indx == 0):
            gl.add_text(positionXY = [0.1,.5], text = r' Initilization Incomplete LogLike: %.2f'%(logl[0]),fontsize = 15)
            pass
        elif (indx >= 1):
           
            gl.plot(range(1,np.array(logl).flatten()[1:].size +1),np.array(logl).flatten()[1:(indx+1)], ax = ax2, 
                    legend = ["Iteration %i, Incom LL: %.2f"%(indx, logl[indx])], labels = ["Convergence of LL with generated data","Iterations","LL"], lw = 2)
            gl.scatter(1, logl[1], lw = 2)
            pt = 0.05
            gl.set_zoom(xlim = [0,len(logl)], ylim = [logl[1] - (logl[-1]-logl[1])*pt,logl[-1] + (logl[-1]-logl[1])*pt], ax = ax2)
            
        gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.2, hspace=0.01)
        
        gl.savefig(folder_images_gif +'gif_'+ str(indx) + '.png', 
               dpi = 100, sizeInches = [16, 8], close = "yes",bbox_inches = None)
        
        gl.close("all")
            label="train")
    plt.bar(np.arange(Nclassifiers) + 0.4,
            test_CE,
            width=0.2,
            color='r',
            align='center',
            label="test")
    plt.xticks(np.arange(Nclassifiers) + 0.3, classifiers_keys)
    #    plt.title('Classifiers CE')
    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',
Exemple #26
0
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)