File size: 2,835 Bytes
ffd9d26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import matplotlib.pyplot as plt
import seaborn as sns
import time
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
import torch


#Execution duration

def time_execution(start,end):
    timespan=end-start
    minutes=timespan//60
    secondes=timespan%60
    heures=minutes//60
    minutes=minutes%60
    print(f"{int(heures)}h {int(minutes)} min {secondes} s")
    return(f"{int(heures)}h {int(minutes)} min {secondes} s")


#Graphs

def display_prediction(X_test,h,y_test,rd):
    plt.figure(figsize=[9,6])
        
    plt.scatter(X_test,h(X_test).cpu(),label="predicted values")
    plt.scatter(X_test,y_test,label="true_values")
    plt.legend()
    if rd=="final":
        plt.title("true et predicted values at the end")
    else:plt.title(f"true et predicted values after {rd} rounds")
    

def display_chosen_labelled_datas_PCA(X_train,idx_lb,y_train,b_idxs,rd):
    
    pca = PCA(n_components=2)
    transformed = pca.fit_transform(X=X_train)
    x_component = transformed[:, 0]
    
    plt.figure(figsize=[9,6])
    plt.scatter(transformed[:, 0][~idx_lb],transformed[:, 1][~idx_lb],label="unlabelled points",c="brown")
    plt.scatter(transformed[:, 0][idx_lb],transformed[:, 1][idx_lb],label="labelled points")
    plt.scatter(transformed[:, 0][b_idxs],transformed[:, 1][b_idxs],label="new points added",c="yellow")
    plt.legend()
    plt.title(f"points selected after {rd} rounds")
    
def display_chosen_labelled_datas(X_train,idx_lb,y_train,b_idxs,rd):
    plt.figure(figsize=[9,6])
    
    plt.scatter(X_train[~idx_lb],y_train[~idx_lb],label="unlabelled points",c="brown")   
    plt.scatter(X_train[idx_lb],y_train[idx_lb],label="labelled points")
    plt.scatter(X_train[b_idxs],y_train[b_idxs],label="new points added",c="yellow")
    plt.legend()
    plt.title(f"points selected after {rd} rounds")
        
def display_loss_t1(t1_descend,rd):
    plt.figure(figsize=[9,6])
    plt.plot(t1_descend)
    plt.xlabel("batch")
    plt.title(f"t1 loss evolution each batch after {rd} rounds")
    

def display_loss_t2(t2_ascend,rd):
    plt.figure(figsize=[9,6])
    plt.plot(t2_ascend)
    plt.xlabel("batch")
    plt.title(f"t2 loss evolution each batch after {rd} rounds")
    
def display_phi(X_train,phi,rd=None):
    plt.figure(figsize=[9,6])
    plt.scatter(X_train,phi(X_train))
    plt.xlabel("X_train")
    plt.title(f"phi function on the full trainset after {rd} rounds")
    

#Metrics        
        
def MAPE(X_test,y_test,h):
    acc_per_i=sum(abs(h(X_test)-y_test)/abs(y_test))
    acc_per_i = acc_per_i[0]/len(y_test)
    return acc_per_i    


def MAE(X_test,y_test,h):
    acc_i = sum(abs((h(X_test)-y_test)))
    acc_i = acc_i[0]/len(y_test)
    return acc_i

def RMSE(X_test,y_test,h):
    acc_i = ((h(X_test)-y_test)**2).mean()
    return torch.sqrt(acc_i)