forked from jsyoon0823/VIME
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupervised_models.py
144 lines (108 loc) · 3.81 KB
/
supervised_models.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
132
133
134
135
136
137
138
139
140
141
142
143
144
"""VIME: Extending the Success of Self- and Semi-supervised Learning to Tabular Domain (VIME) Codebase.
Reference: Jinsung Yoon, Yao Zhang, James Jordon, Mihaela van der Schaar,
"VIME: Extending the Success of Self- and Semi-supervised Learning to Tabular Domain,"
Neural Information Processing Systems (NeurIPS), 2020.
Paper link: TBD
Last updated Date: October 11th 2020
Code author: Jinsung Yoon ([email protected])
-----------------------------
supervised_models.py
- Train supervised model and return predictions on the testing data
(1) logit: logistic regression
(2) xgb_model: XGBoost model
(3) mlp: multi-layer perceptrons
"""
# Necessary packages
import numpy as np
from sklearn.linear_model import LogisticRegression
import xgboost as xgb
from keras import backend as K
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping
from vime_utils import convert_matrix_to_vector, convert_vector_to_matrix
#%%
def logit(x_train, y_train, x_test):
"""Logistic Regression.
Args:
- x_train, y_train: training dataset
- x_test: testing feature
Returns:
- y_test_hat: predicted values for x_test
"""
# Convert labels into proper format
if len(y_train.shape) > 1:
y_train = convert_matrix_to_vector(y_train)
# Define and fit model on training dataset
model = LogisticRegression()
model.fit(x_train, y_train)
# Predict on x_test
y_test_hat = model.predict_proba(x_test)
return y_test_hat
#%%
def xgb_model(x_train, y_train, x_test):
"""XGBoost.
Args:
- x_train, y_train: training dataset
- x_test: testing feature
Returns:
- y_test_hat: predicted values for x_test
"""
# Convert labels into proper format
if len(y_train.shape) > 1:
y_train = convert_matrix_to_vector(y_train)
# Define and fit model on training dataset
model = xgb.XGBClassifier()
model.fit(x_train, y_train)
# Predict on x_test
y_test_hat = model.predict_proba(x_test)
return y_test_hat
#%%
def mlp(x_train, y_train, x_test, parameters):
"""Multi-layer perceptron (MLP).
Args:
- x_train, y_train: training dataset
- x_test: testing feature
- parameters: hidden_dim, epochs, activation, batch_size
Returns:
- y_test_hat: predicted values for x_test
"""
# Convert labels into proper format
if len(y_train.shape) == 1:
y_train = convert_vector_to_matrix(y_train)
# Divide training and validation sets (9:1)
idx = np.random.permutation(len(x_train[:, 0]))
train_idx = idx[:int(len(idx)*0.9)]
valid_idx = idx[int(len(idx)*0.9):]
# Validation set
x_valid = x_train[valid_idx, :]
y_valid = y_train[valid_idx, :]
# Training set
x_train = x_train[train_idx, :]
y_train = y_train[train_idx, :]
# Reset the graph
K.clear_session()
# Define network parameters
hidden_dim = parameters['hidden_dim']
epochs_size = parameters['epochs']
act_fn = parameters['activation']
batch_size = parameters['batch_size']
# Define basic parameters
data_dim = len(x_train[0, :])
label_dim = len(y_train[0, :])
# Build model
model = Sequential()
model.add(Dense(hidden_dim, input_dim = data_dim, activation = act_fn))
model.add(Dense(hidden_dim, activation = act_fn))
model.add(Dense(label_dim, activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer='adam',
metrics = ['acc'])
es = EarlyStopping(monitor='val_loss', mode = 'min',
verbose = 1, restore_best_weights=True, patience=50)
# Fit model on training dataset
model.fit(x_train, y_train, validation_data = (x_valid, y_valid),
epochs = epochs_size, batch_size = batch_size,
verbose = 0, callbacks=[es])
# Predict on x_test
y_test_hat = model.predict(x_test)
return y_test_hat