Пример #1
0
def generate_plot(logging,
                  ex_name,
                  ex_instance,
                  title,
                  x_label,
                  y_label,
                  data,
                  transpose=False,
                  cmap=None):
    """

    :param logging: Logging object.
    :param ex_name: Experience name.
    :param ex_instance: Experience instance.
    :param title: Plot's title.
    :param x_label: X axis label.
    :param y_label: Y axis label.
    :param data: Data to display.
    :param transpose: Transpose the data.
    :param cmap: Color map.
    """
    plot = RCNLPPlotGenerator(title=ex_name, n_plots=1)
    plot.add_sub_plot(title=ex_instance + ", " + title,
                      x_label=x_label,
                      y_label=y_label)
    if transpose:
        plot.imshow(np.transpose(data), cmap=cmap)
    else:
        plot.imshow(data, cmap=cmap)
    # end if
    logging.save_plot(plot)
Пример #2
0
def pca_reduction(doc, title, ncomponents):
    # PCA
    pca = PCA(n_components=ncomponents)
    pca.fit(doc)

    # Generate PCA
    reduced = pca.transform(doc)

    # Show
    plot = RCNLPPlotGenerator(title=ex_name, n_plots=2)
    plot.add_sub_plot(title=ex_instance + ", " + title + " inputs",
                      x_label="Time",
                      y_label="Inputs")
    plot.imshow(np.transpose(doc), cmap='Greys')
    plot.add_sub_plot(title=ex_instance + ", " + title + " reduced inputs",
                      x_label="Time",
                      y_label="Inputs")
    plot.imshow(np.transpose(reduced), cmap='Greys')
    logging.save_plot(plot)
Пример #3
0
def save_pca_image(logging, reduced1, reduced2, index1, index2):
    """
    Save the PCA images.
    :param logging: Logging object.
    :param reduced1: Reduced states of first author.
    :param reduced2: Reduced states of second author.
    :param index1: Index of the first component.
    :param index2: Index of the second component.
    """
    # Total data
    data = np.vstack(
        (reduced1[:, index1:index2 + 1], reduced2[:, index1:index2 + 1]))

    # Compute K-Means with K = 2
    centroids, _ = kmeans(data, 2)

    # Generate PCA image for 1th and 2th
    image = generate_pca_image(reduced1, reduced2, index1, index2, centroids)
    plot = RCNLPPlotGenerator(title="PCA Images", n_plots=1)
    plot.add_sub_plot(title="PCA",
                      x_label="Principal component %d" % index1,
                      y_label="Principal component %d" % index2)
    plot.imshow(image)
    logging.save_plot(plot)
    del plot
Пример #4
0
def save_pca_image(reduced1, reduced2, index1, index2):
    # Total data
    data = np.vstack(
        (reduced1[:, index1:index2 + 1], reduced2[:, index1:index2 + 1]))

    # Compute K-Means with K = 2
    centroids, _ = kmeans(data, 2)

    # Generate PCA image for 1th and 2th
    image = generate_pca_image(reduced1, reduced2, index1, index2, centroids)
    plot = RCNLPPlotGenerator(title=ex_name, n_plots=1)
    plot.add_sub_plot(title=ex_instance + ", PCA",
                      x_label="Principal component %d" % index1,
                      y_label="Principal component %d" % index2)
    plot.imshow(image)
    logging.save_plot(plot)
    del plot
Пример #5
0
        # The observation
        observation = test_out[pos]

        # Prediction 2
        prediction2 = predictions2[pos]

        # The input
        system_input = test_in[pos]

        # Final  outputs
        final_output1 = final_outputs1[pos]
        final_output2 = final_outputs2[pos]

        # Plot pred and bos
        plot = RCNLPPlotGenerator(title=ex_name, n_plots=2)

        # First subplot
        plot.add_sub_plot(title=ex_instance + ", leak rate = 0.9", x_label="Steps", y_label="Values", ylim=[-1.2, 1.2])
        plot.plot(y=observation, label="Target signal", subplot=1, color='b')
        plot.plot(y=prediction1, label="ESN output", subplot=1, color='r')
        plot.add_hline(value=rc_threshold, length=ds_sample_length, subplot=1)
        plot.add_hline(value=-rc_threshold, length=ds_sample_length, subplot=1)

        # First subplot
        plot.add_sub_plot(title=ex_instance + ", leak rate = 0.05", x_label="Steps", y_label="Values", ylim=[-1.2, 1.2])
        plot.plot(y=observation, label="Target signal", subplot=2, color='b')
        plot.plot(y=prediction2, label="ESN output", subplot=2, color='r')
        plot.add_hline(value=rc_threshold, length=ds_sample_length, subplot=2)
        plot.add_hline(value=-rc_threshold, length=ds_sample_length, subplot=2)
Пример #6
0
        doc_success_rate_avg = np.append(
            doc_success_rate_avg,
            np.average(training_size_average_doc_success_rate))
        doc_success_rate_std = np.append(
            doc_success_rate_std,
            np.std(training_size_average_doc_success_rate))
        sen_success_rate_avg = np.append(
            sen_success_rate_avg,
            np.average(training_size_average_sen_success_rate))
        sen_success_rate_std = np.append(
            sen_success_rate_std,
            np.std(training_size_average_sen_success_rate))
    # end for

    # Plot perfs
    plot = RCNLPPlotGenerator(title=ex_name, n_plots=1)
    plot.add_sub_plot(title=ex_instance + ", success rates vs training size.",
                      x_label="Nb. text file",
                      y_label="Success rates",
                      ylim=[-10, 120])
    plot.plot(y=doc_success_rate_avg,
              x=training_set_sizes,
              yerr=doc_success_rate_std,
              label="Doc. success rate",
              subplot=1,
              marker='o',
              color='b')
    plot.plot(y=sen_success_rate_avg,
              x=training_set_sizes,
              yerr=sen_success_rate_std,
              label="Sen. success rate",
Пример #7
0
                               locals()))
    logging.save_globals()
    logging.save_variables(locals())

    # Main
    results = []
    for leak_rate in a_leak_rate:
        print("Computing average state ratio for leak rate = %f" % leak_rate)
        results += [
            main(word_sparsity=rc_word_sparsity,
                 size=rc_size,
                 input_scaling=rc_input_scaling,
                 leak_rate=leak_rate,
                 spectral_radius=rc_spectral_radius,
                 w_sparsity=rc_w_sparsity)
        ]
    # end for

    # Plot pred and bos
    plot = RCNLPPlotGenerator(title=ex_name, n_plots=1)
    plot.add_sub_plot(title=ex_instance + ", explore leak rate",
                      x_label="Leak rate",
                      y_label="Average state ratio")
    plot.plot(x=a_leak_rate, y=results, label="Average state ratio", subplot=1)
    plot.add_hline(value=50, length=53622, subplot=1)
    logging.save_plot(plot)

    # Open logging dir
    logging.open_dir()

# end if
Пример #8
0
def main():
    # Create a reservoir
    flow = create_reservoir(45, rc_word_sparsity, rc_size, rc_input_scaling,
                            rc_leak_rate, rc_spectral_radius, rc_w_sparsity)

    # Generate states for first author
    for index, text_file in enumerate(os.listdir(args.author1)):
        print("Generating state for author 1 from file %s" %
              os.path.join(args.author1, text_file))
        if index == 0:
            state1 = generate_reservoir_states(
                flow, os.path.join(args.author1, text_file), args.startup)
        else:
            state1 = np.vstack((state1,
                                generate_reservoir_states(
                                    flow, os.path.join(args.author1,
                                                       text_file),
                                    args.startup)))
        # end if
        if index == args.nfile:
            break
            # end if
    # end for

    # Display reservoir states for author 1
    plot = RCNLPPlotGenerator(title=ex_name, n_plots=1)
    plot.add_sub_plot(title=ex_instance + ", Reservoir states for Author 1",
                      x_label="Time",
                      y_label="Neurons")
    plot.imshow(np.transpose(state1), cmap='Greys')
    logging.save_plot(plot)
    print("Dimensions of states for author 1 : " + str(state1.shape))

    # Generate states for second author
    for index, text_file in enumerate(os.listdir(args.author2)):
        print("Generating state for author 2 from file %s" %
              os.path.join(args.author2, text_file))
        if index == 0:
            state2 = generate_reservoir_states(
                flow, os.path.join(args.author2, text_file), args.startup)
        else:
            state2 = np.vstack((state2,
                                generate_reservoir_states(
                                    flow, os.path.join(args.author2,
                                                       text_file),
                                    args.startup)))
        # end if
        if index == args.nfile:
            break
            # end if
    # end for

    # Display reservoir states for author 2
    plot = RCNLPPlotGenerator(title=ex_name, n_plots=1)
    plot.add_sub_plot(title=ex_instance + ", Reservoir states for Author 2",
                      x_label="Time",
                      y_label="Neurons")
    plot.imshow(np.transpose(state2), cmap='Greys')
    logging.save_plot(plot)
    print("Dimensions of states for author 2 : " + str(state2.shape))

    # Same size for each authors
    if state1.shape[0] > state2.shape[0]:
        state1 = state1[:state2.shape[0]]
        sample_size = state1.shape[0]
    elif state2.shape[0] > state1.shape[0]:
        state2 = state2[:state1.shape[0]]
        sample_size = state2.shape[0]
    # end if

    # Join states
    join_states = np.vstack((state1, state2))
    plot = RCNLPPlotGenerator(title=ex_name, n_plots=1)
    plot.add_sub_plot(title=ex_instance + ", Joined Reservoir states",
                      x_label="Time",
                      y_label="Neurons")
    plot.imshow(np.transpose(join_states), cmap='Greys')
    logging.save_plot(plot)
    print("Dimensions of joined states : " + str(join_states.shape))

    # PCA
    pca = PCA(n_components=args.ncomponents)
    pca.fit(join_states)

    # Generate PCA
    reduced1 = pca.transform(state1)
    reduced2 = pca.transform(state2)
    treduced = pca.transform(join_states)

    # Generate PCA image for 1th and 2th
    for c in np.arange(0, 8):
        save_pca_image(reduced1, reduced2, c, c + 1)
    # end for

    # Get centroids for the whole states and for components 1 and 2
    centroids, _ = kmeans(join_states, 2)
    rcentroids12, _ = kmeans(treduced[:, 0:2], 2)
    rcentroids15, _ = kmeans(treduced[:, 0:5], 2)

    # Assign each sample to a cluster
    idx, _ = vq(join_states, centroids)
    ridx12, _ = vq(treduced[:, 0:2], rcentroids12)
    ridx15, _ = vq(treduced[:, 0:5], rcentroids15)

    # Compute average state success rate
    logging.save_results("Average state ratio",
                         get_average_state_success_rate(idx, sample_size),
                         display=True)
    logging.save_results("PCA12 Average state ratio",
                         get_average_state_success_rate(ridx12, sample_size),
                         display=True)
    logging.save_results("PCA15 Average state ratio",
                         get_average_state_success_rate(ridx15, sample_size),
                         display=True)

    # Open logging dir
    logging.open_dir()
        success_rate_avg = np.append(
            success_rate_avg, np.average(reservoir_size_average_success_rate))
        success_rate_std = np.append(
            success_rate_std, np.std(reservoir_size_average_success_rate))
    # end for

    for index, success_rate in enumerate(success_rate_avg):
        print("(%d, %f)" % (reservoir_sizes[index], success_rate))
    # end for

    for index, success_rate in enumerate(success_rate_std):
        print("(%d, %f)" % (reservoir_sizes[index], success_rate))
    # end for

    # Plot perfs
    plot = RCNLPPlotGenerator(title=ex_name, n_plots=1)
    plot.add_sub_plot(title=ex_instance + ", success rates vs training size.",
                      x_label="Nb. tokens",
                      y_label="Success rates",
                      ylim=[-10, 120])
    plot.plot(y=success_rate_avg,
              x=reservoir_sizes,
              yerr=success_rate_std,
              label="Success rate",
              subplot=1,
              marker='o',
              color='b')
    logging.save_plot(plot)

    # Open logging dir
    logging.open_dir()
Пример #10
0
        # Average for this value
        parameter_remembering_rates += [np.average(sample_remembering_rate)]
        parameter_lucidity += [np.average(sample_lucidity)]

        # Error for this value
        parameter_remembering_rates_std += [np.std(sample_remembering_rate)]
        parameter_lucidity_std += [np.std(sample_lucidity)]

    # Log results
    logging.save_results("Remembering rates",
                         parameter_remembering_rates,
                         display=False)
    logging.save_results("Lucidity", parameter_lucidity, display=False)

    # Plot perfs
    plot = RCNLPPlotGenerator(title=ex_name, n_plots=1)

    # First subplot
    plot.add_sub_plot(title=ex_instance + ", sparsity 0.05 to 1.0, 2 dim.",
                      x_label="Inputs sparsity",
                      y_label="Percentage",
                      ylim=[-10, 120])
    plot.plot(y=parameter_remembering_rates,
              x=rc_sparsity,
              yerr=parameter_remembering_rates_std,
              label="Remembering rate",
              subplot=1,
              marker='o',
              color='b')
    plot.plot(y=parameter_lucidity,
              x=rc_sparsity,
        logging.save_results("Overall std precision",
                             np.std(results),
                             display=True)
        logging.save_results("T-test",
                             ttest_1samp(results, 50.0) * 100.0,
                             display=True)

        # Save
        explore_results = np.append(explore_results, np.average(results))
        explore_deviation = np.append(explore_deviation, np.std(results))
        explore_t_test = np.append(explore_t_test,
                                   ttest_1samp(results, 50.0) * 100.0)
    # end for

    # First subplot
    plot = RCNLPPlotGenerator(title=ex_name, n_plots=1)
    plot.add_sub_plot(title="Explorer",
                      x_label="Leak rate",
                      y_label="Precision",
                      ylim=[0, 100],
                      xlim=[0.0, 1.0])
    plot.plot(y=explore_results,
              x=a_leak_rate,
              yerr=explore_deviation,
              label="Precision",
              subplot=1,
              marker='o',
              color='b')
    plot.plot(y=explore_t_test,
              x=a_leak_rate,
              label="T-Test",