-
Notifications
You must be signed in to change notification settings - Fork 18
/
tabnet_model.py
248 lines (208 loc) · 9.14 KB
/
tabnet_model.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# coding=utf-8
# Copyright 2019 The Google Research Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""TabNet model."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import tensorflow as tf
from tensorflow.contrib import sparsemax as contrib_sparsemax
def glu(act, n_units):
"""Generalized linear unit nonlinear activation."""
return act[:, :n_units] * tf.nn.sigmoid(act[:, n_units:])
class TabNet(object):
"""TabNet model class."""
def __init__(self,
columns,
num_features,
feature_dim,
output_dim,
num_decision_steps,
relaxation_factor,
batch_momentum,
virtual_batch_size,
num_classes,
epsilon=0.00001):
"""Initializes a TabNet instance.
Args:
columns: The Tensorflow column names for the dataset.
num_features: The number of input features (i.e the number of columns for
tabular data assuming each feature is represented with 1 dimension).
feature_dim: Dimensionality of the hidden representation in feature
transformation block. Each layer first maps the representation to a
2*feature_dim-dimensional output and half of it is used to determine the
nonlinearity of the GLU activation where the other half is used as an
input to GLU, and eventually feature_dim-dimensional output is
transferred to the next layer.
output_dim: Dimensionality of the outputs of each decision step, which is
later mapped to the final classification or regression output.
num_decision_steps: Number of sequential decision steps.
relaxation_factor: Relaxation factor that promotes the reuse of each
feature at different decision steps. When it is 1, a feature is enforced
to be used only at one decision step and as it increases, more
flexibility is provided to use a feature at multiple decision steps.
batch_momentum: Momentum in ghost batch normalization.
virtual_batch_size: Virtual batch size in ghost batch normalization. The
overall batch size should be an integer multiple of virtual_batch_size.
num_classes: Number of output classes.
epsilon: A small number for numerical stability of the entropy calcations.
Returns:
A TabNet instance.
"""
self.columns = columns
self.num_features = num_features
self.feature_dim = feature_dim
self.output_dim = output_dim
self.num_decision_steps = num_decision_steps
self.relaxation_factor = relaxation_factor
self.batch_momentum = batch_momentum
self.virtual_batch_size = virtual_batch_size
self.num_classes = num_classes
self.epsilon = epsilon
def encoder(self, data, is_training):
"""TabNet encoder model."""
with tf.compat.v1.variable_scope("Encoder", reuse=tf.compat.v1.AUTO_REUSE):
# Reads and normalizes input features.
features = tf.compat.v1.feature_column.input_layer(data, self.columns)
features = tf.layers.batch_normalization(
features, training=is_training, momentum=self.batch_momentum)
batch_size = tf.shape(features)[0]
# Initializes decision-step dependent variables.
output_aggregated = tf.zeros([batch_size, self.output_dim])
masked_features = features
mask_values = tf.zeros([batch_size, self.num_features])
aggregated_mask_values = tf.zeros([batch_size, self.num_features])
complemantary_aggregated_mask_values = tf.ones(
[batch_size, self.num_features])
total_entropy = 0
if is_training:
v_b = self.virtual_batch_size
else:
v_b = 1
for ni in range(self.num_decision_steps):
# Feature transformer with two shared and two decision step dependent
# blocks is used below.
reuse_flag = (ni > 0)
transform_f1 = tf.layers.dense(
masked_features,
self.feature_dim * 2,
name="Transform_f1",
reuse=reuse_flag,
use_bias=False)
transform_f1 = tf.layers.batch_normalization(
transform_f1,
training=is_training,
momentum=self.batch_momentum,
virtual_batch_size=v_b)
transform_f1 = glu(transform_f1, self.feature_dim)
transform_f2 = tf.layers.dense(
transform_f1,
self.feature_dim * 2,
name="Transform_f2",
reuse=reuse_flag,
use_bias=False)
transform_f2 = tf.layers.batch_normalization(
transform_f2,
training=is_training,
momentum=self.batch_momentum,
virtual_batch_size=v_b)
transform_f2 = (glu(transform_f2, self.feature_dim) +
transform_f1) * np.sqrt(0.5)
transform_f3 = tf.layers.dense(
transform_f2,
self.feature_dim * 2,
name="Transform_f3" + str(ni),
use_bias=False)
transform_f3 = tf.layers.batch_normalization(
transform_f3,
training=is_training,
momentum=self.batch_momentum,
virtual_batch_size=v_b)
transform_f3 = (glu(transform_f3, self.feature_dim) +
transform_f2) * np.sqrt(0.5)
transform_f4 = tf.layers.dense(
transform_f3,
self.feature_dim * 2,
name="Transform_f4" + str(ni),
use_bias=False)
transform_f4 = tf.layers.batch_normalization(
transform_f4,
training=is_training,
momentum=self.batch_momentum,
virtual_batch_size=v_b)
transform_f4 = (glu(transform_f4, self.feature_dim) +
transform_f3) * np.sqrt(0.5)
if ni > 0:
decision_out = tf.nn.relu(transform_f4[:, :self.output_dim])
# Decision aggregation.
output_aggregated += decision_out
# Aggregated masks are used for visualization of the
# feature importance attributes.
scale_agg = tf.reduce_sum(
decision_out, axis=1, keep_dims=True) / (
self.num_decision_steps - 1)
aggregated_mask_values += mask_values * scale_agg
features_for_coef = (transform_f4[:, self.output_dim:])
if ni < self.num_decision_steps - 1:
# Determines the feature masks via linear and nonlinear
# transformations, taking into account of aggregated feature use.
mask_values = tf.layers.dense(
features_for_coef,
self.num_features,
name="Transform_coef" + str(ni),
use_bias=False)
mask_values = tf.layers.batch_normalization(
mask_values,
training=is_training,
momentum=self.batch_momentum,
virtual_batch_size=v_b)
mask_values *= complemantary_aggregated_mask_values
mask_values = contrib_sparsemax.sparsemax(mask_values)
# Relaxation factor controls the amount of reuse of features between
# different decision blocks and updated with the values of
# coefficients.
complemantary_aggregated_mask_values *= (
self.relaxation_factor - mask_values)
# Entropy is used to penalize the amount of sparsity in feature
# selection.
total_entropy += tf.reduce_mean(
tf.reduce_sum(
-mask_values * tf.math.log(mask_values + self.epsilon),
axis=1)) / (
self.num_decision_steps - 1)
# Feature selection.
masked_features = tf.multiply(mask_values, features)
# Visualization of the feature selection mask at decision step ni
tf.compat.v1.summary.image(
"Mask for step" + str(ni),
tf.expand_dims(tf.expand_dims(mask_values, 0), 3),
max_outputs=1)
# Visualization of the aggregated feature importances
tf.compat.v1.summary.image(
"Aggregated mask",
tf.expand_dims(tf.expand_dims(aggregated_mask_values, 0), 3),
max_outputs=1)
return output_aggregated, total_entropy
def classify(self, activations):
"""TabNet classify block."""
with tf.compat.v1.variable_scope("Classify", reuse=tf.compat.v1.AUTO_REUSE):
logits = tf.layers.dense(activations, self.num_classes, use_bias=False)
predictions = tf.nn.softmax(logits)
return logits, predictions
def regress(self, activations):
"""TabNet regress block."""
with tf.compat.v1.variable_scope("Regress", reuse=tf.compat.v1.AUTO_REUSE):
predictions = tf.layers.dense(activations, 1)
return predictions