-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplotting.py
120 lines (109 loc) · 4.8 KB
/
plotting.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import numpy as np
import scipy as sp
import matplotlib as mpl
import matplotlib.pyplot as plt
import torch
def plot_particles(X, target, grad, target_name, img_name, folder_name, arrows):
d = X.shape[1]
if d == 2:
plt.figure()
plt.plot(target[:, 1], target[:, 0], '.', c='orange', ms=2)
plt.plot(X[:, 1], X[:, 0], 'b.', ms=2)
if arrows:
minus_grad = - grad.cpu()
plt.quiver(X[:, 1], X[:, 0], minus_grad[:, 1], minus_grad[:, 0], angles='xy', scale_units='xy', scale=1)
if target_name == 'circles':
plt.ylim([-1.0, 1.0])
plt.xlim([-2.0, 0.5])
if target_name == 'bananas':
plt.ylim([-7.5, 7.5])
plt.xlim([-5.0, 10.0])
if target_name == 'four_wells':
plt.xlim([-2.5, 7.0])
plt.ylim([-2.5, 7.0])
plt.gca().set_aspect('equal')
plt.axis('off')
plt.savefig(folder_name + img_name, dpi=300, bbox_inches='tight')
plt.close()
elif d == 3:
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection="3d")
fig.add_axes(ax)
ax.view_init(azim=-66, elev=12)
ax.scatter(target[:, 0], target[:, 1], target[:, 2], c='orange', s=2)
ax.scatter(X[:, 0], X[:, 1], X[:, 2], 'b.', s=2)
plt.savefig(folder_name + img_name, dpi=300, bbox_inches='tight')
plt.close()
def plot_lambdas(lambdas, lower_bds_lambd, plot_name, folder_name):
fig, ax = plt.subplots()
plt.plot(lambdas, label=r'$\lambda$')
plt.plot(lower_bds_lambd, label=r'lower bound on $\lambda$')
plt.gca().yaxis.set_minor_locator(plt.LogLocator(base=10.0, subs=(0.2, 0.4, 0.6, 0.8)))
plt.yscale('log')
plt.xlabel('iterations')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.legend(frameon=False)
plt.savefig(folder_name + plot_name, dpi=300, bbox_inches='tight')
plt.close()
def plot_gaps(duality_gaps, relative_duality_gaps, plot_name, folder_name):
fig, ax = plt.subplots()
plt.plot(duality_gaps, label='duality gap')
plt.plot(relative_duality_gaps, '-.', label='relative duality gap')
plt.axhline(y=1e-2, linestyle='--', color='gray', label='tolerance')
plt.gca().yaxis.set_minor_locator(plt.LogLocator(base=10.0, subs=(0.2, 0.4, 0.6, 0.8)))
plt.yscale('log')
plt.xlabel('iterations')
plt.legend()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.savefig(folder_name + plot_name, dpi=300, bbox_inches='tight')
plt.close()
def plot_all_gaps(relative_pseudo_duality_gaps, pseudo_duality_gaps, plot_name, folder_name):
fig, ax = plt.subplots()
plt.plot(pseudo_duality_gaps, ':', label='pseudo duality gap')
plt.plot(relative_pseudo_duality_gaps, label='relative pseudo-duality gap')
plt.axhline(y=1e-2, linestyle='--', color='gray', label='tolerance')
plt.gca().yaxis.set_minor_locator(plt.LogLocator(base=10.0, subs=(0.2, 0.4, 0.6, 0.8)))
plt.yscale('log')
plt.xlabel('iterations')
plt.legend()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.savefig(folder_name + plot_name, dpi=300, bbox_inches='tight')
plt.close()
def plot_W2(W2, plot_name, folder_name):
fig, ax = plt.subplots()
plt.plot(W2)
plt.yscale('log')
plt.gca().yaxis.set_minor_locator(plt.LogLocator(base=10.0, subs=(0.2, 0.4, 0.6, 0.8)))
plt.xlabel('iterations')
plt.ylabel(r'$W_2(\mu, \nu)$')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.savefig(folder_name + plot_name, dpi=300, bbox_inches='tight')
plt.close()
def plot_func_values(primals, duals, lambd, plot_name, folder_name):
fig, ax = plt.subplots()
plt.plot(primals, '--', label='primal objective')
plt.plot(duals, '--', label='dual objective')
plt.yscale('log')
plt.gca().yaxis.set_minor_locator(plt.LogLocator(base=10.0, subs=(0.2, 0.4, 0.6, 0.8)))
plt.xlabel('iterations')
plt.ylabel(r'$D_{f_{\alpha}}^{{' + str(lambd) + r'}}(\mu \mid \nu)$')
plt.legend(frameon=False)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.savefig(folder_name + plot_name, dpi=300, bbox_inches='tight')
plt.close()
def plot_MMD(MMD, plot_name, folder_name):
fig, ax = plt.subplots()
plt.plot(MMD)
plt.xlabel('iterations')
plt.ylabel(r'$\frac{1}{2} d_{K}(\mu, \nu)^2$')
plt.yscale('log')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.gca().yaxis.set_minor_locator(plt.LogLocator(base=10.0, subs=(0.2, 0.4, 0.6, 0.8)))
plt.savefig(folder_name + plot_name, dpi=300, bbox_inches='tight')
plt.close()