-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_mnist.py
131 lines (113 loc) · 3.36 KB
/
test_mnist.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
import torch
import numpy as np
from torch import nn, optim
from dataset import load_dataset
from train_pu import train_model
def main():
iters = 10 # Run multiple experiments to get average results
pdata = 1000
epochs = 100
batch_size = 1000
seed = 2018
use_gpu = torch.cuda.is_available()
loss_pu = np.zeros((iters, epochs))
est_error_pu = np.zeros((iters, epochs))
est_error_pubp = np.zeros((iters, epochs))
est_precision_pu = np.zeros((iters, epochs))
est_recall_pu = np.zeros((iters, epochs))
est_precision_pubp = np.zeros((iters, epochs))
est_recall_pubp = np.zeros((iters, epochs))
x_train, y_train, x_test, y_test = load_dataset("mnist")
for i in range(iters):
print("Experiment:", i)
np.random.seed(seed)
pn_model = nn.Sequential(
nn.Linear(784, 300, bias=False),
nn.BatchNorm1d(300),
nn.ReLU(),
nn.Linear(300, 300, bias=False),
nn.BatchNorm1d(300),
nn.ReLU(),
nn.Linear(300, 300, bias=False),
nn.BatchNorm1d(300),
nn.ReLU(),
nn.Linear(300, 300, bias=False),
nn.BatchNorm1d(300),
nn.ReLU(),
nn.Linear(300, 1),
)
pn_optimizer = optim.Adam(pn_model.parameters(), lr=1e-5)
pu_model = nn.Sequential(
nn.Linear(784, 300, bias=False),
nn.BatchNorm1d(300),
nn.ReLU(),
nn.Linear(300, 300, bias=False),
nn.BatchNorm1d(300),
nn.ReLU(),
nn.Linear(300, 300, bias=False),
nn.BatchNorm1d(300),
nn.ReLU(),
nn.Linear(300, 300, bias=False),
nn.BatchNorm1d(300),
nn.ReLU(),
nn.Linear(300, 1),
)
pu_optimizer = optim.Adam(pu_model.parameters(), lr=1e-5, weight_decay=0.005)
(
pn_model,
pu_model,
loss,
acc,
pre,
rec,
acc_quant,
pre_quant,
rec_quant,
) = train_model(
pn_model,
pu_model,
pn_optimizer,
pu_optimizer,
x_train,
y_train,
x_test,
y_test,
pdata,
epochs,
batch_size,
use_gpu,
)
loss_pu[i] = loss
est_error_pu[i] = acc
est_precision_pu[i] = pre
est_recall_pu[i] = rec
est_error_pubp[i] = acc_quant
est_precision_pubp[i] = pre_quant
est_recall_pubp[i] = rec_quant
seed += 1
loss_pu_mean = np.mean(loss_pu, axis=0)
est_error_pu_mean = np.mean(est_error_pu, axis=0)
est_error_pubp_mean = np.mean(est_error_pubp, axis=0)
est_error_pu_std = np.std(est_error_pu, axis=0)
est_error_pubp_std = np.std(est_error_pubp, axis=0)
return (
loss_pu_mean,
est_error_pu_mean,
est_error_pubp_mean,
est_error_pu_std,
est_error_pubp_std,
)
if __name__ == "__main__":
(
loss_pu_mean,
est_error_pu_mean,
est_error_pubp_mean,
est_error_pu_std,
est_error_pubp_std,
) = main()
print("loss_pu_mean:")
print(loss_pu_mean)
print("est_error_pu_mean:")
print(est_error_pu_mean)
print("est_error_pubp_mean:")
print(est_error_pubp_mean)