/
feature_scaled_with_embedded_categorical.py
383 lines (346 loc) · 16.7 KB
/
feature_scaled_with_embedded_categorical.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# Copyright 2023 The TensorFlow Probability 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.
# ============================================================================
"""FeatureScaled kernel over continuous and embedded categorical data."""
import tensorflow.compat.v2 as tf
from tensorflow_probability.python.experimental.psd_kernels import feature_scaled_with_categorical as fswc
from tensorflow_probability.python.internal import assert_util
from tensorflow_probability.python.internal import dtype_util
from tensorflow_probability.python.internal import parameter_properties
from tensorflow_probability.python.internal import prefer_static as ps
from tensorflow_probability.python.internal import tensor_util
from tensorflow_probability.python.math.psd_kernels import positive_semidefinite_kernel as psd_kernel
from tensorflow_probability.python.math.psd_kernels.internal import util
class FeatureScaledWithEmbeddedCategorical(
psd_kernel.AutoCompositeTensorPsdKernel):
"""`FeatureScaled` kernel for continuous and embedded categorical data.
This kernel is an extension of `FeatureScaled` that handles categorical data
(encoded as integers, not one-hot) in addition to continuous (float) data.
`ContinuousAndCategoricalValues` structures, containing arrays of continuous
and categorical data, are passed to the `apply`, `matrix` and `tensor`
methods. The continuous inputs are scaled and then passed to the distance
function, like in `FeatureScaled`. Categorical data, encoded as integers,
is continuously embedded using `LinearOperator`s. When all `LinearOperator`s
are either `LinearOperatorIdentity` or `LinearOperatorScaledIdentity`
instances, this kernel is the same as `FeatureScaledWithCategorical`, though
in that case the latter should be used since it will be more efficient.
#### Examples
Compute the kernel matrix on synthetic data.
```python
import numpy as np
continuous_dim = 3
categorical_dim = 2
# Define an ARD kernel that takes a structure of continuous and categorical
# data as inputs, with randomly-sampled `continuous_scale_diag` values and
# diagonal embeddings of categorical data.
base_kernel = tfpk.MaternFiveHalves()
continuous_scale_diag = np.random.uniform(size=[continuous_dim])
# Categorical `scale_diag`s are passed as an iterable of `LinearOperator`s,
# where each `LinearOperator` applies to a categorical feature and has number
# of rows equivalent to the cardinality of that feature. Categorical data is
# assumed to be represented as integers between 0 and `n - 1` inclusive, which
# are used to index into the `inverse_scale_diag` vectors.
num_categories = [5, 4]
categorical_embedding_operators = [
tf.linalg.LinearOperatorDiag(np.random.uniform(size=[n]))
for n in num_categories]
kernel = tfpke.FeatureScaledWithEmbeddedCategorical(
base_kernel,
categorical_embedding_operators=categorical_embedding_operators,
continuous_scale_diag=continuous_scale_diag,
validate_args=True)
# Create `num_points` examples in the continuous/categorical feature space.
num_points = 12
categorical_data_1 = np.stack(
[np.random.randint(n, size=(num_points,)) for n in num_categories])
categorical_data_2 = np.stack(
[np.random.randint(n, size=(num_points,)) for n in num_categories])
x1 = tfpke.ContinuousAndCategoricalValues(
continuous=np.random.normal(size=(num_points, continuous_dim)),
categorical=categorical_data_1)
x2 = tfpke.ContinuousAndCategoricalValues(
continuous=np.random.normal(size=(num_points, continuous_dim)),
categorical=categorical_data_2)
# Evaluate the kernel matrix for `x1` and `x2`.
kernel.matrix(x1, x2) # has shape `[num_points, num_points]`
```
"""
def __init__(
self,
kernel,
categorical_embedding_operators,
continuous_scale_diag=None,
continuous_inverse_scale_diag=None,
feature_ndims=None,
validate_args=False,
name='FeatureScaledWithCategorical'):
"""Construct an `FeatureScaledWithCategorical` kernel instance.
Args:
kernel: `PositiveSemidefiniteKernel` instance. Parameters to `kernel` must
be broadcastable with `scale_diag`. `kernel` must be isotropic and
implement an `_apply_with_distance` method.
categorical_embedding_operators: Iterable of `LinearOperator` instances
used to embed the categorical features. If the input categorical data
has shape `[..., d]` and a single feature dimension, the iterable has
length `d`. Each `LinearOperator` has number of rows equal to the
number of categories, and embeddings are equivalent to one-hot encoded
categorical vectors multiplied by the densified `LinearOperator`.
Euclidean distances are computed between the emeddings. If there are 0
feature dimensions, the iterable should have length 1.
continuous_scale_diag: Floating point array that control the
sharpness/width of the kernel shape. Each `continuous_scale_diag` must
have dimensionality of at least `kernel.feature_ndims.continuous`, and
extra dimensions must be broadcastable with parameters of `kernel`.
Default value: None.
continuous_inverse_scale_diag: Non-negative floating point vectors that
are treated as the reciprocals of the corresponding components of
`continuous_scale_diag`. Only one of `continuous_scale_diag` or
`continuous_inverse_scale_diag` should be provided.
Default value: None
feature_ndims: `ContinuousAndCategoricalValues` instance containing
integers indicating the rank of the continuous and categorical feature
space. Default value: None, i.e. `kernel.feature_ndims` for both
components of the feature space. Categorical `feature_ndims` > 1 is not
supported.
validate_args: If `True`, parameters are checked for validity despite
possibly degrading runtime performance.
name: Python `str` name prefixed to Ops created by this class.
"""
parameters = dict(locals())
if ((continuous_scale_diag is None) ==
(continuous_inverse_scale_diag is None)):
raise ValueError(
'Must specify exactly one of `continuous_scale_diag` and '
'`continuous_inverse_scale_diag`.')
with tf.name_scope(name):
float_dtype = dtype_util.common_dtype(
[kernel, continuous_scale_diag, continuous_inverse_scale_diag,
categorical_embedding_operators],
dtype_hint=tf.float32)
if continuous_scale_diag is None:
self._continuous_scale_diag = continuous_scale_diag
self._continuous_inverse_scale_diag = (
tensor_util.convert_nonref_to_tensor(
continuous_inverse_scale_diag,
dtype_hint=float_dtype,
name='continuous_inverse_scale_diag'))
else:
self._continuous_inverse_scale_diag = continuous_inverse_scale_diag
self._continuous_scale_diag = (
tensor_util.convert_nonref_to_tensor(
continuous_scale_diag,
dtype_hint=float_dtype,
name='continuous_scale_diag'))
self._categorical_embedding_operators = categorical_embedding_operators
self._kernel = kernel
if feature_ndims is None:
feature_ndims = fswc.ContinuousAndCategoricalValues(
kernel.feature_ndims, kernel.feature_ndims)
if feature_ndims.categorical > 1:
raise ValueError('Categorical `feature_ndims` must be 0 or 1.')
dtype = fswc.ContinuousAndCategoricalValues(float_dtype, None)
super(FeatureScaledWithEmbeddedCategorical, self).__init__(
feature_ndims=feature_ndims,
dtype=dtype,
name=name,
validate_args=validate_args,
parameters=parameters)
@property
def kernel(self):
return self._kernel
@property
def continuous_scale_diag(self):
return self._continuous_scale_diag
@property
def continuous_inverse_scale_diag(self):
return self._continuous_inverse_scale_diag
@property
def categorical_embedding_operators(self):
return self._categorical_embedding_operators
def continuous_inverse_scale_diag_parameters(self):
inverse_scale_diag = self.continuous_inverse_scale_diag
if inverse_scale_diag is None:
inverse_scale_diag = tf.nest.map_structure(
tf.math.reciprocal, self.continuous_scale_diag)
return tf.nest.map_structure(tf.convert_to_tensor, inverse_scale_diag)
@classmethod
def _parameter_properties(cls, dtype):
from tensorflow_probability.python.bijectors import softplus # pylint:disable=g-import-not-at-top
return dict(
kernel=parameter_properties.BatchedComponentProperties(),
continuous_scale_diag=parameter_properties.ParameterProperties(
event_ndims=lambda self: self.feature_ndims.continuous,
default_constraining_bijector_fn=(
lambda: softplus.Softplus(low=dtype_util.eps(dtype))),
is_preferred=False),
continuous_inverse_scale_diag=parameter_properties.ParameterProperties(
event_ndims=lambda self: self.feature_ndims.continuous,
default_constraining_bijector_fn=(
lambda: softplus.Softplus(low=dtype_util.eps(dtype)))),
categorical_embedding_operators=(
parameter_properties.BatchedComponentProperties(
event_ndims=(
lambda self: [0] * len(self.categorical_embedding_operators)
),
)))
def _parameter_control_dependencies(self, is_init):
if not self.validate_args:
return []
assertions = []
if self._continuous_inverse_scale_diag is not None:
if is_init != tensor_util.is_ref(self._continuous_inverse_scale_diag):
assertions.append(assert_util.assert_non_negative(
self._continuous_inverse_scale_diag,
message='`continuous_inverse_scale_diag` must be non-negative.'))
if self._continuous_scale_diag is not None:
if is_init != tensor_util.is_ref(self._continuous_scale_diag):
assertions.append(assert_util.assert_positive(
self._continuous_scale_diag,
message='`continuous_scale_diag` must be positive.'))
return assertions
def _apply(self, x1, x2, example_ndims=0):
isd = self.continuous_inverse_scale_diag_parameters()
isd_cont_padded = util.pad_shape_with_ones(
isd,
ndims=example_ndims,
start=-(self.feature_ndims.continuous + 1))
pairwise_square_distance_cont = util.sum_rightmost_ndims_preserving_shape(
tf.math.squared_difference(
x1.continuous * isd_cont_padded,
x2.continuous * isd_cont_padded),
self.feature_ndims.continuous)
pairwise_square_distance_cat = 0.
if self.categorical_embedding_operators:
pairwise_square_distance_cat = self._get_categorical_distance(
x1.categorical, x2.categorical, example_ndims,
self.feature_ndims.categorical)
return self.kernel._apply_with_distance( # pylint: disable=protected-access
x1, x2,
pairwise_square_distance_cont + pairwise_square_distance_cat,
example_ndims=example_ndims)
def _matrix(self, x1, x2):
isd = self.continuous_inverse_scale_diag_parameters()
isd_cont_padded = util.pad_shape_with_ones(
isd,
ndims=1,
start=-(self.feature_ndims.continuous + 1))
pairwise_square_distance_cont = util.pairwise_square_distance_matrix(
x1.continuous * isd_cont_padded,
x2.continuous * isd_cont_padded,
feature_ndims=self.feature_ndims.continuous)
pairwise_square_distance_cat = self._cat_pairwise_square_distance_tensor(
x1.categorical, x2.categorical, x1_example_ndims=1, x2_example_ndims=1,
feature_ndims=self.feature_ndims.categorical,
inverse_scale_diag=self.categorical_embedding_operators)
return self.kernel._apply_with_distance( # pylint: disable=protected-access
x1, x2,
pairwise_square_distance_cont + pairwise_square_distance_cat,
example_ndims=2)
def _tensor(self, x1, x2, x1_example_ndims, x2_example_ndims):
isd = self.continuous_inverse_scale_diag_parameters()
isd_cont_x1 = util.pad_shape_with_ones(
isd,
ndims=x1_example_ndims,
start=-(self.feature_ndims.continuous + 1))
isd_cont_x2 = util.pad_shape_with_ones(
isd,
ndims=x2_example_ndims,
start=-(self.feature_ndims.continuous + 1))
pairwise_square_distance_cont = util.pairwise_square_distance_tensor(
x1.continuous * isd_cont_x1,
x2.continuous * isd_cont_x2,
self.feature_ndims.continuous,
x1_example_ndims,
x2_example_ndims)
pairwise_square_distance_cat = self._cat_pairwise_square_distance_tensor(
x1.categorical, x2.categorical,
x1_example_ndims=x1_example_ndims, x2_example_ndims=x2_example_ndims,
feature_ndims=self.feature_ndims.categorical,
inverse_scale_diag=self.categorical_embedding_operators)
return self.kernel._apply_with_distance( # pylint: disable=protected-access
x1, x2,
pairwise_square_distance_cont + pairwise_square_distance_cat,
example_ndims=x1_example_ndims+x2_example_ndims)
def _get_categorical_distance(self, x1, x2, example_ndims, feature_ndims):
x_batch, _ = ps.split(
ps.broadcast_shape(ps.shape(x1), ps.shape(x2)),
num_or_size_splits=[-1, example_ndims + 1])
bcast_shape = ps.broadcast_shape(x_batch, self.batch_shape_tensor())
batch_rank = ps.size(bcast_shape)
def _get_categorical_distance_one_feature(x1_, x2_, isd):
if isinstance(isd, tf.linalg.LinearOperatorIdentity):
return tf.cast(tf.not_equal(x1_, x2_), dtype=isd.dtype) * 2.
if isinstance(isd, tf.linalg.LinearOperatorScaledIdentity):
return tf.where(
tf.equal(x1_, x2_),
tf.zeros([], dtype=isd.dtype),
2. * isd.multiplier ** 2)
x1_ = x1_[..., tf.newaxis]
x2_ = x2_[..., tf.newaxis]
x1_bcast = ps.broadcast_to(
x1_,
ps.concat([bcast_shape, ps.shape(x1_)[-(example_ndims + 1):]], axis=0)
)
x2_bcast = ps.broadcast_to(
x2_,
ps.concat([bcast_shape, ps.shape(x2_)[-(example_ndims + 1):]], axis=0)
)
if isinstance(isd, tf.linalg.LinearOperatorDiag):
diag_bcast = tf.broadcast_to(isd.diag, ps.concat(
[bcast_shape, ps.shape(isd.diag)[-1:]], axis=0))
x1_embedding = tf.gather_nd(diag_bcast, x1_bcast, batch_dims=batch_rank)
x2_embedding = tf.gather_nd(diag_bcast, x2_bcast, batch_dims=batch_rank)
return tf.where(
tf.equal(x1_[..., 0], x2_[..., 0]),
tf.zeros([], dtype=isd.dtype),
x1_embedding ** 2 + x2_embedding ** 2)
isd_mat = isd.to_dense()
isd_bcast = tf.broadcast_to(
isd_mat,
ps.concat([bcast_shape, ps.shape(isd_mat)[-2:]], axis=0))
x1_embedding = tf.gather_nd(isd_bcast, x1_bcast, batch_dims=batch_rank)
x2_embedding = tf.gather_nd(isd_bcast, x2_bcast, batch_dims=batch_rank)
# TODO(emilyaf): Use `util.pairwise_square_distance_tensor` if necessary
# for high-cardinality categorical features.
return util.sum_rightmost_ndims_preserving_shape(
tf.math.squared_difference(x1_embedding, x2_embedding),
1)
if feature_ndims == 0:
return _get_categorical_distance_one_feature(
x1, x2, self.categorical_embedding_operators[0]
)
distances = tf.nest.map_structure(
_get_categorical_distance_one_feature,
ps.unstack(x1, axis=-1),
ps.unstack(x2, axis=-1),
self.categorical_embedding_operators
)
return util.sum_rightmost_ndims_preserving_shape(
tf.stack(distances, axis=-1), feature_ndims
)
def _cat_pairwise_square_distance_tensor(
self, x1, x2, x1_example_ndims, x2_example_ndims, feature_ndims,
inverse_scale_diag):
if not inverse_scale_diag:
return 0.
x1 = util.pad_shape_with_ones(
x1,
ndims=x2_example_ndims,
start=-(feature_ndims + 1))
x2 = util.pad_shape_with_ones(
x2,
ndims=x1_example_ndims,
start=-(feature_ndims + 1 + x2_example_ndims))
example_ndims = x1_example_ndims + x2_example_ndims
return self._get_categorical_distance(x1, x2, example_ndims, feature_ndims)