-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathTri-training.py
More file actions
153 lines (88 loc) · 4.17 KB
/
Copy pathTri-training.py
File metadata and controls
153 lines (88 loc) · 4.17 KB
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
import numpy as np
import sklearn
import scipy.io as scio
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
class TriTraining:
def __init__(self, classifier):
if sklearn.base.is_classifier(classifier):
self.classifiers = [sklearn.base.clone(classifier) for i in range(3)]
else:
self.classifiers = [sklearn.base.clone(classifier[i]) for i in range(3)]
def fit(self, L_X, L_y, U_X):
for i in range(3):
sample = sklearn.utils.resample(L_X, L_y)
self.classifiers[i].fit(*sample)
e_prime = [0.5]*3
l_prime = [0]*3
e = [0]*3
update = [False]*3
Li_X, Li_y = [[]]*3, [[]]*3#to save proxy labeled data
improve = True
self.iter = 0
while improve:
self.iter += 1#count iterations
for i in range(3):
j, k = np.delete(np.array([0,1,2]),i)
update[i] = False
e[i] = self.measure_error(L_X, L_y, j, k)
if e[i] < e_prime[i]:
U_y_j = self.classifiers[j].predict(U_X)
U_y_k = self.classifiers[k].predict(U_X)
Li_X[i] = U_X[U_y_j == U_y_k]#when two models agree on the label, save it
Li_y[i] = U_y_j[U_y_j == U_y_k]
if l_prime[i] == 0:#no updated before
l_prime[i] = int(e[i]/(e_prime[i] - e[i]) + 1)
if l_prime[i] < len(Li_y[i]):
if e[i]*len(Li_y[i])<e_prime[i] * l_prime[i]:
update[i] = True
elif l_prime[i] > e[i]/(e_prime[i] - e[i]):
L_index = np.random.choice(len(Li_y[i]), int(e_prime[i] * l_prime[i]/e[i] -1))
Li_X[i], Li_y[i] = Li_X[i][L_index], Li_y[i][L_index]
update[i] = True
for i in range(3):
if update[i]:
self.classifiers[i].fit(np.append(L_X,Li_X[i],axis=0), np.append(L_y, Li_y[i], axis=0))
e_prime[i] = e[i]
l_prime[i] = len(Li_y[i])
if update == [False]*3:
improve = False#if no classifier was updated, no improvement
def predict(self, X):
pred = np.asarray([self.classifiers[i].predict(X) for i in range(3)])
pred[0][pred[1]==pred[2]] = pred[1][pred[1]==pred[2]]
return pred[0]
def score(self, X, y):
return sklearn.metrics.accuracy_score(y, self.predict(X))
def measure_error(self, X, y, j, k):
j_pred = self.classifiers[j].predict(X)
k_pred = self.classifiers[k].predict(X)
wrong_index =np.logical_and(j_pred != y, k_pred==j_pred)
#wrong_index =np.logical_and(j_pred != y_test, k_pred!=y_test)
return sum(wrong_index)/sum(j_pred == k_pred)
dataFile = './norb.mat'
data = scio.loadmat(dataFile)
traindata = np.double(data['train_x']/255)
trainlabel = np.double(data['train_y'])
testdata = np.double(data['test_x']/255)
testlabel = np.double(data['test_y'])
data = np.row_stack([traindata,testdata])
label = np.row_stack([trainlabel,testlabel]).argmax(axis=1)
train_index = np.random.choice(data.shape[0],8600,replace=False)
rest_index = list(set(np.arange(data.shape[0])) - set(train_index))
test_index = np.random.choice(rest_index,10000,replace=False)
u_index = list(set(rest_index) - set(test_index))
traindata = data[train_index]
trainlabel = label[train_index]
testdata = data[test_index]
testlabel = label[test_index]
udata = data[u_index]
print(traindata.shape,testdata.shape,udata.shape)
print(trainlabel.shape,testlabel.shape)
clf = RandomForestClassifier()
clf.fit(traindata,trainlabel)
res1 = clf.predict(testdata)
print(accuracy_score(res1,testlabel))
TT = TriTraining([RandomForestClassifier(),RandomForestClassifier(),RandomForestClassifier()])
TT.fit(traindata,trainlabel,udata)
res2 = TT.predict(testdata)
print(accuracy_score(res2,testlabel))