-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_compare_theta.py
More file actions
72 lines (54 loc) · 1.96 KB
/
plot_compare_theta.py
File metadata and controls
72 lines (54 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
This function plots superimposed accuracy plots from all subfolders of an input folder.
It is used to compare accuacy for different plasticity hyperparameters.
"""
import os
import matplotlib.pyplot as plt
import numpy as np
def comp_moving_avg(vector, period):
"""
Compute moving average.
"""
ret = np.cumsum(vector, dtype=float)
ret[period:] = ret[period:] - ret[:-period]
return ret[period - 1:] / period
def read_theta(file_path):
"""
Read plasticity params from arg file.
"""
with open(file_path, 'r') as file:
content = file.read()
theta_start = content.find('Theta : [') + len('Theta : [')
theta_end = content.find(']', theta_start)
theta_str = content[theta_start:theta_end]
theta_list = theta_str.split(', ')
theta = [float(value) for value in theta_list]
return theta
def plot_acc(folder_name, per):
"""
Plot accuracies for each subfolder in the given folder.
"""
# -- set colors
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'teal', 'purple', 'pink', 'brown', 'gray',
'cyan', 'magenta', 'lime', 'indigo', 'violet', 'maroon', 'olive', 'navy', 'silver', 'gold']
# -- Find all subfolders within the given folder
subfolders = np.sort([f.path for f in os.scandir(folder_name) if f.is_dir()])
# -- Iterate over each subfolder
for i, subfolder in enumerate(subfolders):
# -- load data
z = comp_moving_avg(np.loadtxt(f'{subfolder}/acc.txt'), period=per)
theta = read_theta(f'{subfolder}/args.txt')
# -- plot acc
plt.plot(np.array(range(len(z))) + int((per - 1) / 2), z, c=colors[i], label=theta)
# -- plot settings
plt.title('Accuracy')
plt.xlabel('Training data')
plt.ylabel('Accuracy')
plt.xlim([-10, 5110])
plt.ylim([0, 1])
plt.legend()
# -- visualize
plt.show()
# Example usage
folder_name = './results/Baseline/Test 4'
plot_acc(folder_name, per=20)