-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Expand file tree
/
Copy pathcriteo.py
More file actions
162 lines (130 loc) · 4.92 KB
/
Copy pathcriteo.py
File metadata and controls
162 lines (130 loc) · 4.92 KB
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
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
"""Schema and transform definition for the Criteo dataset."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import tensorflow_transform as tft
def _get_raw_categorical_column_name(column_idx):
return 'categorical-feature-{}'.format(column_idx)
def get_transformed_categorical_column_name(column_name_or_id):
if isinstance(column_name_or_id, bytes):
# assume the input is column name
column_name = column_name_or_id
else:
# assume the input is column id
column_name = _get_raw_categorical_column_name(column_name_or_id)
return column_name + '_id'
def fill_in_missing(feature, default_value):
"""Fills missing values in a rank 2 SparseTensor.
Args:
feature: A rank 2 SparseTensor with at most one value per row.
default_value: The value to fill in for missing entries.
Returns:
A rank 1 Tensor with missing entries filled in.
"""
feature = tft.sparse_tensor_to_dense_with_shape(
feature, [None, 1], default_value=default_value)
return tf.squeeze(feature, axis=1)
_INTEGER_COLUMN_NAMES = [
'int-feature-{}'.format(column_idx) for column_idx in range(1, 14)
]
_CATEGORICAL_COLUMN_NAMES = [
_get_raw_categorical_column_name(column_idx)
for column_idx in range(14, 40)
]
DEFAULT_DELIMITER = '\t'
# Number of buckets for integer columns.
_NUM_BUCKETS = 10
# Schema annotations aren't supported in this build.
tft.common.IS_ANNOTATIONS_PB_AVAILABLE = False
def make_ordered_column_names(include_label=True):
"""Returns the column names in the dataset in the order as they appear.
Args:
include_label: Indicates whether the label feature should be included.
Returns:
A list of column names in the dataset.
"""
result = ['clicked'] if include_label else []
for name in _INTEGER_COLUMN_NAMES:
result.append(name)
for name in _CATEGORICAL_COLUMN_NAMES:
result.append(name)
return result
def make_legacy_input_feature_spec(include_label=True):
"""Input schema definition.
Args:
include_label: Indicates whether the label feature should be included.
Returns:
A `Schema` object.
"""
result = {}
if include_label:
result['clicked'] = tf.io.FixedLenFeature(shape=[], dtype=tf.int64)
for name in _INTEGER_COLUMN_NAMES:
result[name] = tf.io.FixedLenFeature(
shape=[], dtype=tf.int64, default_value=-1)
for name in _CATEGORICAL_COLUMN_NAMES:
result[name] = tf.io.FixedLenFeature(
shape=[], dtype=tf.string, default_value='')
return result
def make_input_feature_spec(include_label=True):
"""Input schema definition.
Args:
include_label: Indicates whether the label feature should be included.
Returns:
A `Schema` object.
"""
result = {}
if include_label:
result['clicked'] = tf.io.FixedLenFeature(shape=[], dtype=tf.int64)
for name in _INTEGER_COLUMN_NAMES:
result[name] = tf.io.VarLenFeature(dtype=tf.int64)
for name in _CATEGORICAL_COLUMN_NAMES:
result[name] = tf.io.VarLenFeature(dtype=tf.string)
return result
def make_preprocessing_fn(frequency_threshold):
"""Creates a preprocessing function for criteo.
Args:
frequency_threshold: The frequency_threshold used when generating
vocabularies for the categorical features.
Returns:
A preprocessing function.
"""
def preprocessing_fn(inputs):
"""User defined preprocessing function for criteo columns.
Args:
inputs: dictionary of input `tensorflow_transform.Column`.
Returns:
A dictionary of `tensorflow_transform.Column` representing the transformed
columns.
"""
result = {'clicked': inputs['clicked']}
for name in _INTEGER_COLUMN_NAMES:
feature = inputs[name]
feature = fill_in_missing(feature, -1)
result[name] = feature
result[name + '_bucketized'] = tft.bucketize(feature, _NUM_BUCKETS)
for name in _CATEGORICAL_COLUMN_NAMES:
feature = inputs[name]
feature = fill_in_missing(feature, '')
result[get_transformed_categorical_column_name(
name)] = tft.compute_and_apply_vocabulary(
feature, frequency_threshold=frequency_threshold)
return result
return preprocessing_fn