-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_mc.jl
159 lines (110 loc) · 3.32 KB
/
example_mc.jl
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
154
155
156
157
158
159
include("adv_or_mc.jl")
include("util.jl")
verbose = false
### prepare data
dname = "diabetes"
D_all = readcsv("data-example/" * dname * ".csv")
id_train = readcsv("data-example/" * dname * ".train")
id_test = readcsv("data-example/" * dname * ".test")
id_train = round.(Int64, id_train)
id_test = round.(Int64, id_test)
println(dname)
### Cross Validation, using first split
## First stage
id_tr = vec(id_train[1,:])
id_ts = vec(id_test[1,:])
X_train = D_all[id_tr,1:end-1]
y_train = round.(Int, D_all[id_tr, end])
X_test = D_all[id_ts,1:end-1]
y_test = round.(Int, D_all[id_ts, end])
X_train, mean_vector, std_vector = standardize(X_train)
X_test = standardize(X_test, mean_vector, std_vector)
lambdas = [2.0^-i for i=1:2:13]
nls = length(lambdas)
# fold
n_train = size(X_train, 1)
n_test = size(X_test, 1)
kf = 5
# k folds
folds = k_fold(n_train, kf)
loss_list = zeros(nls)
# The first stage of CV
idx = randperm(n_train)
X_train = X_train[idx,:]
y_train = y_train[idx]
for i = 1:nls
println(i, " | Adversarial | lambda = ", string(lambdas[i]))
losses = zeros(n_train)
# k fold
for j = 1:kf
# prepare training and validation
id_tr = vcat(folds[[1:j-1; j+1:end]]...)
id_val = folds[j]
X_tr = X_train[id_tr, :]; y_tr = y_train[id_tr]
X_val = X_train[id_val, :]; y_val = y_train[id_val]
print(" ",j, "-th fold : ")
@time model = train_or_adv_mc(X_tr, y_tr, lambdas[i], verbose=verbose)
ls = test_or_adv(model, X_val, y_val)
losses[id_val] = ls
end
loss_list[i] = mean(losses)
println("loss : ", string(mean(losses)))
println()
end
ind_max= indmin(loss_list)
L0 = lambdas[ind_max]
lambdas = [L0*2.0^((i-4)/2) for i=1:7]
nls = length(lambdas)
println("stage 1 lambda : ", L0)
## Second stage
idx = randperm(n_train)
X_train = X_train[idx,:]
y_train = y_train[idx]
for i = 1:nls
println(i, " | Adversarial | lambda = ", string(lambdas[i]))
losses = zeros(n_train)
# k fold
for j = 1:kf
# prepare training and validation
id_tr = vcat(folds[[1:j-1; j+1:end]]...)
id_val = folds[j]
X_tr = X_train[id_tr, :]; y_tr = y_train[id_tr]
X_val = X_train[id_val, :]; y_val = y_train[id_val]
print(" ",j, "-th fold : ")
@time model = train_or_adv_mc(X_tr, y_tr, lambdas[i], verbose=verbose)
ls = test_or_adv(model, X_val, y_val)
losses[id_val] = ls
end
loss_list[i] = mean(losses)
println("loss : ", string(mean(losses)))
println()
end
ind_max= indmin(loss_list)
lambda_best = lambdas[ind_max]
println("best lambda : ", lambda_best)
println("Start Evaluation")
### Evaluation
n_split = size(id_train, 1)
v_model = Vector{ClassificationModel}()
v_mae = zeros(n_split)
for i = 1:n_split
# standardize
id_tr = vec(id_train[i,:])
id_ts = vec(id_test[i,:])
X_train = D_all[id_tr,1:end-1]
y_train = round.(Int, D_all[id_tr, end])
X_test = D_all[id_ts,1:end-1]
y_test = round.(Int, D_all[id_ts, end])
X_train, mean_vector, std_vector = standardize(X_train)
X_test = standardize(X_test, mean_vector, std_vector)
#train and test
model = train_or_adv_mc(X_train, y_train, lambda_best, verbose=verbose)
result = test_or_adv(model, X_test, y_test)
v_mae[i] = result
push!(v_model, model)
println(result)
end
println()
println(dname)
println("adversarial mean mae : ", mean(v_mae))
println("adversarial std mae : ", std(v_mae))