-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathplot_learning.py
More file actions
executable file
·101 lines (86 loc) · 3.25 KB
/
Copy pathplot_learning.py
File metadata and controls
executable file
·101 lines (86 loc) · 3.25 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/python
"""
This module provides a callable for easy evaluation of stored models.
"""
import sys
import numpy as np
from pystruct.utils import SaveLogger
def main():
argv = sys.argv
print("loading %s ..." % argv[1])
ssvm = SaveLogger(file_name=argv[1]).load()
plot_learning(ssvm)
def plot_learning(ssvm, time=True):
"""Plot optimization curves and cache hits.
Create a plot summarizing the optimization / learning process of an SSVM.
It plots the primal and cutting plane objective (if applicable) and also
the target loss on the training set against training time.
For one-slack SSVMs with constraint caching, cached constraints are also
contrasted against inference runs.
Parameters
-----------
ssvm : object
SSVM learner to evaluate. Should work with all learners.
time : boolean, default=True
Whether to use wall clock time instead of iterations as the x-axis.
Notes
-----
Warm-starting a model might mess up the alignment of the curves.
So if you warm-started a model, please don't count on proper alignment
of time, cache hits and objective.
"""
import matplotlib.pyplot as plt
print(ssvm)
if hasattr(ssvm, 'base_ssvm'):
ssvm = ssvm.base_ssvm
print("Iterations: %d" % len(ssvm.objective_curve_))
print("Objective: %f" % ssvm.objective_curve_[-1])
inference_run = None
if hasattr(ssvm, 'cached_constraint_'):
inference_run = ~np.array(ssvm.cached_constraint_)
print("Gap: %f" %
(np.array(ssvm.primal_objective_curve_)[inference_run][-1] -
ssvm.objective_curve_[-1]))
if hasattr(ssvm, "loss_curve_"):
n_plots = 2
fig, axes = plt.subplots(1, 2)
else:
n_plots = 1
fig, axes = plt.subplots(1, 1)
axes = [axes]
if time and hasattr(ssvm, 'timestamps_'):
print("loading timestamps")
inds = np.array(ssvm.timestamps_)
inds = inds[2:len(ssvm.objective_curve_) + 1] / 60.
inds = np.hstack([inds, [inds[-1]]])
axes[0].set_xlabel('training time (min)')
else:
inds = np.arange(len(ssvm.objective_curve_))
axes[0].set_xlabel('QP iterations')
axes[0].set_title("Objective")
axes[0].plot(inds, ssvm.objective_curve_, label="dual")
axes[0].set_yscale('log')
if hasattr(ssvm, "primal_objective_curve_"):
axes[0].plot(inds, ssvm.primal_objective_curve_,
label="cached primal" if inference_run is not None
else "primal")
if inference_run is not None:
inference_run = inference_run[:len(ssvm.objective_curve_)]
axes[0].plot(inds[inference_run],
np.array(ssvm.primal_objective_curve_)[inference_run],
'o', label="primal")
axes[0].legend()
if n_plots == 2:
if time and hasattr(ssvm, "timestamps_"):
axes[1].set_xlabel('training time (min)')
else:
axes[1].set_xlabel('QP iterations')
try:
axes[1].plot(inds[::ssvm.show_loss_every], ssvm.loss_curve_)
except:
axes[1].plot(ssvm.loss_curve_)
axes[1].set_title("Training Error")
axes[1].set_yscale('log')
plt.show()
if __name__ == "__main__":
main()