-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplots.py
165 lines (142 loc) · 4.93 KB
/
plots.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 2 12:00:08 2023
@author: Tommaso Giacometti
"""
import matplotlib.pyplot as plt
import numpy as np
import torch
class Bcolors:
#Class to print on terminal with different colors
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def plot_loss(lossi : list, mean = 20, tit = None) -> None:
'''
Plot the loss history in log scale.
Parameters
----------
lossi : list, ArrayLike
mean : Optional
The plot will show the mean of the loss for this number of steps.
tit : str, optional
Title to put on the plot.
Returns
-------
Show the plot
'''
try:
lossi = np.array(lossi)
y = lossi.reshape(-1,mean).mean(axis=1)
x = np.linspace(1, len(y), num=len(y))
fig, ax = plt.subplots()
ax.plot(x,y)
if tit is None:
ax.set_title(f'Mean of {mean} losses steps')
else:
ax.set_title(tit)
ax.set_ylabel('loss')
ax.set_xlabel(f'epoch/{mean}')
ax.set_yscale('log')
plt.show()
pass
except:
print(f'{Bcolors.WARNING}WARNING : {Bcolors.ENDC}the shape of lossi is not multiple of {mean}!')
print('The loss track plot will not be shown')
pass
def plot_train_distribution_VGAE(model, data) -> None:
'''
Plot the distribution of link probability for the given edges, only for the TRAIN set.
Parameters
----------
model : VGAE (or GAE)
data : Data class according to Data_Paper or Data_Bio
Returns
-------
Show the histogram of the distribution
'''
model.eval()
with torch.no_grad():
z = model.encode(data.x, data.train_pos)
pos_out = model.decode(z, data.train_pos)
pos_out = pos_out.cpu().numpy()
neg_out = model.decode(z, data.train_neg)
neg_out = neg_out.cpu().numpy()
fig, (ax1,ax2) = plt.subplots(1,2, figsize = (9,4))
fig.suptitle('VGAE distributions')
ax1.hist(pos_out, bins = 30, label = f'{data.train_pos.shape[1]} total edges')
ax1.legend()
ax1.set_xlabel('Probability of link')
ax1.set_ylabel('Number of edges')
ax1.set_title('Positive train edges')
ax2.hist(neg_out, bins = 30, label = f'{data.test_neg.shape[1]} total edges')
ax2.legend()
ax2.set_xlabel('Probability of link')
ax2.set_title('Negative train edges')
plt.show()
pass
def plot_test_distribution_VGAE(model, data) -> None:
'''
Plot the distribution of link probability for the given edges, only for the VAL/TEST set.
Parameters
----------
model : VGAE (or GAE)
data : Data class according to Data_Paper or Data_Bio
Returns
-------
Show the histogram of the distribution
'''
model.eval()
with torch.no_grad():
z = model.encode(data.x, data.train_pos)
pos_out = model.decode(z, data.test_pos)
pos_out = pos_out.cpu().numpy()
neg_out = model.decode(z, data.test_neg)
neg_out = neg_out.cpu().numpy()
fig, (ax1,ax2) = plt.subplots(1,2, figsize = (9,4))
fig.suptitle('VGAE distributions')
ax1.hist(pos_out, bins = 30, label = f'{data.test_pos.shape[1]} total edges')
ax1.legend()
ax1.set_xlabel('Probability of link')
ax1.set_ylabel('Number of edges')
ax1.set_title('Positive test edges')
ax2.hist(neg_out, bins = 30, label = f'{data.test_neg.shape[1]} total edges')
ax2.legend()
ax2.set_xlabel('Probability of link')
ax2.set_title('Negative test edges')
plt.show()
pass
def plot_distribution_FNN(model, embedding, data, test : bool) -> None:
model.eval()
with torch.no_grad():
if test:
h_pos = torch.nn.functional.softmax(model(data.test_emb_pos), dim = 1)
h_neg = torch.nn.functional.softmax(model(data.test_emb_neg), dim = 1)
h_pos = h_pos.detach().cpu().numpy()[:,1]
h_neg = h_neg.detach().cpu().numpy()[:,1]
tit = 'test'
else:
h_pos = torch.nn.functional.softmax(model(data.train_emb_pos), dim = 1)
h_neg = torch.nn.functional.softmax(model(data.train_emb_neg), dim = 1)
h_pos = h_pos.detach().cpu().numpy()[:,1]
h_neg = h_neg.detach().cpu().numpy()[:,1]
tit = 'train'
fig, (ax1,ax2) = plt.subplots(1,2, figsize = (9,4))
fig.suptitle('FNN distributions, probability for the link to exist')
ax1.hist(h_pos, bins = 30, label = f'{len(h_pos)} total links')
ax1.legend()
ax1.set_xlabel('Probability of link')
ax1.set_ylabel('Number of edges')
ax1.set_title(f'Positive {tit} edges')
ax2.hist(h_neg, bins = 30, label = f'{len(h_neg)} total links')
ax2.legend()
ax2.set_xlabel('Probability of link')
ax2.set_title(f'Negative {tit} edges')
plt.show()
pass