-
Notifications
You must be signed in to change notification settings - Fork 12
/
inference_utils.py
45 lines (38 loc) · 1.63 KB
/
inference_utils.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
import os
import numpy as np
import math
import scipy
import sys
def inference_via_confidence(confidence_mtx1, confidence_mtx2, label_vec1, label_vec2):
#----------------First step: obtain confidence lists for both training dataset and test dataset--------------
confidence1 = []
confidence2 = []
acc1 = 0
acc2 = 0
for num in range(confidence_mtx1.shape[0]):
confidence1.append(confidence_mtx1[num,label_vec1[num]])
if np.argmax(confidence_mtx1[num,:]) == label_vec1[num]:
acc1 += 1
for num in range(confidence_mtx2.shape[0]):
confidence2.append(confidence_mtx2[num,label_vec2[num]])
if np.argmax(confidence_mtx2[num,:]) == label_vec2[num]:
acc2 += 1
confidence1 = np.array(confidence1)
confidence2 = np.array(confidence2)
print('model accuracy for training and test-', (acc1/confidence_mtx1.shape[0], acc2/confidence_mtx2.shape[0]) )
#sort_confidence = np.sort(confidence1)
sort_confidence = np.sort(np.concatenate((confidence1, confidence2)))
max_accuracy = 0.5
best_precision = 0.5
best_recall = 0.5
for num in range(len(sort_confidence)):
delta = sort_confidence[num]
ratio1 = np.sum(confidence1>=delta)/confidence_mtx1.shape[0]
ratio2 = np.sum(confidence2>=delta)/confidence_mtx2.shape[0]
accuracy_now = 0.5*(ratio1+1-ratio2)
if accuracy_now > max_accuracy:
max_accuracy = accuracy_now
best_precision = ratio1/(ratio1+ratio2)
best_recall = ratio1
print('membership inference accuracy is:', max_accuracy)
return max_accuracy