-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.py
74 lines (60 loc) · 2.6 KB
/
evaluation.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
import numpy as np
from sklearn.svm import SVC
from utils import davies_bouldin_index, distances_between_centers
from plotting import plot_2d, plot_3d
def evaluate(dataset, train_x, train_y, test_x, test_y,
qNode, params, step, classes, output_qubits,
show=False, save=True, cont=True):
svm = SVC(kernel="linear")
if dataset == "mnist" or dataset == "mnist_ae":
clf = svm.fit([qNode(params, x) for x in train_x],
[np.argmax(y) for y in train_y]
)
test_data = [qNode(params, x) for x in test_x]
test_target = [np.argmax(y) for y in test_y]
accuracy = clf.score(test_data, test_target)
else:
clf = svm.fit([qNode(params, x) for x in train_x],
train_y
)
test_data = [qNode(params, x) for x in test_x]
accuracy = clf.score(test_data, test_y)
print("Accuracy", accuracy)
# this will store:
# label (0 - len(CLASSES)), x_output, y_output, distance_to_center
values = np.zeros((len(test_y), output_qubits+2))
# store label and output
for index, (label, datum) in enumerate(zip(test_y, test_x)):
output = qNode(params, datum)
values[index, 0] = np.argmax(label) if "mnist" in dataset \
else label
for i in range(len(output)):
values[index, 1+i] = output[i]
# calculate centers, key is label
centers = {}
for cls in range(len(classes)):
rows = np.where(values[:, 0] == cls)
center = np.average(values[rows][:, 1:(1+output_qubits)], axis=0)
centers[cls] = center
# calculate distance to center
for i in range(len(test_y)):
values[i, -1] = np.linalg.norm(values[i, 1:(1+output_qubits)]
- centers[int(values[i, 0])])
c_distances = distances_between_centers(centers)
print("Distances between centers\n", c_distances)
dbi = davies_bouldin_index(len(classes), values, c_distances)
print("Davies Bouldin Index:", dbi)
if show or save:
if output_qubits == 2:
plot_2d(classes, values, centers, step, clf,
accuracy, show, save, cont, dbi)
elif output_qubits == 3:
plot_3d(classes, values, centers, step, accuracy, show, save, dbi)
return accuracy, dbi
def evaluate_classical(test_x, test_y, qNode, params):
correct = 0
for x, y in zip(test_x, test_y):
prediction = qNode(params, x)
if np.argmax(prediction[:, 1]) == np.argmax(y):
correct += 1
return correct/len(test_y)