Skip to content
Snippets Groups Projects
Commit 4a5324ba authored by Léo Bouscarrat's avatar Léo Bouscarrat
Browse files

Add elements on requirements.txt

parent d89d9d52
No related branches found
No related tags found
2 merge requests!5Resolve "Add plots",!3clean scripts
import matplotlib.pyplot as plt
import numpy as np
from sklearn.neighbors.kde import KernelDensity
import pandas as pd
class Plotter(object):
@staticmethod
def weight_density(weights, X, file_path):
X_plot = [np.exp(elem) for elem in weights]
def weight_density(all_experiment_weights, file_path):
'''
Function that creates the figure with the density of the weights
:param all_experiment_weights: The weights for the different experiments
:param file path: str, path where the figure will be saved
'''
fig, ax = plt.subplots()
for weights in all_experiment_weights.values():
pd.Series([weight for weight in weights if weight != 0]).plot.kde(figsize=(15, 10), ax=ax)
for kernel in ['gaussian', 'tophat', 'epanechnikov']:
kde = KernelDensity(kernel=kernel, bandwidth=0.5).fit(X_plot)
log_dens = kde.score_samples(X_plot)
ax.plot(X_plot, np.exp(log_dens), '-',
label="kernel = '{0}'".format(kernel))
ax.legend(loc='upper left')
ax.plot(X[:, 0], -0.005 - 0.01 * np.random.random(X.shape[0]), '+k')
ax.set_xlim(-4, 9)
ax.set_ylim(-0.02, 0.4)
legends = ['Experience ' + str(i+1) for i in range(len(all_experiment_weights))]
ax.legend(legends)
fig.savefig(file_path, dpi=fig.dpi)
fig.title('Density weights of the OMP')
plt.close(fig)
@staticmethod
......
......@@ -59,6 +59,8 @@ if __name__ == "__main__":
experiment_dev_scores = dict()
experiment_test_scores = dict()
experiment_weights = dict()
# Used to check if all losses were computed using the same metric (it should be the case)
experiment_score_metrics = list()
......@@ -74,6 +76,8 @@ if __name__ == "__main__":
experiment_dev_scores[seed] = list()
experiment_test_scores[seed] = list()
experiment_weights[seed] = list()
# List the forest sizes in models/{experiment_id}/seeds/{seed}/extracted_forest_size
extracted_forest_sizes = os.listdir(extracted_forest_size_root_path)
for extracted_forest_size in extracted_forest_sizes:
......@@ -84,9 +88,13 @@ if __name__ == "__main__":
# Load [...]/model_parameters.json file and build the model using these parameters and the weights and forest from model_raw_results.pickle
model = ModelFactory.load(dataset.task, extracted_forest_size_path, experiment_id, model_raw_results)
# Save temporarly some raw results (TODO: to complete to retreive more results)
# Save the scores
experiment_train_scores[seed].append(model_raw_results.train_score)
experiment_dev_scores[seed].append(model_raw_results.dev_score)
experiment_test_scores[seed].append(model_raw_results.test_score)
# Save the weights
experiment_weights[seed].append(model_raw_results.weights)
# Save the metric
experiment_score_metrics.append(model_raw_results.score_metric)
if len(set(experiment_score_metrics)) > 1:
......@@ -107,3 +115,10 @@ if __name__ == "__main__":
all_labels=['train', 'dev', 'test'],
title='Loss values of the trained model'
)
# Plot the density of the weights
Plotter.weight_density(
file_path=args.results_dir + os.sep + experiment_id + os.sep + 'density_weight.png',
all_experiment_weights=experiment_weights
)
......@@ -11,4 +11,5 @@ python-dotenv>=0.5.1
scikit-learn
python-dotenv
tqdm
matplotlib
\ No newline at end of file
matplotlib
pandas
\ No newline at end of file
results/1/losses.png

24.7 KiB

results/2/losses.png

24 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment