aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/feature_column
diff options
context:
space:
mode:
authorGravatar Rohan Jain <rohanj@google.com>2018-07-06 11:51:23 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-07-06 11:54:16 -0700
commit3acc7e344f31c6543813ffdd135c29fe9bf8b2b9 (patch)
tree5e56bb6694a7681d4525b660ad3385f9a9f2376d /tensorflow/python/feature_column
parent8a9f023e3becdc7414f1aad98ddfaa7f2c983749 (diff)
Creating the new FeatureColumn's that would be made public. This CL clones the existing implementation with minor changes to fit in the new API.
InputLayer and LinearModel are mostly unchanged and currently only supported using the old _FeatureColumns. In subsequent changes, they'll be made to support both new and old FeatureColumn API's. PiperOrigin-RevId: 203506113
Diffstat (limited to 'tensorflow/python/feature_column')
-rw-r--r--tensorflow/python/feature_column/BUILD68
-rw-r--r--tensorflow/python/feature_column/feature_column_v2.py3600
-rw-r--r--tensorflow/python/feature_column/feature_column_v2_test.py6583
3 files changed, 10251 insertions, 0 deletions
diff --git a/tensorflow/python/feature_column/BUILD b/tensorflow/python/feature_column/BUILD
index 295d4ca094..80707030e6 100644
--- a/tensorflow/python/feature_column/BUILD
+++ b/tensorflow/python/feature_column/BUILD
@@ -48,6 +48,39 @@ py_library(
],
)
+py_library(
+ name = "feature_column_v2",
+ srcs = ["feature_column_v2.py"],
+ srcs_version = "PY2AND3",
+ deps = [
+ "//tensorflow/python:array_ops",
+ "//tensorflow/python:check_ops",
+ "//tensorflow/python:control_flow_ops",
+ "//tensorflow/python:dtypes",
+ "//tensorflow/python:embedding_ops",
+ "//tensorflow/python:framework_ops",
+ "//tensorflow/python:init_ops",
+ "//tensorflow/python:lookup_ops",
+ "//tensorflow/python:math_ops",
+ "//tensorflow/python:nn_ops",
+ "//tensorflow/python:parsing_ops",
+ "//tensorflow/python:platform",
+ "//tensorflow/python:resource_variable_ops",
+ "//tensorflow/python:sparse_ops",
+ "//tensorflow/python:sparse_tensor",
+ "//tensorflow/python:string_ops",
+ "//tensorflow/python:template",
+ "//tensorflow/python:tensor_shape",
+ "//tensorflow/python:training",
+ "//tensorflow/python:util",
+ "//tensorflow/python:variable_scope",
+ "//tensorflow/python:variables",
+ "//tensorflow/python/keras",
+ "//third_party/py/numpy",
+ "@six_archive//:six",
+ ],
+)
+
filegroup(
name = "vocabulary_testdata",
srcs = [
@@ -92,3 +125,38 @@ py_test(
"//tensorflow/python/estimator:numpy_io",
],
)
+
+py_test(
+ name = "feature_column_v2_test",
+ srcs = ["feature_column_v2_test.py"],
+ data = [":vocabulary_testdata"],
+ srcs_version = "PY2AND3",
+ tags = [
+ "no_cuda_on_cpu_tap",
+ "no_pip",
+ ],
+ deps = [
+ ":feature_column_py",
+ ":feature_column_v2",
+ "//tensorflow/core:protos_all_py",
+ "//tensorflow/python:array_ops",
+ "//tensorflow/python:client_testlib",
+ "//tensorflow/python:constant_op",
+ "//tensorflow/python:dtypes",
+ "//tensorflow/python:errors",
+ "//tensorflow/python:framework_ops",
+ "//tensorflow/python:framework_test_lib",
+ "//tensorflow/python:lookup_ops",
+ "//tensorflow/python:parsing_ops",
+ "//tensorflow/python:partitioned_variables",
+ "//tensorflow/python:session",
+ "//tensorflow/python:sparse_tensor",
+ "//tensorflow/python:training",
+ "//tensorflow/python:variable_scope",
+ "//tensorflow/python:variables",
+ "//tensorflow/python/eager:backprop",
+ "//tensorflow/python/eager:context",
+ "//tensorflow/python/estimator:numpy_io",
+ "//third_party/py/numpy",
+ ],
+)
diff --git a/tensorflow/python/feature_column/feature_column_v2.py b/tensorflow/python/feature_column/feature_column_v2.py
new file mode 100644
index 0000000000..b4dd23f58d
--- /dev/null
+++ b/tensorflow/python/feature_column/feature_column_v2.py
@@ -0,0 +1,3600 @@
+# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
+#
+# 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.
+# ==============================================================================
+"""This API defines FeatureColumn abstraction.
+
+FeatureColumns provide a high level abstraction for ingesting and representing
+features. FeatureColumns are also the primary way of encoding features for
+canned @{tf.estimator.Estimator}s.
+
+When using FeatureColumns with `Estimators`, the type of feature column you
+should choose depends on (1) the feature type and (2) the model type.
+
+1. Feature type:
+
+ * Continuous features can be represented by `numeric_column`.
+ * Categorical features can be represented by any `categorical_column_with_*`
+ column:
+ - `categorical_column_with_vocabulary_list`
+ - `categorical_column_with_vocabulary_file`
+ - `categorical_column_with_hash_bucket`
+ - `categorical_column_with_identity`
+ - `weighted_categorical_column`
+
+2. Model type:
+
+ * Deep neural network models (`DNNClassifier`, `DNNRegressor`).
+
+ Continuous features can be directly fed into deep neural network models.
+
+ age_column = numeric_column("age")
+
+ To feed sparse features into DNN models, wrap the column with
+ `embedding_column` or `indicator_column`. `indicator_column` is recommended
+ for features with only a few possible values. For features with many
+ possible values, to reduce the size of your model, `embedding_column` is
+ recommended.
+
+ embedded_dept_column = embedding_column(
+ categorical_column_with_vocabulary_list(
+ "department", ["math", "philosophy", ...]), dimension=10)
+
+ * Wide (aka linear) models (`LinearClassifier`, `LinearRegressor`).
+
+ Sparse features can be fed directly into linear models. They behave like an
+ indicator column but with an efficient implementation.
+
+ dept_column = categorical_column_with_vocabulary_list("department",
+ ["math", "philosophy", "english"])
+
+ It is recommended that continuous features be bucketized before being
+ fed into linear models.
+
+ bucketized_age_column = bucketized_column(
+ source_column=age_column,
+ boundaries=[18, 25, 30, 35, 40, 45, 50, 55, 60, 65])
+
+ Sparse features can be crossed (also known as conjuncted or combined) in
+ order to form non-linearities, and then fed into linear models.
+
+ cross_dept_age_column = crossed_column(
+ columns=["department", bucketized_age_column],
+ hash_bucket_size=1000)
+
+Example of building canned `Estimator`s using FeatureColumns:
+
+ ```python
+ # Define features and transformations
+ deep_feature_columns = [age_column, embedded_dept_column]
+ wide_feature_columns = [dept_column, bucketized_age_column,
+ cross_dept_age_column]
+
+ # Build deep model
+ estimator = DNNClassifier(
+ feature_columns=deep_feature_columns,
+ hidden_units=[500, 250, 50])
+ estimator.train(...)
+
+ # Or build a wide model
+ estimator = LinearClassifier(
+ feature_columns=wide_feature_columns)
+ estimator.train(...)
+
+ # Or build a wide and deep model!
+ estimator = DNNLinearCombinedClassifier(
+ linear_feature_columns=wide_feature_columns,
+ dnn_feature_columns=deep_feature_columns,
+ dnn_hidden_units=[500, 250, 50])
+ estimator.train(...)
+ ```
+
+
+FeatureColumns can also be transformed into a generic input layer for
+custom models using `input_layer`.
+
+Example of building model using FeatureColumns, this can be used in a
+`model_fn` which is given to the {tf.estimator.Estimator}:
+
+ ```python
+ # Building model via layers
+
+ deep_feature_columns = [age_column, embedded_dept_column]
+ columns_to_tensor = parse_feature_columns_from_examples(
+ serialized=my_data,
+ feature_columns=deep_feature_columns)
+ first_layer = input_layer(
+ features=columns_to_tensor,
+ feature_columns=deep_feature_columns)
+ second_layer = fully_connected(first_layer, ...)
+ ```
+
+NOTE: Functions prefixed with "_" indicate experimental or private parts of
+the API subject to change, and should not be relied upon!
+"""
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+import abc
+import collections
+import math
+
+import numpy as np
+import six
+
+
+from tensorflow.python.eager import context
+from tensorflow.python.feature_column import feature_column as fc_old
+from tensorflow.python.framework import dtypes
+from tensorflow.python.framework import ops
+from tensorflow.python.framework import sparse_tensor as sparse_tensor_lib
+from tensorflow.python.framework import tensor_shape
+from tensorflow.python.keras.engine import training
+from tensorflow.python.layers import base
+from tensorflow.python.ops import array_ops
+from tensorflow.python.ops import check_ops
+from tensorflow.python.ops import control_flow_ops
+from tensorflow.python.ops import embedding_ops
+from tensorflow.python.ops import init_ops
+from tensorflow.python.ops import lookup_ops
+from tensorflow.python.ops import math_ops
+from tensorflow.python.ops import nn_ops
+from tensorflow.python.ops import parsing_ops
+from tensorflow.python.ops import resource_variable_ops
+from tensorflow.python.ops import sparse_ops
+from tensorflow.python.ops import string_ops
+from tensorflow.python.ops import template
+from tensorflow.python.ops import variable_scope
+from tensorflow.python.ops import variables
+from tensorflow.python.platform import gfile
+from tensorflow.python.platform import tf_logging as logging
+from tensorflow.python.training import checkpoint_utils
+from tensorflow.python.util import nest
+
+
+def _internal_input_layer(features,
+ feature_columns,
+ weight_collections=None,
+ trainable=True,
+ cols_to_vars=None,
+ scope=None):
+ """See input_layer. `scope` is a name or variable scope to use."""
+
+ feature_columns = fc_old._normalize_feature_columns(feature_columns) # pylint: disable=protected-access
+ for column in feature_columns:
+ if not isinstance(column, fc_old._DenseColumn): # pylint: disable=protected-access
+ raise ValueError(
+ 'Items of feature_columns must be a _DenseColumn. '
+ 'You can wrap a categorical column with an '
+ 'embedding_column or indicator_column. Given: {}'.format(column))
+ weight_collections = list(weight_collections or [])
+ if ops.GraphKeys.GLOBAL_VARIABLES not in weight_collections:
+ weight_collections.append(ops.GraphKeys.GLOBAL_VARIABLES)
+ if ops.GraphKeys.MODEL_VARIABLES not in weight_collections:
+ weight_collections.append(ops.GraphKeys.MODEL_VARIABLES)
+
+ # a non-None `scope` can allow for variable reuse, when, e.g., this function
+ # is wrapped by a `make_template`.
+ with variable_scope.variable_scope(
+ scope, default_name='input_layer', values=features.values()):
+ builder = fc_old._LazyBuilder(features) # pylint: disable=protected-access
+ output_tensors = []
+ ordered_columns = []
+ for column in sorted(feature_columns, key=lambda x: x.name):
+ ordered_columns.append(column)
+ with variable_scope.variable_scope(
+ None, default_name=column._var_scope_name): # pylint: disable=protected-access
+ tensor = column._get_dense_tensor( # pylint: disable=protected-access
+ builder,
+ weight_collections=weight_collections,
+ trainable=trainable)
+ num_elements = column._variable_shape.num_elements() # pylint: disable=protected-access
+ batch_size = array_ops.shape(tensor)[0]
+ output_tensors.append(
+ array_ops.reshape(tensor, shape=(batch_size, num_elements)))
+ if cols_to_vars is not None:
+ # Retrieve any variables created (some _DenseColumn's don't create
+ # variables, in which case an empty list is returned).
+ cols_to_vars[column] = ops.get_collection(
+ ops.GraphKeys.GLOBAL_VARIABLES,
+ scope=variable_scope.get_variable_scope().name)
+ _verify_static_batch_size_equality(output_tensors, ordered_columns)
+ return array_ops.concat(output_tensors, 1)
+
+
+def input_layer(features,
+ feature_columns,
+ weight_collections=None,
+ trainable=True,
+ cols_to_vars=None):
+ """Returns a dense `Tensor` as input layer based on given `feature_columns`.
+
+ Generally a single example in training data is described with FeatureColumns.
+ At the first layer of the model, this column oriented data should be converted
+ to a single `Tensor`.
+
+ Example:
+
+ ```python
+ price = numeric_column('price')
+ keywords_embedded = embedding_column(
+ categorical_column_with_hash_bucket("keywords", 10K), dimensions=16)
+ columns = [price, keywords_embedded, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ dense_tensor = input_layer(features, columns)
+ for units in [128, 64, 32]:
+ dense_tensor = tf.layers.dense(dense_tensor, units, tf.nn.relu)
+ prediction = tf.layers.dense(dense_tensor, 1)
+ ```
+
+ Args:
+ features: A mapping from key to tensors. `_FeatureColumn`s look up via these
+ keys. For example `numeric_column('price')` will look at 'price' key in
+ this dict. Values can be a `SparseTensor` or a `Tensor` depends on
+ corresponding `_FeatureColumn`.
+ feature_columns: An iterable containing the FeatureColumns to use as inputs
+ to your model. All items should be instances of classes derived from
+ `_DenseColumn` such as `numeric_column`, `embedding_column`,
+ `bucketized_column`, `indicator_column`. If you have categorical features,
+ you can wrap them with an `embedding_column` or `indicator_column`.
+ weight_collections: A list of collection names to which the Variable will be
+ added. Note that variables will also be added to collections
+ `tf.GraphKeys.GLOBAL_VARIABLES` and `ops.GraphKeys.MODEL_VARIABLES`.
+ trainable: If `True` also add the variable to the graph collection
+ `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
+ cols_to_vars: If not `None`, must be a dictionary that will be filled with a
+ mapping from `_FeatureColumn` to list of `Variable`s. For example, after
+ the call, we might have cols_to_vars =
+ {_EmbeddingColumn(
+ categorical_column=_HashedCategoricalColumn(
+ key='sparse_feature', hash_bucket_size=5, dtype=tf.string),
+ dimension=10): [<tf.Variable 'some_variable:0' shape=(5, 10),
+ <tf.Variable 'some_variable:1' shape=(5, 10)]}
+ If a column creates no variables, its value will be an empty list.
+
+ Returns:
+ A `Tensor` which represents input layer of a model. Its shape
+ is (batch_size, first_layer_dimension) and its dtype is `float32`.
+ first_layer_dimension is determined based on given `feature_columns`.
+
+ Raises:
+ ValueError: if an item in `feature_columns` is not a `_DenseColumn`.
+ """
+ return _internal_input_layer(features, feature_columns, weight_collections,
+ trainable, cols_to_vars)
+
+
+# TODO(akshayka): InputLayer should be a subclass of Layer, and it
+# should implement the logic in input_layer using Layer's build-and-call
+# paradigm; input_layer should create an instance of InputLayer and
+# return the result of invoking its apply method, just as functional layers do.
+class InputLayer(object):
+ """An object-oriented version of `input_layer` that reuses variables."""
+
+ def __init__(self,
+ feature_columns,
+ weight_collections=None,
+ trainable=True,
+ cols_to_vars=None):
+ """See `input_layer`."""
+
+ self._feature_columns = feature_columns
+ self._weight_collections = weight_collections
+ self._trainable = trainable
+ self._cols_to_vars = cols_to_vars
+ self._input_layer_template = template.make_template(
+ 'feature_column_input_layer',
+ _internal_input_layer,
+ create_scope_now_=True)
+ self._scope = self._input_layer_template.variable_scope
+
+ def __call__(self, features):
+ return self._input_layer_template(
+ features=features,
+ feature_columns=self._feature_columns,
+ weight_collections=self._weight_collections,
+ trainable=self._trainable,
+ cols_to_vars=None,
+ scope=self._scope)
+
+ @property
+ def non_trainable_variables(self):
+ return self._input_layer_template.non_trainable_variables
+
+ @property
+ def non_trainable_weights(self):
+ return self._input_layer_template.non_trainable_weights
+
+ @property
+ def trainable_variables(self):
+ return self._input_layer_template.trainable_variables
+
+ @property
+ def trainable_weights(self):
+ return self._input_layer_template.trainable_weights
+
+ @property
+ def variables(self):
+ return self._input_layer_template.variables
+
+ @property
+ def weights(self):
+ return self._input_layer_template.weights
+
+
+def linear_model(features,
+ feature_columns,
+ units=1,
+ sparse_combiner='sum',
+ weight_collections=None,
+ trainable=True,
+ cols_to_vars=None):
+ """Returns a linear prediction `Tensor` based on given `feature_columns`.
+
+ This function generates a weighted sum based on output dimension `units`.
+ Weighted sum refers to logits in classification problems. It refers to the
+ prediction itself for linear regression problems.
+
+ Note on supported columns: `linear_model` treats categorical columns as
+ `indicator_column`s. To be specific, assume the input as `SparseTensor` looks
+ like:
+
+ ```python
+ shape = [2, 2]
+ {
+ [0, 0]: "a"
+ [1, 0]: "b"
+ [1, 1]: "c"
+ }
+ ```
+ `linear_model` assigns weights for the presence of "a", "b", "c' implicitly,
+ just like `indicator_column`, while `input_layer` explicitly requires wrapping
+ each of categorical columns with an `embedding_column` or an
+ `indicator_column`.
+
+ Example of usage:
+
+ ```python
+ price = numeric_column('price')
+ price_buckets = bucketized_column(price, boundaries=[0., 10., 100., 1000.])
+ keywords = categorical_column_with_hash_bucket("keywords", 10K)
+ keywords_price = crossed_column('keywords', price_buckets, ...)
+ columns = [price_buckets, keywords, keywords_price ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ prediction = linear_model(features, columns)
+ ```
+
+ Args:
+ features: A mapping from key to tensors. `_FeatureColumn`s look up via these
+ keys. For example `numeric_column('price')` will look at 'price' key in
+ this dict. Values are `Tensor` or `SparseTensor` depending on
+ corresponding `_FeatureColumn`.
+ feature_columns: An iterable containing the FeatureColumns to use as inputs
+ to your model. All items should be instances of classes derived from
+ `_FeatureColumn`s.
+ units: An integer, dimensionality of the output space. Default value is 1.
+ sparse_combiner: A string specifying how to reduce if a categorical column
+ is multivalent. Except `numeric_column`, almost all columns passed to
+ `linear_model` are considered as categorical columns. It combines each
+ categorical column independently. Currently "mean", "sqrtn" and "sum" are
+ supported, with "sum" the default for linear model. "sqrtn" often achieves
+ good accuracy, in particular with bag-of-words columns.
+ * "sum": do not normalize features in the column
+ * "mean": do l1 normalization on features in the column
+ * "sqrtn": do l2 normalization on features in the column
+ For example, for two features represented as the categorical columns:
+
+ ```python
+ # Feature 1
+
+ shape = [2, 2]
+ {
+ [0, 0]: "a"
+ [0, 1]: "b"
+ [1, 0]: "c"
+ }
+
+ # Feature 2
+
+ shape = [2, 3]
+ {
+ [0, 0]: "d"
+ [1, 0]: "e"
+ [1, 1]: "f"
+ [1, 2]: "g"
+ }
+ ```
+ with `sparse_combiner` as "mean", the linear model outputs conceptly are:
+ ```
+ y_0 = 1.0 / 2.0 * ( w_a + w_ b) + w_c + b_0
+ y_1 = w_d + 1.0 / 3.0 * ( w_e + w_ f + w_g) + b_1
+ ```
+ where `y_i` is the output, `b_i` is the bias, and `w_x` is the weight
+ assigned to the presence of `x` in the input features.
+ weight_collections: A list of collection names to which the Variable will be
+ added. Note that, variables will also be added to collections
+ `tf.GraphKeys.GLOBAL_VARIABLES` and `ops.GraphKeys.MODEL_VARIABLES`.
+ trainable: If `True` also add the variable to the graph collection
+ `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
+ cols_to_vars: If not `None`, must be a dictionary that will be filled with a
+ mapping from `_FeatureColumn` to associated list of `Variable`s. For
+ example, after the call, we might have cols_to_vars = {
+ _NumericColumn(
+ key='numeric_feature1', shape=(1,):
+ [<tf.Variable 'linear_model/price2/weights:0' shape=(1, 1)>],
+ 'bias': [<tf.Variable 'linear_model/bias_weights:0' shape=(1,)>],
+ _NumericColumn(
+ key='numeric_feature2', shape=(2,)):
+ [<tf.Variable 'linear_model/price1/weights:0' shape=(2, 1)>]}
+ If a column creates no variables, its value will be an empty list. Note
+ that cols_to_vars will also contain a string key 'bias' that maps to a
+ list of Variables.
+
+ Returns:
+ A `Tensor` which represents predictions/logits of a linear model. Its shape
+ is (batch_size, units) and its dtype is `float32`.
+
+ Raises:
+ ValueError: if an item in `feature_columns` is neither a `_DenseColumn`
+ nor `_CategoricalColumn`.
+ """
+ with variable_scope.variable_scope(None, 'linear_model') as vs:
+ model_name = _strip_leading_slashes(vs.name)
+ linear_model_layer = _LinearModel(
+ feature_columns=feature_columns,
+ units=units,
+ sparse_combiner=sparse_combiner,
+ weight_collections=weight_collections,
+ trainable=trainable,
+ name=model_name)
+ retval = linear_model_layer(features) # pylint: disable=not-callable
+ if cols_to_vars is not None:
+ cols_to_vars.update(linear_model_layer.cols_to_vars())
+ return retval
+
+
+def _add_to_collections(var, weight_collections):
+ """Adds a var to the list of weight_collections provided.
+
+ Handles the case for partitioned and non-partitioned variables.
+
+ Args:
+ var: A variable or Partitioned Variable.
+ weight_collections: List of collections to add variable to.
+ """
+ for weight_collection in weight_collections:
+ # The layer self.add_variable call already adds it to GLOBAL_VARIABLES.
+ if weight_collection == ops.GraphKeys.GLOBAL_VARIABLES:
+ continue
+ # TODO(rohanj): Explore adding a _get_variable_list method on `Variable`
+ # so that we don't have to do this check.
+ if isinstance(var, variables.PartitionedVariable):
+ for constituent_var in list(var):
+ ops.add_to_collection(weight_collection, constituent_var)
+ else:
+ ops.add_to_collection(weight_collection, var)
+
+
+class _FCLinearWrapper(base.Layer):
+ """Wraps a _FeatureColumn in a layer for use in a linear model.
+
+ See `linear_model` above.
+ """
+
+ def __init__(self,
+ feature_column,
+ units=1,
+ sparse_combiner='sum',
+ weight_collections=None,
+ trainable=True,
+ name=None,
+ **kwargs):
+ super(_FCLinearWrapper, self).__init__(
+ trainable=trainable, name=name, **kwargs)
+ self._feature_column = feature_column
+ self._units = units
+ self._sparse_combiner = sparse_combiner
+ self._weight_collections = weight_collections
+
+ def build(self, _):
+ if isinstance(self._feature_column, fc_old._CategoricalColumn): # pylint: disable=protected-access
+ weight = self.add_variable(
+ name='weights',
+ shape=(self._feature_column._num_buckets, self._units), # pylint: disable=protected-access
+ initializer=init_ops.zeros_initializer(),
+ trainable=self.trainable)
+ else:
+ num_elements = self._feature_column._variable_shape.num_elements() # pylint: disable=protected-access
+ weight = self.add_variable(
+ name='weights',
+ shape=[num_elements, self._units],
+ initializer=init_ops.zeros_initializer(),
+ trainable=self.trainable)
+ _add_to_collections(weight, self._weight_collections)
+ self._weight_var = weight
+ self.built = True
+
+ def call(self, builder):
+ weighted_sum = fc_old._create_weighted_sum( # pylint: disable=protected-access
+ column=self._feature_column,
+ builder=builder,
+ units=self._units,
+ sparse_combiner=self._sparse_combiner,
+ weight_collections=self._weight_collections,
+ trainable=self.trainable,
+ weight_var=self._weight_var)
+ return weighted_sum
+
+
+class _BiasLayer(base.Layer):
+ """A layer for the bias term.
+ """
+
+ def __init__(self,
+ units=1,
+ trainable=True,
+ weight_collections=None,
+ name=None,
+ **kwargs):
+ super(_BiasLayer, self).__init__(trainable=trainable, name=name, **kwargs)
+ self._units = units
+ self._weight_collections = weight_collections
+
+ def build(self, _):
+ self._bias_variable = self.add_variable(
+ 'bias_weights',
+ shape=[self._units],
+ initializer=init_ops.zeros_initializer(),
+ trainable=self.trainable)
+ _add_to_collections(self._bias_variable, self._weight_collections)
+ self.built = True
+
+ def call(self, _):
+ return self._bias_variable
+
+
+def _get_expanded_variable_list(variable):
+ if (isinstance(variable, variables.Variable) or
+ resource_variable_ops.is_resource_variable(variable)):
+ return [variable] # Single variable case.
+ else: # Must be a PartitionedVariable, so convert into a list.
+ return list(variable)
+
+
+def _strip_leading_slashes(name):
+ return name.rsplit('/', 1)[-1]
+
+
+class _LinearModel(training.Model):
+ """Creates a linear model using feature columns.
+
+ See `linear_model` for details.
+ """
+
+ def __init__(self,
+ feature_columns,
+ units=1,
+ sparse_combiner='sum',
+ weight_collections=None,
+ trainable=True,
+ name=None,
+ **kwargs):
+ super(_LinearModel, self).__init__(name=name, **kwargs)
+ self._feature_columns = fc_old._normalize_feature_columns( # pylint: disable=protected-access
+ feature_columns)
+ self._weight_collections = list(weight_collections or [])
+ if ops.GraphKeys.GLOBAL_VARIABLES not in self._weight_collections:
+ self._weight_collections.append(ops.GraphKeys.GLOBAL_VARIABLES)
+ if ops.GraphKeys.MODEL_VARIABLES not in self._weight_collections:
+ self._weight_collections.append(ops.GraphKeys.MODEL_VARIABLES)
+
+ column_layers = {}
+ for column in sorted(self._feature_columns, key=lambda x: x.name):
+ with variable_scope.variable_scope(
+ None, default_name=column._var_scope_name) as vs: # pylint: disable=protected-access
+ # Having the fully expressed variable scope name ends up doubly
+ # expressing the outer scope (scope with which this method was called)
+ # in the name of the variable that would get created.
+ column_name = _strip_leading_slashes(vs.name)
+ column_layer = _FCLinearWrapper(column, units, sparse_combiner,
+ self._weight_collections, trainable,
+ column_name, **kwargs)
+ column_layers[column_name] = column_layer
+ self._column_layers = self._add_layers(column_layers)
+ self._bias_layer = _BiasLayer(
+ units=units,
+ trainable=trainable,
+ weight_collections=self._weight_collections,
+ name='bias_layer',
+ **kwargs)
+ self._cols_to_vars = {}
+
+ def cols_to_vars(self):
+ """Returns a dict mapping _FeatureColumns to variables.
+
+ See `linear_model` for more information.
+ This is not populated till `call` is called i.e. layer is built.
+ """
+ return self._cols_to_vars
+
+ def call(self, features):
+ with variable_scope.variable_scope(self.name):
+ for column in self._feature_columns:
+ if not isinstance(
+ column,
+ (
+ fc_old._DenseColumn, # pylint: disable=protected-access
+ fc_old._CategoricalColumn)): # pylint: disable=protected-access
+ raise ValueError(
+ 'Items of feature_columns must be either a '
+ '_DenseColumn or _CategoricalColumn. Given: {}'.format(column))
+ weighted_sums = []
+ ordered_columns = []
+ builder = fc_old._LazyBuilder(features) # pylint: disable=protected-access
+ for layer in sorted(self._column_layers.values(), key=lambda x: x.name):
+ column = layer._feature_column # pylint: disable=protected-access
+ ordered_columns.append(column)
+ weighted_sum = layer(builder)
+ weighted_sums.append(weighted_sum)
+ self._cols_to_vars[column] = ops.get_collection(
+ ops.GraphKeys.GLOBAL_VARIABLES, scope=layer.scope_name)
+
+ _verify_static_batch_size_equality(weighted_sums, ordered_columns)
+ predictions_no_bias = math_ops.add_n(
+ weighted_sums, name='weighted_sum_no_bias')
+ predictions = nn_ops.bias_add(
+ predictions_no_bias,
+ self._bias_layer( # pylint: disable=not-callable
+ builder,
+ scope=variable_scope.get_variable_scope()), # pylint: disable=not-callable
+ name='weighted_sum')
+ bias = self._bias_layer.variables[0]
+ self._cols_to_vars['bias'] = _get_expanded_variable_list(bias)
+ return predictions
+
+ def _add_layers(self, layers):
+ # "Magic" required for keras.Model classes to track all the variables in
+ # a list of layers.Layer objects.
+ # TODO(ashankar): Figure out API so user code doesn't have to do this.
+ for name, layer in layers.items():
+ setattr(self, 'layer-%s' % name, layer)
+ return layers
+
+
+def _transform_features(features, feature_columns, state_manager):
+ """Returns transformed features based on features columns passed in.
+
+ Please note that most probably you would not need to use this function. Please
+ check `input_layer` and `linear_model` to see whether they will
+ satisfy your use case or not.
+
+ Example:
+
+ ```python
+ # Define features and transformations
+ crosses_a_x_b = crossed_column(
+ columns=["sparse_feature_a", "sparse_feature_b"], hash_bucket_size=10000)
+ price_buckets = bucketized_column(
+ source_column=numeric_column("price"), boundaries=[...])
+
+ columns = [crosses_a_x_b, price_buckets]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ transformed = transform_features(features=features, feature_columns=columns)
+
+ assertCountEqual(columns, transformed.keys())
+ ```
+
+ Args:
+ features: A mapping from key to tensors. `FeatureColumn`s look up via these
+ keys. For example `numeric_column('price')` will look at 'price' key in
+ this dict. Values can be a `SparseTensor` or a `Tensor` depends on
+ corresponding `FeatureColumn`.
+ feature_columns: An iterable containing all the `FeatureColumn`s.
+ state_manager: A StateManager object that holds the FeatureColumn state.
+
+ Returns:
+ A `dict` mapping `FeatureColumn` to `Tensor` and `SparseTensor` values.
+ """
+ feature_columns = _normalize_feature_columns(feature_columns)
+ outputs = {}
+ with ops.name_scope(
+ None, default_name='transform_features', values=features.values()):
+ transformation_cache = FeatureTransformationCache(features)
+ for column in sorted(feature_columns, key=lambda x: x.name):
+ with ops.name_scope(None, default_name=column.name):
+ outputs[column] = transformation_cache.get(column, state_manager)
+ return outputs
+
+
+def make_parse_example_spec(feature_columns):
+ """Creates parsing spec dictionary from input feature_columns.
+
+ The returned dictionary can be used as arg 'features' in `tf.parse_example`.
+
+ Typical usage example:
+
+ ```python
+ # Define features and transformations
+ feature_a = categorical_column_with_vocabulary_file(...)
+ feature_b = numeric_column(...)
+ feature_c_bucketized = bucketized_column(numeric_column("feature_c"), ...)
+ feature_a_x_feature_c = crossed_column(
+ columns=["feature_a", feature_c_bucketized], ...)
+
+ feature_columns = set(
+ [feature_b, feature_c_bucketized, feature_a_x_feature_c])
+ features = tf.parse_example(
+ serialized=serialized_examples,
+ features=make_parse_example_spec(feature_columns))
+ ```
+
+ For the above example, make_parse_example_spec would return the dict:
+
+ ```python
+ {
+ "feature_a": parsing_ops.VarLenFeature(tf.string),
+ "feature_b": parsing_ops.FixedLenFeature([1], dtype=tf.float32),
+ "feature_c": parsing_ops.FixedLenFeature([1], dtype=tf.float32)
+ }
+ ```
+
+ Args:
+ feature_columns: An iterable containing all feature columns. All items
+ should be instances of classes derived from `FeatureColumn`.
+
+ Returns:
+ A dict mapping each feature key to a `FixedLenFeature` or `VarLenFeature`
+ value.
+
+ Raises:
+ ValueError: If any of the given `feature_columns` is not a `FeatureColumn`
+ instance.
+ """
+ result = {}
+ for column in feature_columns:
+ if not isinstance(column, FeatureColumn):
+ raise ValueError('All feature_columns must be FeatureColumn instances. '
+ 'Given: {}'.format(column))
+ config = column.parse_example_spec
+ for key, value in six.iteritems(config):
+ if key in result and value != result[key]:
+ raise ValueError(
+ 'feature_columns contain different parse_spec for key '
+ '{}. Given {} and {}'.format(key, value, result[key]))
+ result.update(config)
+ return result
+
+
+def embedding_column(
+ categorical_column, dimension, combiner='mean', initializer=None,
+ ckpt_to_load_from=None, tensor_name_in_ckpt=None, max_norm=None,
+ trainable=True):
+ """`_DenseColumn` that converts from sparse, categorical input.
+
+ Use this when your inputs are sparse, but you want to convert them to a dense
+ representation (e.g., to feed to a DNN).
+
+ Inputs must be a `_CategoricalColumn` created by any of the
+ `categorical_column_*` function. Here is an example of using
+ `embedding_column` with `DNNClassifier`:
+
+ ```python
+ video_id = categorical_column_with_identity(
+ key='video_id', num_buckets=1000000, default_value=0)
+ columns = [embedding_column(video_id, 9),...]
+
+ estimator = tf.estimator.DNNClassifier(feature_columns=columns, ...)
+
+ label_column = ...
+ def input_fn():
+ features = tf.parse_example(
+ ..., features=make_parse_example_spec(columns + [label_column]))
+ labels = features.pop(label_column.name)
+ return features, labels
+
+ estimator.train(input_fn=input_fn, steps=100)
+ ```
+
+ Here is an example using `embedding_column` with model_fn:
+
+ ```python
+ def model_fn(features, ...):
+ video_id = categorical_column_with_identity(
+ key='video_id', num_buckets=1000000, default_value=0)
+ columns = [embedding_column(video_id, 9),...]
+ dense_tensor = input_layer(features, columns)
+ # Form DNN layers, calculate loss, and return EstimatorSpec.
+ ...
+ ```
+
+ Args:
+ categorical_column: A `_CategoricalColumn` created by a
+ `categorical_column_with_*` function. This column produces the sparse IDs
+ that are inputs to the embedding lookup.
+ dimension: An integer specifying dimension of the embedding, must be > 0.
+ combiner: A string specifying how to reduce if there are multiple entries
+ in a single row. Currently 'mean', 'sqrtn' and 'sum' are supported, with
+ 'mean' the default. 'sqrtn' often achieves good accuracy, in particular
+ with bag-of-words columns. Each of this can be thought as example level
+ normalizations on the column. For more information, see
+ `tf.embedding_lookup_sparse`.
+ initializer: A variable initializer function to be used in embedding
+ variable initialization. If not specified, defaults to
+ `tf.truncated_normal_initializer` with mean `0.0` and standard deviation
+ `1/sqrt(dimension)`.
+ ckpt_to_load_from: String representing checkpoint name/pattern from which to
+ restore column weights. Required if `tensor_name_in_ckpt` is not `None`.
+ tensor_name_in_ckpt: Name of the `Tensor` in `ckpt_to_load_from` from
+ which to restore the column weights. Required if `ckpt_to_load_from` is
+ not `None`.
+ max_norm: If not `None`, embedding values are l2-normalized to this value.
+ trainable: Whether or not the embedding is trainable. Default is True.
+
+ Returns:
+ `_DenseColumn` that converts from sparse input.
+
+ Raises:
+ ValueError: if `dimension` not > 0.
+ ValueError: if exactly one of `ckpt_to_load_from` and `tensor_name_in_ckpt`
+ is specified.
+ ValueError: if `initializer` is specified and is not callable.
+ RuntimeError: If eager execution is enabled.
+ """
+ if (dimension is None) or (dimension < 1):
+ raise ValueError('Invalid dimension {}.'.format(dimension))
+ if (ckpt_to_load_from is None) != (tensor_name_in_ckpt is None):
+ raise ValueError('Must specify both `ckpt_to_load_from` and '
+ '`tensor_name_in_ckpt` or none of them.')
+
+ if (initializer is not None) and (not callable(initializer)):
+ raise ValueError('initializer must be callable if specified. '
+ 'Embedding of column_name: {}'.format(
+ categorical_column.name))
+ if initializer is None:
+ initializer = init_ops.truncated_normal_initializer(
+ mean=0.0, stddev=1 / math.sqrt(dimension))
+
+ return EmbeddingColumn(
+ categorical_column=categorical_column,
+ dimension=dimension,
+ combiner=combiner,
+ initializer=initializer,
+ ckpt_to_load_from=ckpt_to_load_from,
+ tensor_name_in_ckpt=tensor_name_in_ckpt,
+ max_norm=max_norm,
+ trainable=trainable)
+
+
+def shared_embedding_columns(
+ categorical_columns, dimension, combiner='mean', initializer=None,
+ shared_embedding_collection_name=None, ckpt_to_load_from=None,
+ tensor_name_in_ckpt=None, max_norm=None, trainable=True):
+ """List of dense columns that convert from sparse, categorical input.
+
+ This is similar to `embedding_column`, except that it produces a list of
+ embedding columns that share the same embedding weights.
+
+ Use this when your inputs are sparse and of the same type (e.g. watched and
+ impression video IDs that share the same vocabulary), and you want to convert
+ them to a dense representation (e.g., to feed to a DNN).
+
+ Inputs must be a list of categorical columns created by any of the
+ `categorical_column_*` function. They must all be of the same type and have
+ the same arguments except `key`. E.g. they can be
+ categorical_column_with_vocabulary_file with the same vocabulary_file. Some or
+ all columns could also be weighted_categorical_column.
+
+ Here is an example embedding of two features for a DNNClassifier model:
+
+ ```python
+ watched_video_id = categorical_column_with_vocabulary_file(
+ 'watched_video_id', video_vocabulary_file, video_vocabulary_size)
+ impression_video_id = categorical_column_with_vocabulary_file(
+ 'impression_video_id', video_vocabulary_file, video_vocabulary_size)
+ columns = shared_embedding_columns(
+ [watched_video_id, impression_video_id], dimension=10)
+
+ estimator = tf.estimator.DNNClassifier(feature_columns=columns, ...)
+
+ label_column = ...
+ def input_fn():
+ features = tf.parse_example(
+ ..., features=make_parse_example_spec(columns + [label_column]))
+ labels = features.pop(label_column.name)
+ return features, labels
+
+ estimator.train(input_fn=input_fn, steps=100)
+ ```
+
+ Here is an example using `shared_embedding_columns` with model_fn:
+
+ ```python
+ def model_fn(features, ...):
+ watched_video_id = categorical_column_with_vocabulary_file(
+ 'watched_video_id', video_vocabulary_file, video_vocabulary_size)
+ impression_video_id = categorical_column_with_vocabulary_file(
+ 'impression_video_id', video_vocabulary_file, video_vocabulary_size)
+ columns = shared_embedding_columns(
+ [watched_video_id, impression_video_id], dimension=10)
+ dense_tensor = input_layer(features, columns)
+ # Form DNN layers, calculate loss, and return EstimatorSpec.
+ ...
+ ```
+
+ Args:
+ categorical_columns: List of categorical columns created by a
+ `categorical_column_with_*` function. These columns produce the sparse IDs
+ that are inputs to the embedding lookup. All columns must be of the same
+ type and have the same arguments except `key`. E.g. they can be
+ categorical_column_with_vocabulary_file with the same vocabulary_file.
+ Some or all columns could also be weighted_categorical_column.
+ dimension: An integer specifying dimension of the embedding, must be > 0.
+ combiner: A string specifying how to reduce if there are multiple entries
+ in a single row. Currently 'mean', 'sqrtn' and 'sum' are supported, with
+ 'mean' the default. 'sqrtn' often achieves good accuracy, in particular
+ with bag-of-words columns. Each of this can be thought as example level
+ normalizations on the column. For more information, see
+ `tf.embedding_lookup_sparse`.
+ initializer: A variable initializer function to be used in embedding
+ variable initialization. If not specified, defaults to
+ `tf.truncated_normal_initializer` with mean `0.0` and standard deviation
+ `1/sqrt(dimension)`.
+ shared_embedding_collection_name: Optional collective name of these columns.
+ If not given, a reasonable name will be chosen based on the names of
+ `categorical_columns`.
+ ckpt_to_load_from: String representing checkpoint name/pattern from which to
+ restore column weights. Required if `tensor_name_in_ckpt` is not `None`.
+ tensor_name_in_ckpt: Name of the `Tensor` in `ckpt_to_load_from` from
+ which to restore the column weights. Required if `ckpt_to_load_from` is
+ not `None`.
+ max_norm: If not `None`, each embedding is clipped if its l2-norm is
+ larger than this value, before combining.
+ trainable: Whether or not the embedding is trainable. Default is True.
+
+ Returns:
+ A list of dense columns that converts from sparse input. The order of
+ results follows the ordering of `categorical_columns`.
+
+ Raises:
+ ValueError: if `dimension` not > 0.
+ ValueError: if any of the given `categorical_columns` is of different type
+ or has different arguments than the others.
+ ValueError: if exactly one of `ckpt_to_load_from` and `tensor_name_in_ckpt`
+ is specified.
+ ValueError: if `initializer` is specified and is not callable.
+ RuntimeError: if eager execution is enabled.
+ """
+ if context.executing_eagerly():
+ raise RuntimeError('shared_embedding_columns are not supported when eager '
+ 'execution is enabled.')
+
+ if (dimension is None) or (dimension < 1):
+ raise ValueError('Invalid dimension {}.'.format(dimension))
+ if (ckpt_to_load_from is None) != (tensor_name_in_ckpt is None):
+ raise ValueError('Must specify both `ckpt_to_load_from` and '
+ '`tensor_name_in_ckpt` or none of them.')
+
+ if (initializer is not None) and (not callable(initializer)):
+ raise ValueError('initializer must be callable if specified.')
+ if initializer is None:
+ initializer = init_ops.truncated_normal_initializer(
+ mean=0.0, stddev=1. / math.sqrt(dimension))
+
+ # Sort the columns so the default collection name is deterministic even if the
+ # user passes columns from an unsorted collection, such as dict.values().
+ sorted_columns = sorted(categorical_columns, key=lambda x: x.name)
+
+ c0 = sorted_columns[0]
+ num_buckets = c0.num_buckets
+ if not isinstance(c0, CategoricalColumn):
+ raise ValueError(
+ 'All categorical_columns must be subclasses of CategoricalColumn. '
+ 'Given: {}, of type: {}'.format(c0, type(c0)))
+ if isinstance(c0, WeightedCategoricalColumn):
+ c0 = c0.categorical_column
+ for c in sorted_columns[1:]:
+ if isinstance(c, WeightedCategoricalColumn):
+ c = c.categorical_column
+ if not isinstance(c, type(c0)):
+ raise ValueError(
+ 'To use shared_embedding_column, all categorical_columns must have '
+ 'the same type, or be weighted_categorical_column of the same type. '
+ 'Given column: {} of type: {} does not match given column: {} of '
+ 'type: {}'.format(c0, type(c0), c, type(c)))
+ if num_buckets != c.num_buckets:
+ raise ValueError(
+ 'To use shared_embedding_column, all categorical_columns must have '
+ 'the same number of buckets. Given column: {} with buckets: {} does '
+ 'not match column: {} with buckets: {}'.format(
+ c0, num_buckets, c, c.num_buckets))
+
+ if not shared_embedding_collection_name:
+ shared_embedding_collection_name = '_'.join(c.name for c in sorted_columns)
+ shared_embedding_collection_name += '_shared_embedding'
+
+ result = []
+ for column in categorical_columns:
+ result.append(
+ SharedEmbeddingColumn(
+ categorical_column=column,
+ initializer=initializer,
+ dimension=dimension,
+ combiner=combiner,
+ shared_embedding_collection_name=shared_embedding_collection_name,
+ ckpt_to_load_from=ckpt_to_load_from,
+ tensor_name_in_ckpt=tensor_name_in_ckpt,
+ max_norm=max_norm,
+ trainable=trainable))
+
+ return result
+
+
+def numeric_column(key,
+ shape=(1,),
+ default_value=None,
+ dtype=dtypes.float32,
+ normalizer_fn=None):
+ """Represents real valued or numerical features.
+
+ Example:
+
+ ```python
+ price = numeric_column('price')
+ columns = [price, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ dense_tensor = input_layer(features, columns)
+
+ # or
+ bucketized_price = bucketized_column(price, boundaries=[...])
+ columns = [bucketized_price, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ linear_prediction = linear_model(features, columns)
+ ```
+
+ Args:
+ key: A unique string identifying the input feature. It is used as the
+ column name and the dictionary key for feature parsing configs, feature
+ `Tensor` objects, and feature columns.
+ shape: An iterable of integers specifies the shape of the `Tensor`. An
+ integer can be given which means a single dimension `Tensor` with given
+ width. The `Tensor` representing the column will have the shape of
+ [batch_size] + `shape`.
+ default_value: A single value compatible with `dtype` or an iterable of
+ values compatible with `dtype` which the column takes on during
+ `tf.Example` parsing if data is missing. A default value of `None` will
+ cause `tf.parse_example` to fail if an example does not contain this
+ column. If a single value is provided, the same value will be applied as
+ the default value for every item. If an iterable of values is provided,
+ the shape of the `default_value` should be equal to the given `shape`.
+ dtype: defines the type of values. Default value is `tf.float32`. Must be a
+ non-quantized, real integer or floating point type.
+ normalizer_fn: If not `None`, a function that can be used to normalize the
+ value of the tensor after `default_value` is applied for parsing.
+ Normalizer function takes the input `Tensor` as its argument, and returns
+ the output `Tensor`. (e.g. lambda x: (x - 3.0) / 4.2). Please note that
+ even though the most common use case of this function is normalization, it
+ can be used for any kind of Tensorflow transformations.
+
+ Returns:
+ A `NumericColumn`.
+
+ Raises:
+ TypeError: if any dimension in shape is not an int
+ ValueError: if any dimension in shape is not a positive integer
+ TypeError: if `default_value` is an iterable but not compatible with `shape`
+ TypeError: if `default_value` is not compatible with `dtype`.
+ ValueError: if `dtype` is not convertible to `tf.float32`.
+ """
+ shape = _check_shape(shape, key)
+ if not (dtype.is_integer or dtype.is_floating):
+ raise ValueError('dtype must be convertible to float. '
+ 'dtype: {}, key: {}'.format(dtype, key))
+ default_value = _check_default_value(shape, default_value, dtype, key)
+
+ if normalizer_fn is not None and not callable(normalizer_fn):
+ raise TypeError(
+ 'normalizer_fn must be a callable. Given: {}'.format(normalizer_fn))
+
+ _assert_key_is_string(key)
+ return NumericColumn(
+ key,
+ shape=shape,
+ default_value=default_value,
+ dtype=dtype,
+ normalizer_fn=normalizer_fn)
+
+
+def bucketized_column(source_column, boundaries):
+ """Represents discretized dense input.
+
+ Buckets include the left boundary, and exclude the right boundary. Namely,
+ `boundaries=[0., 1., 2.]` generates buckets `(-inf, 0.)`, `[0., 1.)`,
+ `[1., 2.)`, and `[2., +inf)`.
+
+ For example, if the inputs are
+
+ ```python
+ boundaries = [0, 10, 100]
+ input tensor = [[-5, 10000]
+ [150, 10]
+ [5, 100]]
+ ```
+
+ then the output will be
+
+ ```python
+ output = [[0, 3]
+ [3, 2]
+ [1, 3]]
+ ```
+
+ Example:
+
+ ```python
+ price = numeric_column('price')
+ bucketized_price = bucketized_column(price, boundaries=[...])
+ columns = [bucketized_price, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ linear_prediction = linear_model(features, columns)
+
+ # or
+ columns = [bucketized_price, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ dense_tensor = input_layer(features, columns)
+ ```
+
+ `bucketized_column` can also be crossed with another categorical column using
+ `crossed_column`:
+
+ ```python
+ price = numeric_column('price')
+ # bucketized_column converts numerical feature to a categorical one.
+ bucketized_price = bucketized_column(price, boundaries=[...])
+ # 'keywords' is a string feature.
+ price_x_keywords = crossed_column([bucketized_price, 'keywords'], 50K)
+ columns = [price_x_keywords, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ linear_prediction = linear_model(features, columns)
+ ```
+
+ Args:
+ source_column: A one-dimensional dense column which is generated with
+ `numeric_column`.
+ boundaries: A sorted list or tuple of floats specifying the boundaries.
+
+ Returns:
+ A `BucketizedColumn`.
+
+ Raises:
+ ValueError: If `source_column` is not a numeric column, or if it is not
+ one-dimensional.
+ ValueError: If `boundaries` is not a sorted list or tuple.
+ """
+ if not isinstance(source_column, NumericColumn):
+ raise ValueError(
+ 'source_column must be a column generated with numeric_column(). '
+ 'Given: {}'.format(source_column))
+ if len(source_column.shape) > 1:
+ raise ValueError(
+ 'source_column must be one-dimensional column. '
+ 'Given: {}'.format(source_column))
+ if (not boundaries or
+ not (isinstance(boundaries, list) or isinstance(boundaries, tuple))):
+ raise ValueError('boundaries must be a sorted list.')
+ for i in range(len(boundaries) - 1):
+ if boundaries[i] >= boundaries[i + 1]:
+ raise ValueError('boundaries must be a sorted list.')
+ return BucketizedColumn(source_column, tuple(boundaries))
+
+
+def _assert_string_or_int(dtype, prefix):
+ if (dtype != dtypes.string) and (not dtype.is_integer):
+ raise ValueError(
+ '{} dtype must be string or integer. dtype: {}.'.format(prefix, dtype))
+
+
+def _assert_key_is_string(key):
+ if not isinstance(key, six.string_types):
+ raise ValueError(
+ 'key must be a string. Got: type {}. Given key: {}.'.format(
+ type(key), key))
+
+
+def categorical_column_with_hash_bucket(key,
+ hash_bucket_size,
+ dtype=dtypes.string):
+ """Represents sparse feature where ids are set by hashing.
+
+ Use this when your sparse features are in string or integer format, and you
+ want to distribute your inputs into a finite number of buckets by hashing.
+ output_id = Hash(input_feature_string) % bucket_size for string type input.
+ For int type input, the value is converted to its string representation first
+ and then hashed by the same formula.
+
+ For input dictionary `features`, `features[key]` is either `Tensor` or
+ `SparseTensor`. If `Tensor`, missing values can be represented by `-1` for int
+ and `''` for string, which will be dropped by this feature column.
+
+ Example:
+
+ ```python
+ keywords = categorical_column_with_hash_bucket("keywords", 10K)
+ columns = [keywords, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ linear_prediction = linear_model(features, columns)
+
+ # or
+ keywords_embedded = embedding_column(keywords, 16)
+ columns = [keywords_embedded, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ dense_tensor = input_layer(features, columns)
+ ```
+
+ Args:
+ key: A unique string identifying the input feature. It is used as the
+ column name and the dictionary key for feature parsing configs, feature
+ `Tensor` objects, and feature columns.
+ hash_bucket_size: An int > 1. The number of buckets.
+ dtype: The type of features. Only string and integer types are supported.
+
+ Returns:
+ A `HashedCategoricalColumn`.
+
+ Raises:
+ ValueError: `hash_bucket_size` is not greater than 1.
+ ValueError: `dtype` is neither string nor integer.
+ """
+ if hash_bucket_size is None:
+ raise ValueError('hash_bucket_size must be set. ' 'key: {}'.format(key))
+
+ if hash_bucket_size < 1:
+ raise ValueError('hash_bucket_size must be at least 1. '
+ 'hash_bucket_size: {}, key: {}'.format(
+ hash_bucket_size, key))
+
+ _assert_key_is_string(key)
+ _assert_string_or_int(dtype, prefix='column_name: {}'.format(key))
+
+ return HashedCategoricalColumn(key, hash_bucket_size, dtype)
+
+
+def categorical_column_with_vocabulary_file(key,
+ vocabulary_file,
+ vocabulary_size=None,
+ num_oov_buckets=0,
+ default_value=None,
+ dtype=dtypes.string):
+ """A `CategoricalColumn` with a vocabulary file.
+
+ Use this when your inputs are in string or integer format, and you have a
+ vocabulary file that maps each value to an integer ID. By default,
+ out-of-vocabulary values are ignored. Use either (but not both) of
+ `num_oov_buckets` and `default_value` to specify how to include
+ out-of-vocabulary values.
+
+ For input dictionary `features`, `features[key]` is either `Tensor` or
+ `SparseTensor`. If `Tensor`, missing values can be represented by `-1` for int
+ and `''` for string, which will be dropped by this feature column.
+
+ Example with `num_oov_buckets`:
+ File '/us/states.txt' contains 50 lines, each with a 2-character U.S. state
+ abbreviation. All inputs with values in that file are assigned an ID 0-49,
+ corresponding to its line number. All other values are hashed and assigned an
+ ID 50-54.
+
+ ```python
+ states = categorical_column_with_vocabulary_file(
+ key='states', vocabulary_file='/us/states.txt', vocabulary_size=50,
+ num_oov_buckets=5)
+ columns = [states, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ linear_prediction = linear_model(features, columns)
+ ```
+
+ Example with `default_value`:
+ File '/us/states.txt' contains 51 lines - the first line is 'XX', and the
+ other 50 each have a 2-character U.S. state abbreviation. Both a literal 'XX'
+ in input, and other values missing from the file, will be assigned ID 0. All
+ others are assigned the corresponding line number 1-50.
+
+ ```python
+ states = categorical_column_with_vocabulary_file(
+ key='states', vocabulary_file='/us/states.txt', vocabulary_size=51,
+ default_value=0)
+ columns = [states, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ linear_prediction, _, _ = linear_model(features, columns)
+ ```
+
+ And to make an embedding with either:
+
+ ```python
+ columns = [embedding_column(states, 3),...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ dense_tensor = input_layer(features, columns)
+ ```
+
+ Args:
+ key: A unique string identifying the input feature. It is used as the
+ column name and the dictionary key for feature parsing configs, feature
+ `Tensor` objects, and feature columns.
+ vocabulary_file: The vocabulary file name.
+ vocabulary_size: Number of the elements in the vocabulary. This must be no
+ greater than length of `vocabulary_file`, if less than length, later
+ values are ignored. If None, it is set to the length of `vocabulary_file`.
+ num_oov_buckets: Non-negative integer, the number of out-of-vocabulary
+ buckets. All out-of-vocabulary inputs will be assigned IDs in the range
+ `[vocabulary_size, vocabulary_size+num_oov_buckets)` based on a hash of
+ the input value. A positive `num_oov_buckets` can not be specified with
+ `default_value`.
+ default_value: The integer ID value to return for out-of-vocabulary feature
+ values, defaults to `-1`. This can not be specified with a positive
+ `num_oov_buckets`.
+ dtype: The type of features. Only string and integer types are supported.
+
+ Returns:
+ A `CategoricalColumn` with a vocabulary file.
+
+ Raises:
+ ValueError: `vocabulary_file` is missing or cannot be opened.
+ ValueError: `vocabulary_size` is missing or < 1.
+ ValueError: `num_oov_buckets` is a negative integer.
+ ValueError: `num_oov_buckets` and `default_value` are both specified.
+ ValueError: `dtype` is neither string nor integer.
+ """
+ if not vocabulary_file:
+ raise ValueError('Missing vocabulary_file in {}.'.format(key))
+
+ if vocabulary_size is None:
+ if not gfile.Exists(vocabulary_file):
+ raise ValueError('vocabulary_file in {} does not exist.'.format(key))
+
+ with gfile.GFile(vocabulary_file) as f:
+ vocabulary_size = sum(1 for _ in f)
+ logging.info(
+ 'vocabulary_size = %d in %s is inferred from the number of elements '
+ 'in the vocabulary_file %s.', vocabulary_size, key, vocabulary_file)
+
+ # `vocabulary_size` isn't required for lookup, but it is for `_num_buckets`.
+ if vocabulary_size < 1:
+ raise ValueError('Invalid vocabulary_size in {}.'.format(key))
+ if num_oov_buckets:
+ if default_value is not None:
+ raise ValueError(
+ 'Can\'t specify both num_oov_buckets and default_value in {}.'.format(
+ key))
+ if num_oov_buckets < 0:
+ raise ValueError('Invalid num_oov_buckets {} in {}.'.format(
+ num_oov_buckets, key))
+ _assert_string_or_int(dtype, prefix='column_name: {}'.format(key))
+ _assert_key_is_string(key)
+ return VocabularyFileCategoricalColumn(
+ key=key,
+ vocabulary_file=vocabulary_file,
+ vocabulary_size=vocabulary_size,
+ num_oov_buckets=0 if num_oov_buckets is None else num_oov_buckets,
+ default_value=-1 if default_value is None else default_value,
+ dtype=dtype)
+
+
+def categorical_column_with_vocabulary_list(
+ key, vocabulary_list, dtype=None, default_value=-1, num_oov_buckets=0):
+ """A `_CategoricalColumn` with in-memory vocabulary.
+
+ Use this when your inputs are in string or integer format, and you have an
+ in-memory vocabulary mapping each value to an integer ID. By default,
+ out-of-vocabulary values are ignored. Use either (but not both) of
+ `num_oov_buckets` and `default_value` to specify how to include
+ out-of-vocabulary values.
+
+ For input dictionary `features`, `features[key]` is either `Tensor` or
+ `SparseTensor`. If `Tensor`, missing values can be represented by `-1` for int
+ and `''` for string, which will be dropped by this feature column.
+
+ Example with `num_oov_buckets`:
+ In the following example, each input in `vocabulary_list` is assigned an ID
+ 0-3 corresponding to its index (e.g., input 'B' produces output 2). All other
+ inputs are hashed and assigned an ID 4-5.
+
+ ```python
+ colors = categorical_column_with_vocabulary_list(
+ key='colors', vocabulary_list=('R', 'G', 'B', 'Y'),
+ num_oov_buckets=2)
+ columns = [colors, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ linear_prediction, _, _ = linear_model(features, columns)
+ ```
+
+ Example with `default_value`:
+ In the following example, each input in `vocabulary_list` is assigned an ID
+ 0-4 corresponding to its index (e.g., input 'B' produces output 3). All other
+ inputs are assigned `default_value` 0.
+
+
+ ```python
+ colors = categorical_column_with_vocabulary_list(
+ key='colors', vocabulary_list=('X', 'R', 'G', 'B', 'Y'), default_value=0)
+ columns = [colors, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ linear_prediction, _, _ = linear_model(features, columns)
+ ```
+
+ And to make an embedding with either:
+
+ ```python
+ columns = [embedding_column(colors, 3),...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ dense_tensor = input_layer(features, columns)
+ ```
+
+ Args:
+ key: A unique string identifying the input feature. It is used as the
+ column name and the dictionary key for feature parsing configs, feature
+ `Tensor` objects, and feature columns.
+ vocabulary_list: An ordered iterable defining the vocabulary. Each feature
+ is mapped to the index of its value (if present) in `vocabulary_list`.
+ Must be castable to `dtype`.
+ dtype: The type of features. Only string and integer types are supported.
+ If `None`, it will be inferred from `vocabulary_list`.
+ default_value: The integer ID value to return for out-of-vocabulary feature
+ values, defaults to `-1`. This can not be specified with a positive
+ `num_oov_buckets`.
+ num_oov_buckets: Non-negative integer, the number of out-of-vocabulary
+ buckets. All out-of-vocabulary inputs will be assigned IDs in the range
+ `[len(vocabulary_list), len(vocabulary_list)+num_oov_buckets)` based on a
+ hash of the input value. A positive `num_oov_buckets` can not be specified
+ with `default_value`.
+
+ Returns:
+ A `CategoricalColumn` with in-memory vocabulary.
+
+ Raises:
+ ValueError: if `vocabulary_list` is empty, or contains duplicate keys.
+ ValueError: `num_oov_buckets` is a negative integer.
+ ValueError: `num_oov_buckets` and `default_value` are both specified.
+ ValueError: if `dtype` is not integer or string.
+ """
+ if (vocabulary_list is None) or (len(vocabulary_list) < 1):
+ raise ValueError(
+ 'vocabulary_list {} must be non-empty, column_name: {}'.format(
+ vocabulary_list, key))
+ if len(set(vocabulary_list)) != len(vocabulary_list):
+ raise ValueError(
+ 'Duplicate keys in vocabulary_list {}, column_name: {}'.format(
+ vocabulary_list, key))
+ vocabulary_dtype = dtypes.as_dtype(np.array(vocabulary_list).dtype)
+ if num_oov_buckets:
+ if default_value != -1:
+ raise ValueError(
+ 'Can\'t specify both num_oov_buckets and default_value in {}.'.format(
+ key))
+ if num_oov_buckets < 0:
+ raise ValueError('Invalid num_oov_buckets {} in {}.'.format(
+ num_oov_buckets, key))
+ _assert_string_or_int(
+ vocabulary_dtype, prefix='column_name: {} vocabulary'.format(key))
+ if dtype is None:
+ dtype = vocabulary_dtype
+ elif dtype.is_integer != vocabulary_dtype.is_integer:
+ raise ValueError(
+ 'dtype {} and vocabulary dtype {} do not match, column_name: {}'.format(
+ dtype, vocabulary_dtype, key))
+ _assert_string_or_int(dtype, prefix='column_name: {}'.format(key))
+ _assert_key_is_string(key)
+
+ return VocabularyListCategoricalColumn(
+ key=key,
+ vocabulary_list=tuple(vocabulary_list),
+ dtype=dtype,
+ default_value=default_value,
+ num_oov_buckets=num_oov_buckets)
+
+
+def categorical_column_with_identity(key, num_buckets, default_value=None):
+ """A `CategoricalColumn` that returns identity values.
+
+ Use this when your inputs are integers in the range `[0, num_buckets)`, and
+ you want to use the input value itself as the categorical ID. Values outside
+ this range will result in `default_value` if specified, otherwise it will
+ fail.
+
+ Typically, this is used for contiguous ranges of integer indexes, but
+ it doesn't have to be. This might be inefficient, however, if many of IDs
+ are unused. Consider `categorical_column_with_hash_bucket` in that case.
+
+ For input dictionary `features`, `features[key]` is either `Tensor` or
+ `SparseTensor`. If `Tensor`, missing values can be represented by `-1` for int
+ and `''` for string, which will be dropped by this feature column.
+
+ In the following examples, each input in the range `[0, 1000000)` is assigned
+ the same value. All other inputs are assigned `default_value` 0. Note that a
+ literal 0 in inputs will result in the same default ID.
+
+ Linear model:
+
+ ```python
+ video_id = categorical_column_with_identity(
+ key='video_id', num_buckets=1000000, default_value=0)
+ columns = [video_id, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ linear_prediction, _, _ = linear_model(features, columns)
+ ```
+
+ Embedding for a DNN model:
+
+ ```python
+ columns = [embedding_column(video_id, 9),...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ dense_tensor = input_layer(features, columns)
+ ```
+
+ Args:
+ key: A unique string identifying the input feature. It is used as the
+ column name and the dictionary key for feature parsing configs, feature
+ `Tensor` objects, and feature columns.
+ num_buckets: Range of inputs and outputs is `[0, num_buckets)`.
+ default_value: If `None`, this column's graph operations will fail for
+ out-of-range inputs. Otherwise, this value must be in the range
+ `[0, num_buckets)`, and will replace inputs in that range.
+
+ Returns:
+ A `CategoricalColumn` that returns identity values.
+
+ Raises:
+ ValueError: if `num_buckets` is less than one.
+ ValueError: if `default_value` is not in range `[0, num_buckets)`.
+ """
+ if num_buckets < 1:
+ raise ValueError(
+ 'num_buckets {} < 1, column_name {}'.format(num_buckets, key))
+ if (default_value is not None) and (
+ (default_value < 0) or (default_value >= num_buckets)):
+ raise ValueError(
+ 'default_value {} not in range [0, {}), column_name {}'.format(
+ default_value, num_buckets, key))
+ _assert_key_is_string(key)
+ return IdentityCategoricalColumn(
+ key=key, number_buckets=num_buckets, default_value=default_value)
+
+
+def indicator_column(categorical_column):
+ """Represents multi-hot representation of given categorical column.
+
+ - For DNN model, `indicator_column` can be used to wrap any
+ `categorical_column_*` (e.g., to feed to DNN). Consider to Use
+ `embedding_column` if the number of buckets/unique(values) are large.
+
+ - For Wide (aka linear) model, `indicator_column` is the internal
+ representation for categorical column when passing categorical column
+ directly (as any element in feature_columns) to `linear_model`. See
+ `linear_model` for details.
+
+ ```python
+ name = indicator_column(categorical_column_with_vocabulary_list(
+ 'name', ['bob', 'george', 'wanda'])
+ columns = [name, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ dense_tensor = input_layer(features, columns)
+
+ dense_tensor == [[1, 0, 0]] # If "name" bytes_list is ["bob"]
+ dense_tensor == [[1, 0, 1]] # If "name" bytes_list is ["bob", "wanda"]
+ dense_tensor == [[2, 0, 0]] # If "name" bytes_list is ["bob", "bob"]
+ ```
+
+ Args:
+ categorical_column: A `CategoricalColumn` which is created by
+ `categorical_column_with_*` or `crossed_column` functions.
+
+ Returns:
+ An `IndicatorColumn`.
+ """
+ return IndicatorColumn(categorical_column)
+
+
+def weighted_categorical_column(
+ categorical_column, weight_feature_key, dtype=dtypes.float32):
+ """Applies weight values to a `_CategoricalColumn`.
+
+ Use this when each of your sparse inputs has both an ID and a value. For
+ example, if you're representing text documents as a collection of word
+ frequencies, you can provide 2 parallel sparse input features ('terms' and
+ 'frequencies' below).
+
+ Example:
+
+ Input `tf.Example` objects:
+
+ ```proto
+ [
+ features {
+ feature {
+ key: "terms"
+ value {bytes_list {value: "very" value: "model"}}
+ }
+ feature {
+ key: "frequencies"
+ value {float_list {value: 0.3 value: 0.1}}
+ }
+ },
+ features {
+ feature {
+ key: "terms"
+ value {bytes_list {value: "when" value: "course" value: "human"}}
+ }
+ feature {
+ key: "frequencies"
+ value {float_list {value: 0.4 value: 0.1 value: 0.2}}
+ }
+ }
+ ]
+ ```
+
+ ```python
+ categorical_column = categorical_column_with_hash_bucket(
+ column_name='terms', hash_bucket_size=1000)
+ weighted_column = weighted_categorical_column(
+ categorical_column=categorical_column, weight_feature_key='frequencies')
+ columns = [weighted_column, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ linear_prediction, _, _ = linear_model(features, columns)
+ ```
+
+ This assumes the input dictionary contains a `SparseTensor` for key
+ 'terms', and a `SparseTensor` for key 'frequencies'. These 2 tensors must have
+ the same indices and dense shape.
+
+ Args:
+ categorical_column: A `_CategoricalColumn` created by
+ `categorical_column_with_*` functions.
+ weight_feature_key: String key for weight values.
+ dtype: Type of weights, such as `tf.float32`. Only float and integer weights
+ are supported.
+
+ Returns:
+ A `CategoricalColumn` composed of two sparse features: one represents id,
+ the other represents weight (value) of the id feature in that example.
+
+ Raises:
+ ValueError: if `dtype` is not convertible to float.
+ """
+ if (dtype is None) or not (dtype.is_integer or dtype.is_floating):
+ raise ValueError('dtype {} is not convertible to float.'.format(dtype))
+ return WeightedCategoricalColumn(
+ categorical_column=categorical_column,
+ weight_feature_key=weight_feature_key,
+ dtype=dtype)
+
+
+def crossed_column(keys, hash_bucket_size, hash_key=None):
+ """Returns a column for performing crosses of categorical features.
+
+ Crossed features will be hashed according to `hash_bucket_size`. Conceptually,
+ the transformation can be thought of as:
+ Hash(cartesian product of features) % `hash_bucket_size`
+
+ For example, if the input features are:
+
+ * SparseTensor referred by first key:
+
+ ```python
+ shape = [2, 2]
+ {
+ [0, 0]: "a"
+ [1, 0]: "b"
+ [1, 1]: "c"
+ }
+ ```
+
+ * SparseTensor referred by second key:
+
+ ```python
+ shape = [2, 1]
+ {
+ [0, 0]: "d"
+ [1, 0]: "e"
+ }
+ ```
+
+ then crossed feature will look like:
+
+ ```python
+ shape = [2, 2]
+ {
+ [0, 0]: Hash64("d", Hash64("a")) % hash_bucket_size
+ [1, 0]: Hash64("e", Hash64("b")) % hash_bucket_size
+ [1, 1]: Hash64("e", Hash64("c")) % hash_bucket_size
+ }
+ ```
+
+ Here is an example to create a linear model with crosses of string features:
+
+ ```python
+ keywords_x_doc_terms = crossed_column(['keywords', 'doc_terms'], 50K)
+ columns = [keywords_x_doc_terms, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ linear_prediction = linear_model(features, columns)
+ ```
+
+ You could also use vocabulary lookup before crossing:
+
+ ```python
+ keywords = categorical_column_with_vocabulary_file(
+ 'keywords', '/path/to/vocabulary/file', vocabulary_size=1K)
+ keywords_x_doc_terms = crossed_column([keywords, 'doc_terms'], 50K)
+ columns = [keywords_x_doc_terms, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ linear_prediction = linear_model(features, columns)
+ ```
+
+ If an input feature is of numeric type, you can use
+ `categorical_column_with_identity`, or `bucketized_column`, as in the example:
+
+ ```python
+ # vertical_id is an integer categorical feature.
+ vertical_id = categorical_column_with_identity('vertical_id', 10K)
+ price = numeric_column('price')
+ # bucketized_column converts numerical feature to a categorical one.
+ bucketized_price = bucketized_column(price, boundaries=[...])
+ vertical_id_x_price = crossed_column([vertical_id, bucketized_price], 50K)
+ columns = [vertical_id_x_price, ...]
+ features = tf.parse_example(..., features=make_parse_example_spec(columns))
+ linear_prediction = linear_model(features, columns)
+ ```
+
+ To use crossed column in DNN model, you need to add it in an embedding column
+ as in this example:
+
+ ```python
+ vertical_id_x_price = crossed_column([vertical_id, bucketized_price], 50K)
+ vertical_id_x_price_embedded = embedding_column(vertical_id_x_price, 10)
+ dense_tensor = input_layer(features, [vertical_id_x_price_embedded, ...])
+ ```
+
+ Args:
+ keys: An iterable identifying the features to be crossed. Each element can
+ be either:
+ * string: Will use the corresponding feature which must be of string type.
+ * `CategoricalColumn`: Will use the transformed tensor produced by this
+ column. Does not support hashed categorical column.
+ hash_bucket_size: An int > 1. The number of buckets.
+ hash_key: Specify the hash_key that will be used by the `FingerprintCat64`
+ function to combine the crosses fingerprints on SparseCrossOp (optional).
+
+ Returns:
+ A `CrossedColumn`.
+
+ Raises:
+ ValueError: If `len(keys) < 2`.
+ ValueError: If any of the keys is neither a string nor `CategoricalColumn`.
+ ValueError: If any of the keys is `HashedCategoricalColumn`.
+ ValueError: If `hash_bucket_size < 1`.
+ """
+ if not hash_bucket_size or hash_bucket_size < 1:
+ raise ValueError('hash_bucket_size must be > 1. '
+ 'hash_bucket_size: {}'.format(hash_bucket_size))
+ if not keys or len(keys) < 2:
+ raise ValueError(
+ 'keys must be a list with length > 1. Given: {}'.format(keys))
+ for key in keys:
+ if (not isinstance(key, six.string_types) and
+ not isinstance(key, CategoricalColumn)):
+ raise ValueError(
+ 'Unsupported key type. All keys must be either string, or '
+ 'categorical column except HashedCategoricalColumn. '
+ 'Given: {}'.format(key))
+ if isinstance(key, HashedCategoricalColumn):
+ raise ValueError(
+ 'categorical_column_with_hash_bucket is not supported for crossing. '
+ 'Hashing before crossing will increase probability of collision. '
+ 'Instead, use the feature name as a string. Given: {}'.format(key))
+ return CrossedColumn(
+ keys=tuple(keys), hash_bucket_size=hash_bucket_size, hash_key=hash_key)
+
+
+class StateManager(object):
+ """Manages the state associated with FeatureColumns.
+
+ Some `FeatureColumn`s create variables or resources to assist their
+ computation. The `StateManager` is responsible for creating and storing these
+ objects since `FeatureColumn`s are supposed to be stateless configuration
+ only.
+ """
+
+ def get_variable(self,
+ feature_column,
+ name,
+ shape,
+ dtype=None,
+ initializer=None):
+ """Creates a new variable or returns an existing one.
+
+ Args:
+ feature_column: A `FeatureColumn` object this variable corresponds to.
+ name: variable name.
+ shape: variable shape.
+ dtype: The type of the variable. Defaults to `self.dtype` or `float32`.
+ initializer: initializer instance (callable).
+
+ Returns:
+ The variable.
+ """
+ raise NotImplementedError('StateManager.get_variable')
+
+ def get_resource(self, feature_column, name, resource_creator):
+ """Creates a new resource or returns an existing one.
+
+ Resources can be things such as tables etc.
+
+ Args:
+ feature_column: A `FeatureColumn` object this variable corresponds to.
+ name: Name of the resource.
+ resource_creator: A callable that can create the resource.
+
+ Returns:
+ The resource.
+ """
+ raise NotImplementedError('StateManager.get_resource')
+
+
+class FeatureColumn(object):
+ """Represents a feature column abstraction.
+
+ WARNING: Do not subclass this layer unless you know what you are doing:
+ the API is subject to future changes.
+
+ To distinguish between the concept of a feature family and a specific binary
+ feature within a family, we refer to a feature family like "country" as a
+ feature column. For example, we can have a feature in a `tf.Example` format:
+ {key: "country", value: [ "US" ]}
+ In this example the value of feature is "US" and "country" refers to the
+ column of the feature.
+
+ This class is an abstract class. Users should not create instances of this.
+ """
+ __metaclass__ = abc.ABCMeta
+
+ @abc.abstractproperty
+ def name(self):
+ """Returns string. Used for naming."""
+ pass
+
+ @abc.abstractmethod
+ def transform_feature(self, transformation_cache, state_manager):
+ """Returns intermediate representation (usually a `Tensor`).
+
+ Uses `transformation_cache` to create an intermediate representation
+ (usually a `Tensor`) that other feature columns can use.
+
+ Example usage of `transformation_cache`:
+ Let's say a Feature column depends on raw feature ('raw') and another
+ `FeatureColumn` (input_fc). To access corresponding `Tensor`s,
+ transformation_cache will be used as follows:
+
+ ```python
+ raw_tensor = transformation_cache.get('raw', state_manager)
+ fc_tensor = transformation_cache.get(input_fc, state_manager)
+ ```
+
+ Args:
+ transformation_cache: A `FeatureTransformationCache` object to access
+ features.
+ state_manager: A `StateManager` to create / access resources such as
+ lookup tables.
+
+ Returns:
+ Transformed feature `Tensor`.
+ """
+ pass
+
+ @abc.abstractproperty
+ def parse_example_spec(self):
+ """Returns a `tf.Example` parsing spec as dict.
+
+ It is used for get_parsing_spec for `tf.parse_example`. Returned spec is a
+ dict from keys ('string') to `VarLenFeature`, `FixedLenFeature`, and other
+ supported objects. Please check documentation of @{tf.parse_example} for all
+ supported spec objects.
+
+ Let's say a Feature column depends on raw feature ('raw') and another
+ `FeatureColumn` (input_fc). One possible implementation of
+ parse_example_spec is as follows:
+
+ ```python
+ spec = {'raw': tf.FixedLenFeature(...)}
+ spec.update(input_fc.parse_example_spec)
+ return spec
+ ```
+ """
+ pass
+
+ def create_state(self, state_manager):
+ """Uses the `state_manager` to create state for the FeatureColumn.
+
+ Args:
+ state_manager: A `StateManager` to create / access resources such as
+ lookup tables and variables.
+ """
+ pass
+
+
+class DenseColumn(FeatureColumn):
+ """Represents a column which can be represented as `Tensor`.
+
+ Some examples of this type are: numeric_column, embedding_column,
+ indicator_column.
+ """
+
+ __metaclass__ = abc.ABCMeta
+
+ @abc.abstractproperty
+ def variable_shape(self):
+ """`TensorShape` of `get_dense_tensor`, without batch dimension."""
+ pass
+
+ @abc.abstractmethod
+ def get_dense_tensor(self, transformation_cache, state_manager):
+ """Returns a `Tensor`.
+
+ The output of this function will be used by model-builder-functions. For
+ example the pseudo code of `input_layer` will be like:
+
+ ```python
+ def input_layer(features, feature_columns, ...):
+ outputs = [fc.get_dense_tensor(...) for fc in feature_columns]
+ return tf.concat(outputs)
+ ```
+
+ Args:
+ transformation_cache: A `FeatureTransformationCache` object to access
+ features.
+ state_manager: A `StateManager` to create / access resources such as
+ lookup tables.
+
+ Returns:
+ `Tensor` of shape [batch_size] + `variable_shape`.
+ """
+ pass
+
+
+def _create_weighted_sum(column,
+ transformation_cache,
+ state_manager,
+ units,
+ sparse_combiner,
+ weight_collections,
+ trainable,
+ weight_var=None):
+ """Creates a weighted sum for a dense/categorical column for linear_model."""
+ if isinstance(column, CategoricalColumn):
+ return _create_categorical_column_weighted_sum(
+ column=column,
+ transformation_cache=transformation_cache,
+ state_manager=state_manager,
+ units=units,
+ sparse_combiner=sparse_combiner,
+ weight_collections=weight_collections,
+ trainable=trainable,
+ weight_var=weight_var)
+ else:
+ return _create_dense_column_weighted_sum(
+ column=column,
+ transformation_cache=transformation_cache,
+ state_manager=state_manager,
+ units=units,
+ weight_collections=weight_collections,
+ trainable=trainable,
+ weight_var=weight_var)
+
+
+def _create_dense_column_weighted_sum(column,
+ transformation_cache,
+ state_manager,
+ units,
+ weight_collections,
+ trainable,
+ weight_var=None):
+ """Create a weighted sum of a dense column for linear_model."""
+ tensor = column.get_dense_tensor(transformation_cache, state_manager)
+ num_elements = column.variable_shape.num_elements()
+ batch_size = array_ops.shape(tensor)[0]
+ tensor = array_ops.reshape(tensor, shape=(batch_size, num_elements))
+ if weight_var is not None:
+ weight = weight_var
+ else:
+ weight = variable_scope.get_variable(
+ name='weights',
+ shape=[num_elements, units],
+ initializer=init_ops.zeros_initializer(),
+ trainable=trainable,
+ collections=weight_collections)
+ return math_ops.matmul(tensor, weight, name='weighted_sum')
+
+
+class CategoricalColumn(FeatureColumn):
+ """Represents a categorical feature.
+
+ A categorical feature typically handled with a @{tf.SparseTensor} of IDs.
+ """
+ __metaclass__ = abc.ABCMeta
+
+ IdWeightPair = collections.namedtuple( # pylint: disable=invalid-name
+ 'IdWeightPair', ('id_tensor', 'weight_tensor'))
+
+ @abc.abstractproperty
+ def num_buckets(self):
+ """Returns number of buckets in this sparse feature."""
+ pass
+
+ @abc.abstractmethod
+ def get_sparse_tensors(self, transformation_cache, state_manager):
+ """Returns an IdWeightPair.
+
+ `IdWeightPair` is a pair of `SparseTensor`s which represents ids and
+ weights.
+
+ `IdWeightPair.id_tensor` is typically a `batch_size` x `num_buckets`
+ `SparseTensor` of `int64`. `IdWeightPair.weight_tensor` is either a
+ `SparseTensor` of `float` or `None` to indicate all weights should be
+ taken to be 1. If specified, `weight_tensor` must have exactly the same
+ shape and indices as `sp_ids`. Expected `SparseTensor` is same as parsing
+ output of a `VarLenFeature` which is a ragged matrix.
+
+ Args:
+ transformation_cache: A `FeatureTransformationCache` object to access
+ features.
+ state_manager: A `StateManager` to create / access resources such as
+ lookup tables.
+ """
+ pass
+
+
+def _create_categorical_column_weighted_sum(column,
+ transformation_cache,
+ state_manager,
+ units,
+ sparse_combiner,
+ weight_collections,
+ trainable,
+ weight_var=None):
+ # pylint: disable=g-doc-return-or-yield,g-doc-args
+ """Create a weighted sum of a categorical column for linear_model.
+
+ Note to maintainer: As implementation details, the weighted sum is
+ implemented via embedding_lookup_sparse toward efficiency. Mathematically,
+ they are the same.
+
+ To be specific, conceptually, categorical column can be treated as multi-hot
+ vector. Say:
+
+ ```python
+ x = [0 0 1] # categorical column input
+ w = [a b c] # weights
+ ```
+ The weighted sum is `c` in this case, which is same as `w[2]`.
+
+ Another example is
+
+ ```python
+ x = [0 1 1] # categorical column input
+ w = [a b c] # weights
+ ```
+ The weighted sum is `b + c` in this case, which is same as `w[2] + w[3]`.
+
+ For both cases, we can implement weighted sum via embedding_lookup with
+ sparse_combiner = "sum".
+ """
+
+ sparse_tensors = column.get_sparse_tensors(transformation_cache,
+ state_manager)
+ id_tensor = sparse_ops.sparse_reshape(sparse_tensors.id_tensor, [
+ array_ops.shape(sparse_tensors.id_tensor)[0], -1
+ ])
+ weight_tensor = sparse_tensors.weight_tensor
+ if weight_tensor is not None:
+ weight_tensor = sparse_ops.sparse_reshape(
+ weight_tensor, [array_ops.shape(weight_tensor)[0], -1])
+
+ if weight_var is not None:
+ weight = weight_var
+ else:
+ weight = variable_scope.get_variable(
+ name='weights',
+ shape=(column.num_buckets, units),
+ initializer=init_ops.zeros_initializer(),
+ trainable=trainable,
+ collections=weight_collections)
+ return _safe_embedding_lookup_sparse(
+ weight,
+ id_tensor,
+ sparse_weights=weight_tensor,
+ combiner=sparse_combiner,
+ name='weighted_sum')
+
+
+class SequenceDenseColumn(FeatureColumn):
+ """Represents dense sequence data."""
+
+ __metaclass__ = abc.ABCMeta
+
+ TensorSequenceLengthPair = collections.namedtuple( # pylint: disable=invalid-name
+ 'TensorSequenceLengthPair', ('dense_tensor', 'sequence_length'))
+
+ @abc.abstractmethod
+ def get_sequence_dense_tensor(self, transformation_cache, state_manager):
+ """Returns a `TensorSequenceLengthPair`.
+
+ Args:
+ transformation_cache: A `FeatureTransformationCache` object to access
+ features.
+ state_manager: A `StateManager` to create / access resources such as
+ lookup tables.
+ """
+ pass
+
+
+class FeatureTransformationCache(object):
+ """Handles caching of transformations while building the model.
+
+ `FeatureColumn` specifies how to digest an input column to the network. Some
+ feature columns require data transformations. This class caches those
+ transformations.
+
+ Some features may be used in more than one place. For example, one can use a
+ bucketized feature by itself and a cross with it. In that case we
+ should create only one bucketization op instead of creating ops for each
+ feature column separately. To handle re-use of transformed columns,
+ `FeatureTransformationCache` caches all previously transformed columns.
+
+ Example:
+ We're trying to use the following `FeatureColumn`s:
+
+ ```python
+ bucketized_age = fc.bucketized_column(fc.numeric_column("age"), ...)
+ keywords = fc.categorical_column_with_hash_buckets("keywords", ...)
+ age_X_keywords = fc.crossed_column([bucketized_age, "keywords"])
+ ... = linear_model(features,
+ [bucketized_age, keywords, age_X_keywords]
+ ```
+
+ If we transform each column independently, then we'll get duplication of
+ bucketization (one for cross, one for bucketization itself).
+ The `FeatureTransformationCache` eliminates this duplication.
+ """
+
+ def __init__(self, features):
+ """Creates a `FeatureTransformationCache`.
+
+ Args:
+ features: A mapping from feature column to objects that are `Tensor` or
+ `SparseTensor`, or can be converted to same via
+ `sparse_tensor.convert_to_tensor_or_sparse_tensor`. A `string` key
+ signifies a base feature (not-transformed). A `FeatureColumn` key
+ means that this `Tensor` is the output of an existing `FeatureColumn`
+ which can be reused.
+ """
+ self._features = features.copy()
+ self._feature_tensors = {}
+
+ def get(self, key, state_manager):
+ """Returns a `Tensor` for the given key.
+
+ A `str` key is used to access a base feature (not-transformed). When a
+ `FeatureColumn` is passed, the transformed feature is returned if it
+ already exists, otherwise the given `FeatureColumn` is asked to provide its
+ transformed output, which is then cached.
+
+ Args:
+ key: a `str` or a `FeatureColumn`.
+ state_manager: A StateManager object that holds the FeatureColumn state.
+
+ Returns:
+ The transformed `Tensor` corresponding to the `key`.
+
+ Raises:
+ ValueError: if key is not found or a transformed `Tensor` cannot be
+ computed.
+ """
+ if key in self._feature_tensors:
+ # FeatureColumn is already transformed or converted.
+ return self._feature_tensors[key]
+
+ if key in self._features:
+ feature_tensor = self._get_raw_feature_as_tensor(key)
+ self._feature_tensors[key] = feature_tensor
+ return feature_tensor
+
+ if isinstance(key, six.string_types):
+ raise ValueError('Feature {} is not in features dictionary.'.format(key))
+
+ if not isinstance(key, FeatureColumn):
+ raise TypeError('"key" must be either a "str" or "FeatureColumn". '
+ 'Provided: {}'.format(key))
+
+ column = key
+ logging.debug('Transforming feature_column %s.', column)
+ transformed = column.transform_feature(self, state_manager)
+ if transformed is None:
+ raise ValueError('Column {} is not supported.'.format(column.name))
+ self._feature_tensors[column] = transformed
+ return transformed
+
+ def _get_raw_feature_as_tensor(self, key):
+ """Gets the raw_feature (keyed by `key`) as `tensor`.
+
+ The raw feature is converted to (sparse) tensor and maybe expand dim.
+
+ For both `Tensor` and `SparseTensor`, the rank will be expanded (to 2) if
+ the rank is 1. This supports dynamic rank also. For rank 0 raw feature, will
+ error out as it is not supported.
+
+ Args:
+ key: A `str` key to access the raw feature.
+
+ Returns:
+ A `Tensor` or `SparseTensor`.
+
+ Raises:
+ ValueError: if the raw feature has rank 0.
+ """
+ raw_feature = self._features[key]
+ feature_tensor = sparse_tensor_lib.convert_to_tensor_or_sparse_tensor(
+ raw_feature)
+
+ def expand_dims(input_tensor):
+ # Input_tensor must have rank 1.
+ if isinstance(input_tensor, sparse_tensor_lib.SparseTensor):
+ return sparse_ops.sparse_reshape(
+ input_tensor, [array_ops.shape(input_tensor)[0], -1])
+ else:
+ return array_ops.expand_dims(input_tensor, -1)
+
+ rank = feature_tensor.get_shape().ndims
+ if rank is not None:
+ if rank == 0:
+ raise ValueError(
+ 'Feature (key: {}) cannot have rank 0. Give: {}'.format(
+ key, feature_tensor))
+ return feature_tensor if rank != 1 else expand_dims(feature_tensor)
+
+ # Handle dynamic rank.
+ with ops.control_dependencies([
+ check_ops.assert_positive(
+ array_ops.rank(feature_tensor),
+ message='Feature (key: {}) cannot have rank 0. Given: {}'.format(
+ key, feature_tensor))]):
+ return control_flow_ops.cond(
+ math_ops.equal(1, array_ops.rank(feature_tensor)),
+ lambda: expand_dims(feature_tensor),
+ lambda: feature_tensor)
+
+
+# TODO(ptucker): Move to third_party/tensorflow/python/ops/sparse_ops.py
+def _shape_offsets(shape):
+ """Returns moving offset for each dimension given shape."""
+ offsets = []
+ for dim in reversed(shape):
+ if offsets:
+ offsets.append(dim * offsets[-1])
+ else:
+ offsets.append(dim)
+ offsets.reverse()
+ return offsets
+
+
+# TODO(ptucker): Move to third_party/tensorflow/python/ops/sparse_ops.py
+def _to_sparse_input_and_drop_ignore_values(input_tensor, ignore_value=None):
+ """Converts a `Tensor` to a `SparseTensor`, dropping ignore_value cells.
+
+ If `input_tensor` is already a `SparseTensor`, just return it.
+
+ Args:
+ input_tensor: A string or integer `Tensor`.
+ ignore_value: Entries in `dense_tensor` equal to this value will be
+ absent from the resulting `SparseTensor`. If `None`, default value of
+ `dense_tensor`'s dtype will be used ('' for `str`, -1 for `int`).
+
+ Returns:
+ A `SparseTensor` with the same shape as `input_tensor`.
+
+ Raises:
+ ValueError: when `input_tensor`'s rank is `None`.
+ """
+ input_tensor = sparse_tensor_lib.convert_to_tensor_or_sparse_tensor(
+ input_tensor)
+ if isinstance(input_tensor, sparse_tensor_lib.SparseTensor):
+ return input_tensor
+ with ops.name_scope(None, 'to_sparse_input', (input_tensor, ignore_value,)):
+ if ignore_value is None:
+ if input_tensor.dtype == dtypes.string:
+ # Exception due to TF strings are converted to numpy objects by default.
+ ignore_value = ''
+ elif input_tensor.dtype.is_integer:
+ ignore_value = -1 # -1 has a special meaning of missing feature
+ else:
+ # NOTE: `as_numpy_dtype` is a property, so with the parentheses this is
+ # constructing a new numpy object of the given type, which yields the
+ # default value for that type.
+ ignore_value = input_tensor.dtype.as_numpy_dtype()
+ ignore_value = math_ops.cast(
+ ignore_value, input_tensor.dtype, name='ignore_value')
+ indices = array_ops.where(
+ math_ops.not_equal(input_tensor, ignore_value), name='indices')
+ return sparse_tensor_lib.SparseTensor(
+ indices=indices,
+ values=array_ops.gather_nd(input_tensor, indices, name='values'),
+ dense_shape=array_ops.shape(
+ input_tensor, out_type=dtypes.int64, name='dense_shape'))
+
+
+def _normalize_feature_columns(feature_columns):
+ """Normalizes the `feature_columns` input.
+
+ This method converts the `feature_columns` to list type as best as it can. In
+ addition, verifies the type and other parts of feature_columns, required by
+ downstream library.
+
+ Args:
+ feature_columns: The raw feature columns, usually passed by users.
+
+ Returns:
+ The normalized feature column list.
+
+ Raises:
+ ValueError: for any invalid inputs, such as empty, duplicated names, etc.
+ """
+ if isinstance(feature_columns, FeatureColumn):
+ feature_columns = [feature_columns]
+
+ if isinstance(feature_columns, collections.Iterator):
+ feature_columns = list(feature_columns)
+
+ if isinstance(feature_columns, dict):
+ raise ValueError('Expected feature_columns to be iterable, found dict.')
+
+ for column in feature_columns:
+ if not isinstance(column, FeatureColumn):
+ raise ValueError('Items of feature_columns must be a FeatureColumn. '
+ 'Given (type {}): {}.'.format(type(column), column))
+ if not feature_columns:
+ raise ValueError('feature_columns must not be empty.')
+ name_to_column = dict()
+ for column in feature_columns:
+ if column.name in name_to_column:
+ raise ValueError('Duplicate feature column name found for columns: {} '
+ 'and {}. This usually means that these columns refer to '
+ 'same base feature. Either one must be discarded or a '
+ 'duplicated but renamed item must be inserted in '
+ 'features dict.'.format(column,
+ name_to_column[column.name]))
+ name_to_column[column.name] = column
+
+ return feature_columns
+
+
+class NumericColumn(
+ DenseColumn,
+ collections.namedtuple(
+ 'NumericColumn',
+ ('key', 'shape', 'default_value', 'dtype', 'normalizer_fn'))):
+ """see `numeric_column`."""
+
+ @property
+ def name(self):
+ """See `FeatureColumn` base class."""
+ return self.key
+
+ @property
+ def parse_example_spec(self):
+ """See `FeatureColumn` base class."""
+ return {
+ self.key:
+ parsing_ops.FixedLenFeature(self.shape, self.dtype,
+ self.default_value)
+ }
+
+ def transform_feature(self, transformation_cache, state_manager):
+ """See `FeatureColumn` base class.
+
+ In this case, we apply the `normalizer_fn` to the input tensor.
+
+ Args:
+ transformation_cache: A `FeatureTransformationCache` object to access
+ features.
+ state_manager: A `StateManager` to create / access resources such as
+ lookup tables.
+
+ Returns:
+ Normalized input tensor.
+ Raises:
+ ValueError: If a SparseTensor is passed in.
+ """
+ input_tensor = transformation_cache.get(self.key, state_manager)
+ if isinstance(input_tensor, sparse_tensor_lib.SparseTensor):
+ raise ValueError(
+ 'The corresponding Tensor of numerical column must be a Tensor. '
+ 'SparseTensor is not supported. key: {}'.format(self.key))
+ if self.normalizer_fn is not None:
+ input_tensor = self.normalizer_fn(input_tensor)
+ return math_ops.to_float(input_tensor)
+
+ @property
+ def variable_shape(self):
+ """See `DenseColumn` base class."""
+ return tensor_shape.TensorShape(self.shape)
+
+ def get_dense_tensor(self, transformation_cache, state_manager):
+ """Returns dense `Tensor` representing numeric feature.
+
+ Args:
+ transformation_cache: A `FeatureTransformationCache` object to access
+ features.
+ state_manager: A `StateManager` to create / access resources such as
+ lookup tables.
+
+ Returns:
+ Dense `Tensor` created within `transform_feature`.
+ """
+ # Feature has been already transformed. Return the intermediate
+ # representation created by _transform_feature.
+ return transformation_cache.get(self, state_manager)
+
+
+class BucketizedColumn(DenseColumn, CategoricalColumn,
+ collections.namedtuple('BucketizedColumn',
+ ('source_column', 'boundaries'))):
+ """See `bucketized_column`."""
+
+ @property
+ def name(self):
+ """See `FeatureColumn` base class."""
+ return '{}_bucketized'.format(self.source_column.name)
+
+ @property
+ def parse_example_spec(self):
+ """See `FeatureColumn` base class."""
+ return self.source_column.parse_example_spec
+
+ def transform_feature(self, transformation_cache, state_manager):
+ """Returns bucketized categorical `source_column` tensor."""
+ source_tensor = transformation_cache.get(self.source_column, state_manager)
+ return math_ops._bucketize( # pylint: disable=protected-access
+ source_tensor,
+ boundaries=self.boundaries)
+
+ @property
+ def variable_shape(self):
+ """See `DenseColumn` base class."""
+ return tensor_shape.TensorShape(
+ tuple(self.source_column.shape) + (len(self.boundaries) + 1,))
+
+ def get_dense_tensor(self, transformation_cache, state_manager):
+ """Returns one hot encoded dense `Tensor`."""
+ input_tensor = transformation_cache.get(self, state_manager)
+ return array_ops.one_hot(
+ indices=math_ops.to_int64(input_tensor),
+ depth=len(self.boundaries) + 1,
+ on_value=1.,
+ off_value=0.)
+
+ @property
+ def num_buckets(self):
+ """See `CategoricalColumn` base class."""
+ # By construction, source_column is always one-dimensional.
+ return (len(self.boundaries) + 1) * self.source_column.shape[0]
+
+ def get_sparse_tensors(self, transformation_cache, state_manager):
+ """Converts dense inputs to SparseTensor so downstream code can use it."""
+ input_tensor = transformation_cache.get(self, state_manager)
+ batch_size = array_ops.shape(input_tensor)[0]
+ # By construction, source_column is always one-dimensional.
+ source_dimension = self.source_column.shape[0]
+
+ i1 = array_ops.reshape(
+ array_ops.tile(
+ array_ops.expand_dims(math_ops.range(0, batch_size), 1),
+ [1, source_dimension]),
+ (-1,))
+ i2 = array_ops.tile(math_ops.range(0, source_dimension), [batch_size])
+ # Flatten the bucket indices and unique them across dimensions
+ # E.g. 2nd dimension indices will range from k to 2*k-1 with k buckets
+ bucket_indices = (
+ array_ops.reshape(input_tensor, (-1,)) +
+ (len(self.boundaries) + 1) * i2)
+
+ indices = math_ops.to_int64(array_ops.transpose(array_ops.stack((i1, i2))))
+ dense_shape = math_ops.to_int64(array_ops.stack(
+ [batch_size, source_dimension]))
+ sparse_tensor = sparse_tensor_lib.SparseTensor(
+ indices=indices,
+ values=bucket_indices,
+ dense_shape=dense_shape)
+ return CategoricalColumn.IdWeightPair(sparse_tensor, None)
+
+
+class EmbeddingColumn(
+ DenseColumn, SequenceDenseColumn,
+ collections.namedtuple(
+ 'EmbeddingColumn',
+ ('categorical_column', 'dimension', 'combiner', 'initializer',
+ 'ckpt_to_load_from', 'tensor_name_in_ckpt', 'max_norm', 'trainable'))):
+ """See `embedding_column`."""
+
+ @property
+ def name(self):
+ """See `FeatureColumn` base class."""
+ return '{}_embedding'.format(self.categorical_column.name)
+
+ @property
+ def parse_example_spec(self):
+ """See `FeatureColumn` base class."""
+ return self.categorical_column.parse_example_spec
+
+ def transform_feature(self, transformation_cache, state_manager):
+ """Transforms underlying `categorical_column`."""
+ return transformation_cache.get(self.categorical_column, state_manager)
+
+ @property
+ def variable_shape(self):
+ """See `DenseColumn` base class."""
+ return tensor_shape.vector(self.dimension)
+
+ def _get_dense_tensor_internal(self, transformation_cache, state_manager):
+ """Private method that follows the signature of _get_dense_tensor."""
+ # Get sparse IDs and weights.
+ sparse_tensors = self.categorical_column.get_sparse_tensors(
+ transformation_cache, state_manager)
+ sparse_ids = sparse_tensors.id_tensor
+ sparse_weights = sparse_tensors.weight_tensor
+
+ embedding_shape = (self.categorical_column.num_buckets, self.dimension)
+ embedding_weights = state_manager.get_variable(
+ self,
+ name='embedding_weights',
+ shape=embedding_shape,
+ dtype=dtypes.float32,
+ initializer=self.initializer)
+
+ if self.ckpt_to_load_from is not None:
+ to_restore = embedding_weights
+ if isinstance(to_restore, variables.PartitionedVariable):
+ to_restore = to_restore._get_variable_list() # pylint: disable=protected-access
+ checkpoint_utils.init_from_checkpoint(self.ckpt_to_load_from, {
+ self.tensor_name_in_ckpt: to_restore
+ })
+
+ # Return embedding lookup result.
+ return _safe_embedding_lookup_sparse(
+ embedding_weights=embedding_weights,
+ sparse_ids=sparse_ids,
+ sparse_weights=sparse_weights,
+ combiner=self.combiner,
+ name='%s_weights' % self.name,
+ max_norm=self.max_norm)
+
+ def get_dense_tensor(self, transformation_cache, state_manager):
+ """Returns tensor after doing the embedding lookup.
+
+ Args:
+ transformation_cache: A `FeatureTransformationCache` object to access
+ features.
+ state_manager: A `StateManager` to create / access resources such as
+ lookup tables.
+
+ Returns:
+ Embedding lookup tensor.
+
+ Raises:
+ ValueError: `categorical_column` is SequenceCategoricalColumn.
+ """
+ if isinstance(self.categorical_column, SequenceCategoricalColumn):
+ raise ValueError(
+ 'In embedding_column: {}. '
+ 'categorical_column must not be of type SequenceCategoricalColumn. '
+ 'Suggested fix A: If you wish to use input_layer, use a '
+ 'non-sequence categorical_column_with_*. '
+ 'Suggested fix B: If you wish to create sequence input, use '
+ 'sequence_input_layer instead of input_layer. '
+ 'Given (type {}): {}'.format(self.name, type(self.categorical_column),
+ self.categorical_column))
+ return self._get_dense_tensor_internal(transformation_cache, state_manager)
+
+ def get_sequence_dense_tensor(self, transformation_cache, state_manager):
+ """See `SequenceDenseColumn` base class."""
+ if not isinstance(self.categorical_column, SequenceCategoricalColumn):
+ raise ValueError(
+ 'In embedding_column: {}. '
+ 'categorical_column must be of type SequenceCategoricalColumn '
+ 'to use sequence_input_layer. '
+ 'Suggested fix: Use one of sequence_categorical_column_with_*. '
+ 'Given (type {}): {}'.format(self.name, type(self.categorical_column),
+ self.categorical_column))
+ dense_tensor = self._get_dense_tensor_internal( # pylint: disable=protected-access
+ transformation_cache, state_manager)
+ sparse_tensors = self.categorical_column.get_sparse_tensors(
+ transformation_cache, state_manager)
+ sequence_length = _sequence_length_from_sparse_tensor(
+ sparse_tensors.id_tensor)
+ return SequenceDenseColumn.TensorSequenceLengthPair(
+ dense_tensor=dense_tensor, sequence_length=sequence_length)
+
+
+def _get_graph_for_variable(var):
+ if isinstance(var, variables.PartitionedVariable):
+ return list(var)[0].graph
+ else:
+ return var.graph
+
+
+class SharedEmbeddingColumn(
+ DenseColumn, SequenceDenseColumn,
+ collections.namedtuple(
+ 'SharedEmbeddingColumn',
+ ('categorical_column', 'dimension', 'combiner', 'initializer',
+ 'shared_embedding_collection_name', 'ckpt_to_load_from',
+ 'tensor_name_in_ckpt', 'max_norm', 'trainable'))):
+ """See `embedding_column`."""
+
+ @property
+ def name(self):
+ """See `FeatureColumn` base class."""
+ return '{}_shared_embedding'.format(self.categorical_column.name)
+
+ @property
+ def shared_collection_name(self):
+ """Returns the shared name of this column.
+
+ A group of columns share an embedding. Each one of those columns would have
+ the same `shared_collection_name` by which they could be collectively
+ referred to.
+ """
+ return self.shared_embedding_collection_name
+
+ @property
+ def parse_example_spec(self):
+ """See `FeatureColumn` base class."""
+ return self.categorical_column.parse_example_spec
+
+ def transform_feature(self, transformation_cache, state_manager):
+ """See `FeatureColumn` base class."""
+ return transformation_cache.get(self.categorical_column, state_manager)
+
+ @property
+ def variable_shape(self):
+ """See `DenseColumn` base class."""
+ return tensor_shape.vector(self.dimension)
+
+ def _get_dense_tensor_internal(self, transformation_cache, state_manager):
+ """Private method that follows the signature of _get_dense_tensor."""
+ # This method is called from a variable_scope with name _var_scope_name,
+ # which is shared among all shared embeddings. Open a name_scope here, so
+ # that the ops for different columns have distinct names.
+ with ops.name_scope(None, default_name=self.name):
+ # Get sparse IDs and weights.
+ sparse_tensors = self.categorical_column.get_sparse_tensors(
+ transformation_cache, state_manager)
+ sparse_ids = sparse_tensors.id_tensor
+ sparse_weights = sparse_tensors.weight_tensor
+
+ embedding_shape = (self.categorical_column.num_buckets, self.dimension)
+ embedding_weights = state_manager.get_variable(
+ self,
+ name='embedding_weights',
+ shape=embedding_shape,
+ dtype=dtypes.float32,
+ initializer=self.initializer)
+
+ if self.ckpt_to_load_from is not None:
+ to_restore = embedding_weights
+ if isinstance(to_restore, variables.PartitionedVariable):
+ to_restore = to_restore._get_variable_list() # pylint: disable=protected-access
+ checkpoint_utils.init_from_checkpoint(self.ckpt_to_load_from, {
+ self.tensor_name_in_ckpt: to_restore
+ })
+
+ # Return embedding lookup result.
+ return _safe_embedding_lookup_sparse(
+ embedding_weights=embedding_weights,
+ sparse_ids=sparse_ids,
+ sparse_weights=sparse_weights,
+ combiner=self.combiner,
+ name='%s_weights' % self.name,
+ max_norm=self.max_norm)
+
+ def get_dense_tensor(self, transformation_cache, state_manager):
+ """Returns the embedding lookup result."""
+ if isinstance(self.categorical_column, SequenceCategoricalColumn):
+ raise ValueError(
+ 'In embedding_column: {}. '
+ 'categorical_column must not be of type SequenceCategoricalColumn. '
+ 'Suggested fix A: If you wish to use input_layer, use a '
+ 'non-sequence categorical_column_with_*. '
+ 'Suggested fix B: If you wish to create sequence input, use '
+ 'sequence_input_layer instead of input_layer. '
+ 'Given (type {}): {}'.format(self.name, type(self.categorical_column),
+ self.categorical_column))
+ return self._get_dense_tensor_internal(transformation_cache, state_manager)
+
+ def get_sequence_dense_tensor(self, transformation_cache, state_manager):
+ """See `SequenceDenseColumn` base class."""
+ if not isinstance(self.categorical_column, SequenceCategoricalColumn):
+ raise ValueError(
+ 'In embedding_column: {}. '
+ 'categorical_column must be of type SequenceCategoricalColumn '
+ 'to use sequence_input_layer. '
+ 'Suggested fix: Use one of sequence_categorical_column_with_*. '
+ 'Given (type {}): {}'.format(self.name, type(self.categorical_column),
+ self.categorical_column))
+ dense_tensor = self.get_dense_tensor_internal(transformation_cache,
+ state_manager)
+ sparse_tensors = self.categorical_column.get_sparse_tensors(
+ transformation_cache, state_manager)
+ sequence_length = _sequence_length_from_sparse_tensor(
+ sparse_tensors.id_tensor)
+ return SequenceDenseColumn.TensorSequenceLengthPair(
+ dense_tensor=dense_tensor, sequence_length=sequence_length)
+
+
+def _create_tuple(shape, value):
+ """Returns a tuple with given shape and filled with value."""
+ if shape:
+ return tuple([_create_tuple(shape[1:], value) for _ in range(shape[0])])
+ return value
+
+
+def _as_tuple(value):
+ if not nest.is_sequence(value):
+ return value
+ return tuple([_as_tuple(v) for v in value])
+
+
+def _check_shape(shape, key):
+ """Returns shape if it's valid, raises error otherwise."""
+ assert shape is not None
+ if not nest.is_sequence(shape):
+ shape = [shape]
+ shape = tuple(shape)
+ for dimension in shape:
+ if not isinstance(dimension, int):
+ raise TypeError('shape dimensions must be integer. '
+ 'shape: {}, key: {}'.format(shape, key))
+ if dimension < 1:
+ raise ValueError('shape dimensions must be greater than 0. '
+ 'shape: {}, key: {}'.format(shape, key))
+ return shape
+
+
+def _is_shape_and_default_value_compatible(default_value, shape):
+ """Verifies compatibility of shape and default_value."""
+ # Invalid condition:
+ # * if default_value is not a scalar and shape is empty
+ # * or if default_value is an iterable and shape is not empty
+ if nest.is_sequence(default_value) != bool(shape):
+ return False
+ if not shape:
+ return True
+ if len(default_value) != shape[0]:
+ return False
+ for i in range(shape[0]):
+ if not _is_shape_and_default_value_compatible(default_value[i], shape[1:]):
+ return False
+ return True
+
+
+def _check_default_value(shape, default_value, dtype, key):
+ """Returns default value as tuple if it's valid, otherwise raises errors.
+
+ This function verifies that `default_value` is compatible with both `shape`
+ and `dtype`. If it is not compatible, it raises an error. If it is compatible,
+ it casts default_value to a tuple and returns it. `key` is used only
+ for error message.
+
+ Args:
+ shape: An iterable of integers specifies the shape of the `Tensor`.
+ default_value: If a single value is provided, the same value will be applied
+ as the default value for every item. If an iterable of values is
+ provided, the shape of the `default_value` should be equal to the given
+ `shape`.
+ dtype: defines the type of values. Default value is `tf.float32`. Must be a
+ non-quantized, real integer or floating point type.
+ key: Column name, used only for error messages.
+
+ Returns:
+ A tuple which will be used as default value.
+
+ Raises:
+ TypeError: if `default_value` is an iterable but not compatible with `shape`
+ TypeError: if `default_value` is not compatible with `dtype`.
+ ValueError: if `dtype` is not convertible to `tf.float32`.
+ """
+ if default_value is None:
+ return None
+
+ if isinstance(default_value, int):
+ return _create_tuple(shape, default_value)
+
+ if isinstance(default_value, float) and dtype.is_floating:
+ return _create_tuple(shape, default_value)
+
+ if callable(getattr(default_value, 'tolist', None)): # Handles numpy arrays
+ default_value = default_value.tolist()
+
+ if nest.is_sequence(default_value):
+ if not _is_shape_and_default_value_compatible(default_value, shape):
+ raise ValueError(
+ 'The shape of default_value must be equal to given shape. '
+ 'default_value: {}, shape: {}, key: {}'.format(
+ default_value, shape, key))
+ # Check if the values in the list are all integers or are convertible to
+ # floats.
+ is_list_all_int = all(
+ isinstance(v, int) for v in nest.flatten(default_value))
+ is_list_has_float = any(
+ isinstance(v, float) for v in nest.flatten(default_value))
+ if is_list_all_int:
+ return _as_tuple(default_value)
+ if is_list_has_float and dtype.is_floating:
+ return _as_tuple(default_value)
+ raise TypeError('default_value must be compatible with dtype. '
+ 'default_value: {}, dtype: {}, key: {}'.format(
+ default_value, dtype, key))
+
+
+class HashedCategoricalColumn(
+ CategoricalColumn,
+ collections.namedtuple('HashedCategoricalColumn',
+ ('key', 'hash_bucket_size', 'dtype'))):
+ """see `categorical_column_with_hash_bucket`."""
+
+ @property
+ def name(self):
+ """See `FeatureColumn` base class."""
+ return self.key
+
+ @property
+ def parse_example_spec(self):
+ """See `FeatureColumn` base class."""
+ return {self.key: parsing_ops.VarLenFeature(self.dtype)}
+
+ def transform_feature(self, transformation_cache, state_manager):
+ """Hashes the values in the feature_column."""
+ input_tensor = _to_sparse_input_and_drop_ignore_values(
+ transformation_cache.get(self.key, state_manager))
+ if not isinstance(input_tensor, sparse_tensor_lib.SparseTensor):
+ raise ValueError('SparseColumn input must be a SparseTensor.')
+
+ _assert_string_or_int(
+ input_tensor.dtype,
+ prefix='column_name: {} input_tensor'.format(self.key))
+
+ if self.dtype.is_integer != input_tensor.dtype.is_integer:
+ raise ValueError(
+ 'Column dtype and SparseTensors dtype must be compatible. '
+ 'key: {}, column dtype: {}, tensor dtype: {}'.format(
+ self.key, self.dtype, input_tensor.dtype))
+
+ if self.dtype == dtypes.string:
+ sparse_values = input_tensor.values
+ else:
+ sparse_values = string_ops.as_string(input_tensor.values)
+
+ sparse_id_values = string_ops.string_to_hash_bucket_fast(
+ sparse_values, self.hash_bucket_size, name='lookup')
+ return sparse_tensor_lib.SparseTensor(
+ input_tensor.indices, sparse_id_values, input_tensor.dense_shape)
+
+ @property
+ def num_buckets(self):
+ """Returns number of buckets in this sparse feature."""
+ return self.hash_bucket_size
+
+ def get_sparse_tensors(self, transformation_cache, state_manager):
+ """See `CategoricalColumn` base class."""
+ return CategoricalColumn.IdWeightPair(
+ transformation_cache.get(self, state_manager), None)
+
+
+class VocabularyFileCategoricalColumn(
+ CategoricalColumn,
+ collections.namedtuple('VocabularyFileCategoricalColumn',
+ ('key', 'vocabulary_file', 'vocabulary_size',
+ 'num_oov_buckets', 'dtype', 'default_value'))):
+ """See `categorical_column_with_vocabulary_file`."""
+
+ @property
+ def name(self):
+ """See `FeatureColumn` base class."""
+ return self.key
+
+ @property
+ def parse_example_spec(self):
+ """See `FeatureColumn` base class."""
+ return {self.key: parsing_ops.VarLenFeature(self.dtype)}
+
+ def transform_feature(self, transformation_cache, state_manager):
+ """Creates a lookup table for the vocabulary."""
+ input_tensor = _to_sparse_input_and_drop_ignore_values(
+ transformation_cache.get(self.key, state_manager))
+
+ if self.dtype.is_integer != input_tensor.dtype.is_integer:
+ raise ValueError(
+ 'Column dtype and SparseTensors dtype must be compatible. '
+ 'key: {}, column dtype: {}, tensor dtype: {}'.format(
+ self.key, self.dtype, input_tensor.dtype))
+
+ _assert_string_or_int(
+ input_tensor.dtype,
+ prefix='column_name: {} input_tensor'.format(self.key))
+
+ key_dtype = self.dtype
+ if input_tensor.dtype.is_integer:
+ # `index_table_from_file` requires 64-bit integer keys.
+ key_dtype = dtypes.int64
+ input_tensor = math_ops.to_int64(input_tensor)
+
+ # TODO(rohanj): Use state manager to manage the index table creation.
+ return lookup_ops.index_table_from_file(
+ vocabulary_file=self.vocabulary_file,
+ num_oov_buckets=self.num_oov_buckets,
+ vocab_size=self.vocabulary_size,
+ default_value=self.default_value,
+ key_dtype=key_dtype,
+ name='{}_lookup'.format(self.key)).lookup(input_tensor)
+
+ @property
+ def num_buckets(self):
+ """Returns number of buckets in this sparse feature."""
+ return self.vocabulary_size + self.num_oov_buckets
+
+ def get_sparse_tensors(self, transformation_cache, state_manager):
+ """See `CategoricalColumn` base class."""
+ return CategoricalColumn.IdWeightPair(
+ transformation_cache.get(self, state_manager), None)
+
+
+class VocabularyListCategoricalColumn(
+ CategoricalColumn,
+ collections.namedtuple(
+ 'VocabularyListCategoricalColumn',
+ ('key', 'vocabulary_list', 'dtype', 'default_value', 'num_oov_buckets'))
+):
+ """See `categorical_column_with_vocabulary_list`."""
+
+ @property
+ def name(self):
+ """See `FeatureColumn` base class."""
+ return self.key
+
+ @property
+ def parse_example_spec(self):
+ """See `FeatureColumn` base class."""
+ return {self.key: parsing_ops.VarLenFeature(self.dtype)}
+
+ def transform_feature(self, transformation_cache, state_manager):
+ """Creates a lookup table for the vocabulary list."""
+ input_tensor = _to_sparse_input_and_drop_ignore_values(
+ transformation_cache.get(self.key, state_manager))
+
+ if self.dtype.is_integer != input_tensor.dtype.is_integer:
+ raise ValueError(
+ 'Column dtype and SparseTensors dtype must be compatible. '
+ 'key: {}, column dtype: {}, tensor dtype: {}'.format(
+ self.key, self.dtype, input_tensor.dtype))
+
+ _assert_string_or_int(
+ input_tensor.dtype,
+ prefix='column_name: {} input_tensor'.format(self.key))
+
+ key_dtype = self.dtype
+ if input_tensor.dtype.is_integer:
+ # `index_table_from_tensor` requires 64-bit integer keys.
+ key_dtype = dtypes.int64
+ input_tensor = math_ops.to_int64(input_tensor)
+
+ # TODO(rohanj): Use state manager to manage the index table creation.
+ return lookup_ops.index_table_from_tensor(
+ vocabulary_list=tuple(self.vocabulary_list),
+ default_value=self.default_value,
+ num_oov_buckets=self.num_oov_buckets,
+ dtype=key_dtype,
+ name='{}_lookup'.format(self.key)).lookup(input_tensor)
+
+ @property
+ def num_buckets(self):
+ """Returns number of buckets in this sparse feature."""
+ return len(self.vocabulary_list) + self.num_oov_buckets
+
+ def get_sparse_tensors(self, transformation_cache, state_manager):
+ """See `CategoricalColumn` base class."""
+ return CategoricalColumn.IdWeightPair(
+ transformation_cache.get(self, state_manager), None)
+
+
+class IdentityCategoricalColumn(
+ CategoricalColumn,
+ collections.namedtuple('IdentityCategoricalColumn',
+ ('key', 'number_buckets', 'default_value'))):
+
+ """See `categorical_column_with_identity`."""
+
+ @property
+ def name(self):
+ """See `FeatureColumn` base class."""
+ return self.key
+
+ @property
+ def parse_example_spec(self):
+ """See `FeatureColumn` base class."""
+ return {self.key: parsing_ops.VarLenFeature(dtypes.int64)}
+
+ def transform_feature(self, transformation_cache, state_manager):
+ """Returns a SparseTensor with identity values."""
+ input_tensor = _to_sparse_input_and_drop_ignore_values(
+ transformation_cache.get(self.key, state_manager))
+
+ if not input_tensor.dtype.is_integer:
+ raise ValueError(
+ 'Invalid input, not integer. key: {} dtype: {}'.format(
+ self.key, input_tensor.dtype))
+
+ values = math_ops.to_int64(input_tensor.values, name='values')
+ num_buckets = math_ops.to_int64(self.num_buckets, name='num_buckets')
+ zero = math_ops.to_int64(0, name='zero')
+ if self.default_value is None:
+ # Fail if values are out-of-range.
+ assert_less = check_ops.assert_less(
+ values, num_buckets, data=(values, num_buckets),
+ name='assert_less_than_num_buckets')
+ assert_greater = check_ops.assert_greater_equal(
+ values, zero, data=(values,),
+ name='assert_greater_or_equal_0')
+ with ops.control_dependencies((assert_less, assert_greater)):
+ values = array_ops.identity(values)
+ else:
+ # Assign default for out-of-range values.
+ values = array_ops.where(
+ math_ops.logical_or(
+ values < zero, values >= num_buckets, name='out_of_range'),
+ array_ops.fill(
+ dims=array_ops.shape(values),
+ value=math_ops.to_int64(self.default_value),
+ name='default_values'),
+ values)
+
+ return sparse_tensor_lib.SparseTensor(
+ indices=input_tensor.indices,
+ values=values,
+ dense_shape=input_tensor.dense_shape)
+
+ @property
+ def num_buckets(self):
+ """Returns number of buckets in this sparse feature."""
+ return self.number_buckets
+
+ def get_sparse_tensors(self, transformation_cache, state_manager):
+ """See `CategoricalColumn` base class."""
+ return CategoricalColumn.IdWeightPair(
+ transformation_cache.get(self, state_manager), None)
+
+
+class WeightedCategoricalColumn(
+ CategoricalColumn,
+ collections.namedtuple(
+ 'WeightedCategoricalColumn',
+ ('categorical_column', 'weight_feature_key', 'dtype'))):
+ """See `weighted_categorical_column`."""
+
+ @property
+ def name(self):
+ """See `FeatureColumn` base class."""
+ return '{}_weighted_by_{}'.format(
+ self.categorical_column.name, self.weight_feature_key)
+
+ @property
+ def parse_example_spec(self):
+ """See `FeatureColumn` base class."""
+ config = self.categorical_column.parse_example_spec
+ if self.weight_feature_key in config:
+ raise ValueError('Parse config {} already exists for {}.'.format(
+ config[self.weight_feature_key], self.weight_feature_key))
+ config[self.weight_feature_key] = parsing_ops.VarLenFeature(self.dtype)
+ return config
+
+ @property
+ def num_buckets(self):
+ """See `DenseColumn` base class."""
+ return self.categorical_column.num_buckets
+
+ def transform_feature(self, transformation_cache, state_manager):
+ """Applies weights to tensor generated from `categorical_column`'."""
+ weight_tensor = transformation_cache.get(self.weight_feature_key,
+ state_manager)
+ if weight_tensor is None:
+ raise ValueError('Missing weights {}.'.format(self.weight_feature_key))
+ weight_tensor = sparse_tensor_lib.convert_to_tensor_or_sparse_tensor(
+ weight_tensor)
+ if self.dtype != weight_tensor.dtype.base_dtype:
+ raise ValueError('Bad dtype, expected {}, but got {}.'.format(
+ self.dtype, weight_tensor.dtype))
+ if not isinstance(weight_tensor, sparse_tensor_lib.SparseTensor):
+ # The weight tensor can be a regular Tensor. In this case, sparsify it.
+ weight_tensor = _to_sparse_input_and_drop_ignore_values(
+ weight_tensor, ignore_value=0.0)
+ if not weight_tensor.dtype.is_floating:
+ weight_tensor = math_ops.to_float(weight_tensor)
+ return (transformation_cache.get(self.categorical_column, state_manager),
+ weight_tensor)
+
+ def get_sparse_tensors(self, transformation_cache, state_manager):
+ """See `CategoricalColumn` base class."""
+ tensors = transformation_cache.get(self, state_manager)
+ return CategoricalColumn.IdWeightPair(tensors[0], tensors[1])
+
+
+class CrossedColumn(
+ CategoricalColumn,
+ collections.namedtuple('CrossedColumn',
+ ('keys', 'hash_bucket_size', 'hash_key'))):
+ """See `crossed_column`."""
+
+ @property
+ def name(self):
+ """See `FeatureColumn` base class."""
+ feature_names = []
+ for key in _collect_leaf_level_keys(self):
+ if isinstance(key, FeatureColumn):
+ feature_names.append(key.name)
+ else: # key must be a string
+ feature_names.append(key)
+ return '_X_'.join(sorted(feature_names))
+
+ @property
+ def parse_example_spec(self):
+ """See `FeatureColumn` base class."""
+ config = {}
+ for key in self.keys:
+ if isinstance(key, FeatureColumn):
+ config.update(key.parse_example_spec)
+ else: # key must be a string
+ config.update({key: parsing_ops.VarLenFeature(dtypes.string)})
+ return config
+
+ def transform_feature(self, transformation_cache, state_manager):
+ """Generates a hashed sparse cross from the input tensors."""
+ feature_tensors = []
+ for key in _collect_leaf_level_keys(self):
+ if isinstance(key, six.string_types):
+ feature_tensors.append(transformation_cache.get(key, state_manager))
+ elif isinstance(key, CategoricalColumn):
+ ids_and_weights = key.get_sparse_tensors(transformation_cache,
+ state_manager)
+ if ids_and_weights.weight_tensor is not None:
+ raise ValueError(
+ 'crossed_column does not support weight_tensor, but the given '
+ 'column populates weight_tensor. '
+ 'Given column: {}'.format(key.name))
+ feature_tensors.append(ids_and_weights.id_tensor)
+ else:
+ raise ValueError('Unsupported column type. Given: {}'.format(key))
+ return sparse_ops.sparse_cross_hashed(
+ inputs=feature_tensors,
+ num_buckets=self.hash_bucket_size,
+ hash_key=self.hash_key)
+
+ @property
+ def num_buckets(self):
+ """Returns number of buckets in this sparse feature."""
+ return self.hash_bucket_size
+
+ def get_sparse_tensors(self, transformation_cache, state_manager):
+ """See `CategoricalColumn` base class."""
+ return CategoricalColumn.IdWeightPair(
+ transformation_cache.get(self, state_manager), None)
+
+
+def _collect_leaf_level_keys(cross):
+ """Collects base keys by expanding all nested crosses.
+
+ Args:
+ cross: A `CrossedColumn`.
+
+ Returns:
+ A list of strings or `CategoricalColumn` instances.
+ """
+ leaf_level_keys = []
+ for k in cross.keys:
+ if isinstance(k, CrossedColumn):
+ leaf_level_keys.extend(_collect_leaf_level_keys(k))
+ else:
+ leaf_level_keys.append(k)
+ return leaf_level_keys
+
+
+# TODO(zakaria): Move this to embedding_ops and make it public.
+def _safe_embedding_lookup_sparse(embedding_weights,
+ sparse_ids,
+ sparse_weights=None,
+ combiner='mean',
+ default_id=None,
+ name=None,
+ partition_strategy='div',
+ max_norm=None):
+ """Lookup embedding results, accounting for invalid IDs and empty features.
+
+ The partitioned embedding in `embedding_weights` must all be the same shape
+ except for the first dimension. The first dimension is allowed to vary as the
+ vocabulary size is not necessarily a multiple of `P`. `embedding_weights`
+ may be a `PartitionedVariable` as returned by using `tf.get_variable()` with a
+ partitioner.
+
+ Invalid IDs (< 0) are pruned from input IDs and weights, as well as any IDs
+ with non-positive weight. For an entry with no features, the embedding vector
+ for `default_id` is returned, or the 0-vector if `default_id` is not supplied.
+
+ The ids and weights may be multi-dimensional. Embeddings are always aggregated
+ along the last dimension.
+
+ Args:
+ embedding_weights: A list of `P` float `Tensor`s or values representing
+ partitioned embedding `Tensor`s. Alternatively, a `PartitionedVariable`
+ created by partitioning along dimension 0. The total unpartitioned
+ shape should be `[e_0, e_1, ..., e_m]`, where `e_0` represents the
+ vocab size and `e_1, ..., e_m` are the embedding dimensions.
+ sparse_ids: `SparseTensor` of shape `[d_0, d_1, ..., d_n]` containing the
+ ids. `d_0` is typically batch size.
+ sparse_weights: `SparseTensor` of same shape as `sparse_ids`, containing
+ float weights corresponding to `sparse_ids`, or `None` if all weights
+ are be assumed to be 1.0.
+ combiner: A string specifying how to combine embedding results for each
+ entry. Currently "mean", "sqrtn" and "sum" are supported, with "mean"
+ the default.
+ default_id: The id to use for an entry with no features.
+ name: A name for this operation (optional).
+ partition_strategy: A string specifying the partitioning strategy.
+ Currently `"div"` and `"mod"` are supported. Default is `"div"`.
+ max_norm: If not `None`, all embeddings are l2-normalized to max_norm before
+ combining.
+
+
+ Returns:
+ Dense `Tensor` of shape `[d_0, d_1, ..., d_{n-1}, e_1, ..., e_m]`.
+
+ Raises:
+ ValueError: if `embedding_weights` is empty.
+ """
+ if embedding_weights is None:
+ raise ValueError('Missing embedding_weights %s.' % embedding_weights)
+ if isinstance(embedding_weights, variables.PartitionedVariable):
+ embedding_weights = list(embedding_weights) # get underlying Variables.
+ if not isinstance(embedding_weights, list):
+ embedding_weights = [embedding_weights]
+ if len(embedding_weights) < 1:
+ raise ValueError('Missing embedding_weights %s.' % embedding_weights)
+
+ dtype = sparse_weights.dtype if sparse_weights is not None else None
+ embedding_weights = [
+ ops.convert_to_tensor(w, dtype=dtype) for w in embedding_weights
+ ]
+
+ with ops.name_scope(name, 'embedding_lookup',
+ embedding_weights + [sparse_ids,
+ sparse_weights]) as scope:
+ # Reshape higher-rank sparse ids and weights to linear segment ids.
+ original_shape = sparse_ids.dense_shape
+ original_rank_dim = sparse_ids.dense_shape.get_shape()[0]
+ original_rank = (
+ array_ops.size(original_shape)
+ if original_rank_dim.value is None
+ else original_rank_dim.value)
+ sparse_ids = sparse_ops.sparse_reshape(sparse_ids, [
+ math_ops.reduce_prod(
+ array_ops.slice(original_shape, [0], [original_rank - 1])),
+ array_ops.gather(original_shape, original_rank - 1)])
+ if sparse_weights is not None:
+ sparse_weights = sparse_tensor_lib.SparseTensor(
+ sparse_ids.indices,
+ sparse_weights.values, sparse_ids.dense_shape)
+
+ # Prune invalid ids and weights.
+ sparse_ids, sparse_weights = _prune_invalid_ids(sparse_ids, sparse_weights)
+ if combiner != 'sum':
+ sparse_ids, sparse_weights = _prune_invalid_weights(
+ sparse_ids, sparse_weights)
+
+ # Fill in dummy values for empty features, if necessary.
+ sparse_ids, is_row_empty = sparse_ops.sparse_fill_empty_rows(sparse_ids,
+ default_id or
+ 0)
+ if sparse_weights is not None:
+ sparse_weights, _ = sparse_ops.sparse_fill_empty_rows(sparse_weights, 1.0)
+
+ result = embedding_ops.embedding_lookup_sparse(
+ embedding_weights,
+ sparse_ids,
+ sparse_weights,
+ combiner=combiner,
+ partition_strategy=partition_strategy,
+ name=None if default_id is None else scope,
+ max_norm=max_norm)
+
+ if default_id is None:
+ # Broadcast is_row_empty to the same shape as embedding_lookup_result,
+ # for use in Select.
+ is_row_empty = array_ops.tile(
+ array_ops.reshape(is_row_empty, [-1, 1]),
+ array_ops.stack([1, array_ops.shape(result)[1]]))
+
+ result = array_ops.where(is_row_empty,
+ array_ops.zeros_like(result),
+ result,
+ name=scope)
+
+ # Reshape back from linear ids back into higher-dimensional dense result.
+ final_result = array_ops.reshape(
+ result,
+ array_ops.concat([
+ array_ops.slice(
+ math_ops.cast(original_shape, dtypes.int32), [0],
+ [original_rank - 1]),
+ array_ops.slice(array_ops.shape(result), [1], [-1])
+ ], 0))
+ final_result.set_shape(tensor_shape.unknown_shape(
+ (original_rank_dim - 1).value).concatenate(result.get_shape()[1:]))
+ return final_result
+
+
+def _prune_invalid_ids(sparse_ids, sparse_weights):
+ """Prune invalid IDs (< 0) from the input ids and weights."""
+ is_id_valid = math_ops.greater_equal(sparse_ids.values, 0)
+ if sparse_weights is not None:
+ is_id_valid = math_ops.logical_and(
+ is_id_valid,
+ array_ops.ones_like(sparse_weights.values, dtype=dtypes.bool))
+ sparse_ids = sparse_ops.sparse_retain(sparse_ids, is_id_valid)
+ if sparse_weights is not None:
+ sparse_weights = sparse_ops.sparse_retain(sparse_weights, is_id_valid)
+ return sparse_ids, sparse_weights
+
+
+def _prune_invalid_weights(sparse_ids, sparse_weights):
+ """Prune invalid weights (< 0) from the input ids and weights."""
+ if sparse_weights is not None:
+ is_weights_valid = math_ops.greater(sparse_weights.values, 0)
+ sparse_ids = sparse_ops.sparse_retain(sparse_ids, is_weights_valid)
+ sparse_weights = sparse_ops.sparse_retain(sparse_weights, is_weights_valid)
+ return sparse_ids, sparse_weights
+
+
+class IndicatorColumn(DenseColumn, SequenceDenseColumn,
+ collections.namedtuple('IndicatorColumn',
+ ('categorical_column'))):
+ """Represents a one-hot column for use in deep networks.
+
+ Args:
+ categorical_column: A `CategoricalColumn` which is created by
+ `categorical_column_with_*` function.
+ """
+
+ @property
+ def name(self):
+ """See `FeatureColumn` base class."""
+ return '{}_indicator'.format(self.categorical_column.name)
+
+ def transform_feature(self, transformation_cache, state_manager):
+ """Returns dense `Tensor` representing feature.
+
+ Args:
+ transformation_cache: A `FeatureTransformationCache` object to access
+ features.
+ state_manager: A `StateManager` to create / access resources such as
+ lookup tables.
+
+ Returns:
+ Transformed feature `Tensor`.
+
+ Raises:
+ ValueError: if input rank is not known at graph building time.
+ """
+ id_weight_pair = self.categorical_column.get_sparse_tensors(
+ transformation_cache, state_manager)
+ id_tensor = id_weight_pair.id_tensor
+ weight_tensor = id_weight_pair.weight_tensor
+
+ # If the underlying column is weighted, return the input as a dense tensor.
+ if weight_tensor is not None:
+ weighted_column = sparse_ops.sparse_merge(
+ sp_ids=id_tensor,
+ sp_values=weight_tensor,
+ vocab_size=int(self.variable_shape[-1]))
+ # Remove (?, -1) index
+ weighted_column = sparse_ops.sparse_slice(weighted_column, [0, 0],
+ weighted_column.dense_shape)
+ return sparse_ops.sparse_tensor_to_dense(weighted_column)
+
+ dense_id_tensor = sparse_ops.sparse_tensor_to_dense(
+ id_tensor, default_value=-1)
+
+ # One hot must be float for tf.concat reasons since all other inputs to
+ # input_layer are float32.
+ one_hot_id_tensor = array_ops.one_hot(
+ dense_id_tensor,
+ depth=self.variable_shape[-1],
+ on_value=1.0,
+ off_value=0.0)
+
+ # Reduce to get a multi-hot per example.
+ return math_ops.reduce_sum(one_hot_id_tensor, axis=[-2])
+
+ @property
+ def parse_example_spec(self):
+ """See `FeatureColumn` base class."""
+ return self.categorical_column.parse_example_spec
+
+ @property
+ def variable_shape(self):
+ """Returns a `TensorShape` representing the shape of the dense `Tensor`."""
+ return tensor_shape.TensorShape([1, self.categorical_column.num_buckets])
+
+ def get_dense_tensor(self, transformation_cache, state_manager):
+ """Returns dense `Tensor` representing feature.
+
+ Args:
+ transformation_cache: A `FeatureTransformationCache` object to access
+ features.
+ state_manager: A `StateManager` to create / access resources such as
+ lookup tables.
+
+ Returns:
+ Dense `Tensor` created within `transform_feature`.
+
+ Raises:
+ ValueError: If `categorical_column` is a `SequenceCategoricalColumn`.
+ """
+ if isinstance(self.categorical_column, SequenceCategoricalColumn):
+ raise ValueError(
+ 'In indicator_column: {}. '
+ 'categorical_column must not be of type SequenceCategoricalColumn. '
+ 'Suggested fix A: If you wish to use input_layer, use a '
+ 'non-sequence categorical_column_with_*. '
+ 'Suggested fix B: If you wish to create sequence input, use '
+ 'sequence_input_layer instead of input_layer. '
+ 'Given (type {}): {}'.format(self.name, type(self.categorical_column),
+ self.categorical_column))
+ # Feature has been already transformed. Return the intermediate
+ # representation created by transform_feature.
+ return transformation_cache.get(self, state_manager)
+
+ def get_sequence_dense_tensor(self, transformation_cache, state_manager):
+ """See `SequenceDenseColumn` base class."""
+ if not isinstance(self.categorical_column, SequenceCategoricalColumn):
+ raise ValueError(
+ 'In indicator_column: {}. '
+ 'categorical_column must be of type SequenceCategoricalColumn '
+ 'to use sequence_input_layer. '
+ 'Suggested fix: Use one of sequence_categorical_column_with_*. '
+ 'Given (type {}): {}'.format(self.name, type(self.categorical_column),
+ self.categorical_column))
+ # Feature has been already transformed. Return the intermediate
+ # representation created by transform_feature.
+ dense_tensor = transformation_cache.get(self, state_manager)
+ sparse_tensors = self.categorical_column.get_sparse_tensors(
+ transformation_cache, state_manager)
+ sequence_length = _sequence_length_from_sparse_tensor(
+ sparse_tensors.id_tensor)
+ return SequenceDenseColumn.TensorSequenceLengthPair(
+ dense_tensor=dense_tensor, sequence_length=sequence_length)
+
+
+def _verify_static_batch_size_equality(tensors, columns):
+ # bath_size is a tf.Dimension object.
+ expected_batch_size = None
+ for i in range(0, len(tensors)):
+ if tensors[i].shape[0].value is not None:
+ if expected_batch_size is None:
+ bath_size_column_index = i
+ expected_batch_size = tensors[i].shape[0]
+ elif not expected_batch_size.is_compatible_with(tensors[i].shape[0]):
+ raise ValueError(
+ 'Batch size (first dimension) of each feature must be same. '
+ 'Batch size of columns ({}, {}): ({}, {})'.format(
+ columns[bath_size_column_index].name, columns[i].name,
+ expected_batch_size, tensors[i].shape[0]))
+
+
+def _sequence_length_from_sparse_tensor(sp_tensor, num_elements=1):
+ """Returns a [batch_size] Tensor with per-example sequence length."""
+ with ops.name_scope(None, 'sequence_length') as name_scope:
+ row_ids = sp_tensor.indices[:, 0]
+ column_ids = sp_tensor.indices[:, 1]
+ column_ids += array_ops.ones_like(column_ids)
+ seq_length = math_ops.to_int64(
+ math_ops.segment_max(column_ids, segment_ids=row_ids) / num_elements)
+ # If the last n rows do not have ids, seq_length will have shape
+ # [batch_size - n]. Pad the remaining values with zeros.
+ n_pad = array_ops.shape(sp_tensor)[:1] - array_ops.shape(seq_length)[:1]
+ padding = array_ops.zeros(n_pad, dtype=seq_length.dtype)
+ return array_ops.concat([seq_length, padding], axis=0, name=name_scope)
+
+
+class SequenceCategoricalColumn(FeatureColumn,
+ collections.namedtuple(
+ 'SequenceCategoricalColumn',
+ ('categorical_column'))):
+ """Represents sequences of categorical data."""
+
+ @property
+ def name(self):
+ """See `FeatureColumn` base class."""
+ return self.categorical_column.name
+
+ @property
+ def parse_example_spec(self):
+ """See `FeatureColumn` base class."""
+ return self.categorical_column.parse_example_spec
+
+ def transform_feature(self, transformation_cache, state_manager):
+ """See `FeatureColumn` base class."""
+ return self.categorical_column.transform_feature(transformation_cache,
+ state_manager)
+
+ @property
+ def num_buckets(self):
+ """Returns number of buckets in this sparse feature."""
+ return self.categorical_column.num_buckets
+
+ def get_sequence_sparse_tensors(self, transformation_cache, state_manager):
+ """Returns an IdWeightPair.
+
+ `IdWeightPair` is a pair of `SparseTensor`s which represents ids and
+ weights.
+
+ `IdWeightPair.id_tensor` is typically a `batch_size` x `num_buckets`
+ `SparseTensor` of `int64`. `IdWeightPair.weight_tensor` is either a
+ `SparseTensor` of `float` or `None` to indicate all weights should be
+ taken to be 1. If specified, `weight_tensor` must have exactly the same
+ shape and indices as `sp_ids`. Expected `SparseTensor` is same as parsing
+ output of a `VarLenFeature` which is a ragged matrix.
+
+ Args:
+ transformation_cache: A `FeatureTransformationCache` object to access
+ features.
+ state_manager: A `StateManager` to create / access resources such as
+ lookup tables.
+ """
+ sparse_tensors = self.categorical_column.get_sparse_tensors(
+ transformation_cache, state_manager)
+ id_tensor = sparse_tensors.id_tensor
+ weight_tensor = sparse_tensors.weight_tensor
+ # Expands final dimension, so that embeddings are not combined during
+ # embedding lookup.
+ check_id_rank = check_ops.assert_equal(
+ array_ops.rank(id_tensor), 2,
+ data=[
+ 'Column {} expected ID tensor of rank 2. '.format(self.name),
+ 'id_tensor shape: ', array_ops.shape(id_tensor)])
+ with ops.control_dependencies([check_id_rank]):
+ id_tensor = sparse_ops.sparse_reshape(
+ id_tensor,
+ shape=array_ops.concat([id_tensor.dense_shape, [1]], axis=0))
+ if weight_tensor is not None:
+ check_weight_rank = check_ops.assert_equal(
+ array_ops.rank(weight_tensor), 2,
+ data=[
+ 'Column {} expected weight tensor of rank 2.'.format(self.name),
+ 'weight_tensor shape:', array_ops.shape(weight_tensor)])
+ with ops.control_dependencies([check_weight_rank]):
+ weight_tensor = sparse_ops.sparse_reshape(
+ weight_tensor,
+ shape=array_ops.concat([weight_tensor.dense_shape, [1]], axis=0))
+ return CategoricalColumn.IdWeightPair(id_tensor, weight_tensor)
diff --git a/tensorflow/python/feature_column/feature_column_v2_test.py b/tensorflow/python/feature_column/feature_column_v2_test.py
new file mode 100644
index 0000000000..80a9d5d40e
--- /dev/null
+++ b/tensorflow/python/feature_column/feature_column_v2_test.py
@@ -0,0 +1,6583 @@
+# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
+#
+# 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.
+# ==============================================================================
+"""Tests for feature_column."""
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+import collections
+import copy
+
+import numpy as np
+
+from tensorflow.core.example import example_pb2
+from tensorflow.core.example import feature_pb2
+from tensorflow.core.protobuf import config_pb2
+from tensorflow.core.protobuf import rewriter_config_pb2
+from tensorflow.python.client import session
+from tensorflow.python.eager import backprop
+from tensorflow.python.eager import context
+from tensorflow.python.estimator.inputs import numpy_io
+from tensorflow.python.feature_column import feature_column as fc_old
+from tensorflow.python.feature_column import feature_column_v2 as fc
+from tensorflow.python.feature_column.feature_column_v2 import FeatureColumn
+from tensorflow.python.feature_column.feature_column_v2 import FeatureTransformationCache
+from tensorflow.python.feature_column.feature_column_v2 import InputLayer
+from tensorflow.python.feature_column.feature_column_v2 import StateManager
+from tensorflow.python.feature_column.feature_column_v2 import _LinearModel
+from tensorflow.python.feature_column.feature_column_v2 import _transform_features
+from tensorflow.python.framework import constant_op
+from tensorflow.python.framework import dtypes
+from tensorflow.python.framework import errors
+from tensorflow.python.framework import ops
+from tensorflow.python.framework import sparse_tensor
+from tensorflow.python.framework import test_util
+from tensorflow.python.ops import array_ops
+from tensorflow.python.ops import lookup_ops
+from tensorflow.python.ops import parsing_ops
+from tensorflow.python.ops import partitioned_variables
+from tensorflow.python.ops import variable_scope
+from tensorflow.python.ops import variables as variables_lib
+from tensorflow.python.platform import test
+from tensorflow.python.training import coordinator
+from tensorflow.python.training import queue_runner_impl
+
+
+def _initialized_session(config=None):
+ sess = session.Session(config=config)
+ sess.run(variables_lib.global_variables_initializer())
+ sess.run(lookup_ops.tables_initializer())
+ return sess
+
+
+class LazyColumnTest(test.TestCase):
+
+ def test_transformations_called_once(self):
+
+ class TransformCounter(FeatureColumn):
+
+ def __init__(self):
+ self.num_transform = 0
+
+ @property
+ def name(self):
+ return 'TransformCounter'
+
+ def transform_feature(self, transformation_cache, state_manager):
+ self.num_transform += 1 # Count transform calls.
+ return transformation_cache.get('a', state_manager)
+
+ @property
+ def parse_example_spec(self):
+ pass
+
+ transformation_cache = FeatureTransformationCache(
+ features={'a': [[2], [3.]]})
+ column = TransformCounter()
+ self.assertEqual(0, column.num_transform)
+ transformation_cache.get(column, None)
+ self.assertEqual(1, column.num_transform)
+ transformation_cache.get(column, None)
+ self.assertEqual(1, column.num_transform)
+
+ def test_returns_transform_output(self):
+
+ class Transformer(FeatureColumn):
+
+ @property
+ def name(self):
+ return 'Transformer'
+
+ def transform_feature(self, transformation_cache, state_manager):
+ return 'Output'
+
+ @property
+ def parse_example_spec(self):
+ pass
+
+ transformation_cache = FeatureTransformationCache(
+ features={'a': [[2], [3.]]})
+ column = Transformer()
+ self.assertEqual('Output', transformation_cache.get(column, None))
+ self.assertEqual('Output', transformation_cache.get(column, None))
+
+ def test_does_not_pollute_given_features_dict(self):
+
+ class Transformer(FeatureColumn):
+
+ @property
+ def name(self):
+ return 'Transformer'
+
+ def transform_feature(self, transformation_cache, state_manager):
+ return 'Output'
+
+ @property
+ def parse_example_spec(self):
+ pass
+
+ features = {'a': [[2], [3.]]}
+ transformation_cache = FeatureTransformationCache(features=features)
+ transformation_cache.get(Transformer(), None)
+ self.assertEqual(['a'], list(features.keys()))
+
+ def test_error_if_feature_is_not_found(self):
+ transformation_cache = FeatureTransformationCache(
+ features={'a': [[2], [3.]]})
+ with self.assertRaisesRegexp(ValueError,
+ 'bbb is not in features dictionary'):
+ transformation_cache.get('bbb', None)
+ with self.assertRaisesRegexp(ValueError,
+ 'bbb is not in features dictionary'):
+ transformation_cache.get(u'bbb', None)
+
+ def test_not_supported_feature_column(self):
+
+ class NotAProperColumn(FeatureColumn):
+
+ @property
+ def name(self):
+ return 'NotAProperColumn'
+
+ def transform_feature(self, transformation_cache, state_manager):
+ # It should return not None.
+ pass
+
+ @property
+ def parse_example_spec(self):
+ pass
+
+ transformation_cache = FeatureTransformationCache(
+ features={'a': [[2], [3.]]})
+ with self.assertRaisesRegexp(ValueError,
+ 'NotAProperColumn is not supported'):
+ transformation_cache.get(NotAProperColumn(), None)
+
+ def test_key_should_be_string_or_feature_colum(self):
+
+ class NotAFeatureColumn(object):
+ pass
+
+ transformation_cache = FeatureTransformationCache(
+ features={'a': [[2], [3.]]})
+ with self.assertRaisesRegexp(
+ TypeError, '"key" must be either a "str" or "FeatureColumn".'):
+ transformation_cache.get(NotAFeatureColumn(), None)
+
+
+class NumericColumnTest(test.TestCase):
+
+ def test_defaults(self):
+ a = fc.numeric_column('aaa')
+ self.assertEqual('aaa', a.key)
+ self.assertEqual('aaa', a.name)
+ self.assertEqual((1,), a.shape)
+ self.assertIsNone(a.default_value)
+ self.assertEqual(dtypes.float32, a.dtype)
+ self.assertIsNone(a.normalizer_fn)
+
+ def test_key_should_be_string(self):
+ with self.assertRaisesRegexp(ValueError, 'key must be a string.'):
+ fc.numeric_column(key=('aaa',))
+
+ def test_shape_saved_as_tuple(self):
+ a = fc.numeric_column('aaa', shape=[1, 2], default_value=[[3, 2.]])
+ self.assertEqual((1, 2), a.shape)
+
+ def test_default_value_saved_as_tuple(self):
+ a = fc.numeric_column('aaa', default_value=4.)
+ self.assertEqual((4.,), a.default_value)
+ a = fc.numeric_column('aaa', shape=[1, 2], default_value=[[3, 2.]])
+ self.assertEqual(((3., 2.),), a.default_value)
+
+ def test_shape_and_default_value_compatibility(self):
+ fc.numeric_column('aaa', shape=[2], default_value=[1, 2.])
+ with self.assertRaisesRegexp(ValueError, 'The shape of default_value'):
+ fc.numeric_column('aaa', shape=[2], default_value=[1, 2, 3.])
+ fc.numeric_column(
+ 'aaa', shape=[3, 2], default_value=[[2, 3], [1, 2], [2, 3.]])
+ with self.assertRaisesRegexp(ValueError, 'The shape of default_value'):
+ fc.numeric_column(
+ 'aaa', shape=[3, 1], default_value=[[2, 3], [1, 2], [2, 3.]])
+ with self.assertRaisesRegexp(ValueError, 'The shape of default_value'):
+ fc.numeric_column(
+ 'aaa', shape=[3, 3], default_value=[[2, 3], [1, 2], [2, 3.]])
+
+ def test_default_value_type_check(self):
+ fc.numeric_column(
+ 'aaa', shape=[2], default_value=[1, 2.], dtype=dtypes.float32)
+ fc.numeric_column(
+ 'aaa', shape=[2], default_value=[1, 2], dtype=dtypes.int32)
+ with self.assertRaisesRegexp(TypeError, 'must be compatible with dtype'):
+ fc.numeric_column(
+ 'aaa', shape=[2], default_value=[1, 2.], dtype=dtypes.int32)
+ with self.assertRaisesRegexp(TypeError,
+ 'default_value must be compatible with dtype'):
+ fc.numeric_column('aaa', default_value=['string'])
+
+ def test_shape_must_be_positive_integer(self):
+ with self.assertRaisesRegexp(TypeError, 'shape dimensions must be integer'):
+ fc.numeric_column(
+ 'aaa', shape=[
+ 1.0,
+ ])
+
+ with self.assertRaisesRegexp(ValueError,
+ 'shape dimensions must be greater than 0'):
+ fc.numeric_column(
+ 'aaa', shape=[
+ 0,
+ ])
+
+ def test_dtype_is_convertible_to_float(self):
+ with self.assertRaisesRegexp(ValueError,
+ 'dtype must be convertible to float'):
+ fc.numeric_column('aaa', dtype=dtypes.string)
+
+ def test_scalar_default_value_fills_the_shape(self):
+ a = fc.numeric_column('aaa', shape=[2, 3], default_value=2.)
+ self.assertEqual(((2., 2., 2.), (2., 2., 2.)), a.default_value)
+
+ def test_parse_spec(self):
+ a = fc.numeric_column('aaa', shape=[2, 3], dtype=dtypes.int32)
+ self.assertEqual({
+ 'aaa': parsing_ops.FixedLenFeature((2, 3), dtype=dtypes.int32)
+ }, a.parse_example_spec)
+
+ def test_parse_example_no_default_value(self):
+ price = fc.numeric_column('price', shape=[2])
+ data = example_pb2.Example(features=feature_pb2.Features(
+ feature={
+ 'price':
+ feature_pb2.Feature(float_list=feature_pb2.FloatList(
+ value=[20., 110.]))
+ }))
+ features = parsing_ops.parse_example(
+ serialized=[data.SerializeToString()],
+ features=fc.make_parse_example_spec([price]))
+ self.assertIn('price', features)
+ with self.test_session():
+ self.assertAllEqual([[20., 110.]], features['price'].eval())
+
+ def test_parse_example_with_default_value(self):
+ price = fc.numeric_column('price', shape=[2], default_value=11.)
+ data = example_pb2.Example(features=feature_pb2.Features(
+ feature={
+ 'price':
+ feature_pb2.Feature(float_list=feature_pb2.FloatList(
+ value=[20., 110.]))
+ }))
+ no_data = example_pb2.Example(features=feature_pb2.Features(
+ feature={
+ 'something_else':
+ feature_pb2.Feature(float_list=feature_pb2.FloatList(
+ value=[20., 110.]))
+ }))
+ features = parsing_ops.parse_example(
+ serialized=[data.SerializeToString(),
+ no_data.SerializeToString()],
+ features=fc.make_parse_example_spec([price]))
+ self.assertIn('price', features)
+ with self.test_session():
+ self.assertAllEqual([[20., 110.], [11., 11.]], features['price'].eval())
+
+ def test_normalizer_fn_must_be_callable(self):
+ with self.assertRaisesRegexp(TypeError, 'must be a callable'):
+ fc.numeric_column('price', normalizer_fn='NotACallable')
+
+ def test_normalizer_fn_transform_feature(self):
+
+ def _increment_two(input_tensor):
+ return input_tensor + 2.
+
+ price = fc.numeric_column('price', shape=[2], normalizer_fn=_increment_two)
+ output = _transform_features({'price': [[1., 2.], [5., 6.]]}, [price], None)
+ with self.test_session():
+ self.assertAllEqual([[3., 4.], [7., 8.]], output[price].eval())
+
+ def test_get_dense_tensor(self):
+
+ def _increment_two(input_tensor):
+ return input_tensor + 2.
+
+ price = fc.numeric_column('price', shape=[2], normalizer_fn=_increment_two)
+ transformation_cache = FeatureTransformationCache({
+ 'price': [[1., 2.], [5., 6.]]
+ })
+ self.assertEqual(
+ transformation_cache.get(price, None),
+ price.get_dense_tensor(transformation_cache, None))
+
+ def test_sparse_tensor_not_supported(self):
+ price = fc.numeric_column('price')
+ transformation_cache = FeatureTransformationCache({
+ 'price':
+ sparse_tensor.SparseTensor(
+ indices=[[0, 0]], values=[0.3], dense_shape=[1, 1])
+ })
+ with self.assertRaisesRegexp(ValueError, 'must be a Tensor'):
+ price.transform_feature(transformation_cache, None)
+
+ def test_deep_copy(self):
+ a = fc.numeric_column('aaa', shape=[1, 2], default_value=[[3., 2.]])
+ a_copy = copy.deepcopy(a)
+ self.assertEqual(a_copy.name, 'aaa')
+ self.assertEqual(a_copy.shape, (1, 2))
+ self.assertEqual(a_copy.default_value, ((3., 2.),))
+
+ def test_numpy_default_value(self):
+ a = fc.numeric_column(
+ 'aaa', shape=[1, 2], default_value=np.array([[3., 2.]]))
+ self.assertEqual(a.default_value, ((3., 2.),))
+
+ def test_linear_model(self):
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default():
+ features = {'price': [[1.], [5.]]}
+ predictions = fc.linear_model(features, [price])
+ bias = get_linear_model_bias()
+ price_var = get_linear_model_column_var(price)
+ with _initialized_session() as sess:
+ self.assertAllClose([0.], bias.eval())
+ self.assertAllClose([[0.]], price_var.eval())
+ self.assertAllClose([[0.], [0.]], predictions.eval())
+ sess.run(price_var.assign([[10.]]))
+ self.assertAllClose([[10.], [50.]], predictions.eval())
+
+ def test_keras_linear_model(self):
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default():
+ features = {'price': [[1.], [5.]]}
+ predictions = get_keras_linear_model_predictions(features, [price])
+ bias = get_linear_model_bias()
+ price_var = get_linear_model_column_var(price)
+ with _initialized_session() as sess:
+ self.assertAllClose([0.], bias.eval())
+ self.assertAllClose([[0.]], price_var.eval())
+ self.assertAllClose([[0.], [0.]], predictions.eval())
+ sess.run(price_var.assign([[10.]]))
+ self.assertAllClose([[10.], [50.]], predictions.eval())
+
+
+class BucketizedColumnTest(test.TestCase):
+
+ def test_invalid_source_column_type(self):
+ a = fc.categorical_column_with_hash_bucket('aaa', hash_bucket_size=10)
+ with self.assertRaisesRegexp(
+ ValueError,
+ 'source_column must be a column generated with numeric_column'):
+ fc.bucketized_column(a, boundaries=[0, 1])
+
+ def test_invalid_source_column_shape(self):
+ a = fc.numeric_column('aaa', shape=[2, 3])
+ with self.assertRaisesRegexp(
+ ValueError, 'source_column must be one-dimensional column'):
+ fc.bucketized_column(a, boundaries=[0, 1])
+
+ def test_invalid_boundaries(self):
+ a = fc.numeric_column('aaa')
+ with self.assertRaisesRegexp(
+ ValueError, 'boundaries must be a sorted list'):
+ fc.bucketized_column(a, boundaries=None)
+ with self.assertRaisesRegexp(
+ ValueError, 'boundaries must be a sorted list'):
+ fc.bucketized_column(a, boundaries=1.)
+ with self.assertRaisesRegexp(
+ ValueError, 'boundaries must be a sorted list'):
+ fc.bucketized_column(a, boundaries=[1, 0])
+ with self.assertRaisesRegexp(
+ ValueError, 'boundaries must be a sorted list'):
+ fc.bucketized_column(a, boundaries=[1, 1])
+
+ def test_name(self):
+ a = fc.numeric_column('aaa', dtype=dtypes.int32)
+ b = fc.bucketized_column(a, boundaries=[0, 1])
+ self.assertEqual('aaa_bucketized', b.name)
+
+ def test_parse_spec(self):
+ a = fc.numeric_column('aaa', shape=[2], dtype=dtypes.int32)
+ b = fc.bucketized_column(a, boundaries=[0, 1])
+ self.assertEqual({
+ 'aaa': parsing_ops.FixedLenFeature((2,), dtype=dtypes.int32)
+ }, b.parse_example_spec)
+
+ def test_variable_shape(self):
+ a = fc.numeric_column('aaa', shape=[2], dtype=dtypes.int32)
+ b = fc.bucketized_column(a, boundaries=[0, 1])
+ # Column 'aaa` has shape [2] times three buckets -> variable_shape=[2, 3].
+ self.assertAllEqual((2, 3), b.variable_shape)
+
+ def test_num_buckets(self):
+ a = fc.numeric_column('aaa', shape=[2], dtype=dtypes.int32)
+ b = fc.bucketized_column(a, boundaries=[0, 1])
+ # Column 'aaa` has shape [2] times three buckets -> num_buckets=6.
+ self.assertEqual(6, b.num_buckets)
+
+ def test_parse_example(self):
+ price = fc.numeric_column('price', shape=[2])
+ bucketized_price = fc.bucketized_column(price, boundaries=[0, 50])
+ data = example_pb2.Example(features=feature_pb2.Features(
+ feature={
+ 'price':
+ feature_pb2.Feature(float_list=feature_pb2.FloatList(
+ value=[20., 110.]))
+ }))
+ features = parsing_ops.parse_example(
+ serialized=[data.SerializeToString()],
+ features=fc.make_parse_example_spec([bucketized_price]))
+ self.assertIn('price', features)
+ with self.test_session():
+ self.assertAllEqual([[20., 110.]], features['price'].eval())
+
+ def test_transform_feature(self):
+ price = fc.numeric_column('price', shape=[2])
+ bucketized_price = fc.bucketized_column(price, boundaries=[0, 2, 4, 6])
+ with ops.Graph().as_default():
+ transformed_tensor = _transform_features({
+ 'price': [[-1., 1.], [5., 6.]]
+ }, [bucketized_price], None)
+ with _initialized_session():
+ self.assertAllEqual([[0, 1], [3, 4]],
+ transformed_tensor[bucketized_price].eval())
+
+ def test_get_dense_tensor_one_input_value(self):
+ """Tests _get_dense_tensor() for input with shape=[1]."""
+ price = fc.numeric_column('price', shape=[1])
+ bucketized_price = fc.bucketized_column(price, boundaries=[0, 2, 4, 6])
+ with ops.Graph().as_default():
+ transformation_cache = FeatureTransformationCache({
+ 'price': [[-1.], [1.], [5.], [6.]]
+ })
+ with _initialized_session():
+ bucketized_price_tensor = bucketized_price.get_dense_tensor(
+ transformation_cache, None)
+ self.assertAllClose(
+ # One-hot tensor.
+ [[[1., 0., 0., 0., 0.]],
+ [[0., 1., 0., 0., 0.]],
+ [[0., 0., 0., 1., 0.]],
+ [[0., 0., 0., 0., 1.]]],
+ bucketized_price_tensor.eval())
+
+ def test_get_dense_tensor_two_input_values(self):
+ """Tests _get_dense_tensor() for input with shape=[2]."""
+ price = fc.numeric_column('price', shape=[2])
+ bucketized_price = fc.bucketized_column(price, boundaries=[0, 2, 4, 6])
+ with ops.Graph().as_default():
+ transformation_cache = FeatureTransformationCache({
+ 'price': [[-1., 1.], [5., 6.]]
+ })
+ with _initialized_session():
+ bucketized_price_tensor = bucketized_price.get_dense_tensor(
+ transformation_cache, None)
+ self.assertAllClose(
+ # One-hot tensor.
+ [[[1., 0., 0., 0., 0.], [0., 1., 0., 0., 0.]],
+ [[0., 0., 0., 1., 0.], [0., 0., 0., 0., 1.]]],
+ bucketized_price_tensor.eval())
+
+ def test_get_sparse_tensors_one_input_value(self):
+ """Tests _get_sparse_tensors() for input with shape=[1]."""
+ price = fc.numeric_column('price', shape=[1])
+ bucketized_price = fc.bucketized_column(price, boundaries=[0, 2, 4, 6])
+ with ops.Graph().as_default():
+ transformation_cache = FeatureTransformationCache({
+ 'price': [[-1.], [1.], [5.], [6.]]
+ })
+ with _initialized_session() as sess:
+ id_weight_pair = bucketized_price.get_sparse_tensors(
+ transformation_cache, None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ id_tensor_value = sess.run(id_weight_pair.id_tensor)
+ self.assertAllEqual(
+ [[0, 0], [1, 0], [2, 0], [3, 0]], id_tensor_value.indices)
+ self.assertAllEqual([0, 1, 3, 4], id_tensor_value.values)
+ self.assertAllEqual([4, 1], id_tensor_value.dense_shape)
+
+ def test_get_sparse_tensors_two_input_values(self):
+ """Tests _get_sparse_tensors() for input with shape=[2]."""
+ price = fc.numeric_column('price', shape=[2])
+ bucketized_price = fc.bucketized_column(price, boundaries=[0, 2, 4, 6])
+ with ops.Graph().as_default():
+ transformation_cache = FeatureTransformationCache({
+ 'price': [[-1., 1.], [5., 6.]]
+ })
+ with _initialized_session() as sess:
+ id_weight_pair = bucketized_price.get_sparse_tensors(
+ transformation_cache, None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ id_tensor_value = sess.run(id_weight_pair.id_tensor)
+ self.assertAllEqual(
+ [[0, 0], [0, 1], [1, 0], [1, 1]], id_tensor_value.indices)
+ # Values 0-4 correspond to the first column of the input price.
+ # Values 5-9 correspond to the second column of the input price.
+ self.assertAllEqual([0, 6, 3, 9], id_tensor_value.values)
+ self.assertAllEqual([2, 2], id_tensor_value.dense_shape)
+
+ def test_sparse_tensor_input_not_supported(self):
+ price = fc.numeric_column('price')
+ bucketized_price = fc.bucketized_column(price, boundaries=[0, 1])
+ transformation_cache = FeatureTransformationCache({
+ 'price':
+ sparse_tensor.SparseTensor(
+ indices=[[0, 0]], values=[0.3], dense_shape=[1, 1])
+ })
+ with self.assertRaisesRegexp(ValueError, 'must be a Tensor'):
+ bucketized_price.transform_feature(transformation_cache, None)
+
+ def test_deep_copy(self):
+ a = fc.numeric_column('aaa', shape=[2])
+ a_bucketized = fc.bucketized_column(a, boundaries=[0, 1])
+ a_bucketized_copy = copy.deepcopy(a_bucketized)
+ self.assertEqual(a_bucketized_copy.name, 'aaa_bucketized')
+ self.assertAllEqual(a_bucketized_copy.variable_shape, (2, 3))
+ self.assertEqual(a_bucketized_copy.boundaries, (0, 1))
+
+ def test_linear_model_one_input_value(self):
+ """Tests linear_model() for input with shape=[1]."""
+ price = fc_old.numeric_column('price', shape=[1])
+ bucketized_price = fc_old.bucketized_column(price, boundaries=[0, 2, 4, 6])
+ with ops.Graph().as_default():
+ features = {'price': [[-1.], [1.], [5.], [6.]]}
+ predictions = fc.linear_model(features, [bucketized_price])
+ bias = get_linear_model_bias()
+ bucketized_price_var = get_linear_model_column_var(bucketized_price)
+ with _initialized_session() as sess:
+ self.assertAllClose([0.], bias.eval())
+ # One weight variable per bucket, all initialized to zero.
+ self.assertAllClose(
+ [[0.], [0.], [0.], [0.], [0.]], bucketized_price_var.eval())
+ self.assertAllClose([[0.], [0.], [0.], [0.]], predictions.eval())
+ sess.run(bucketized_price_var.assign(
+ [[10.], [20.], [30.], [40.], [50.]]))
+ # price -1. is in the 0th bucket, whose weight is 10.
+ # price 1. is in the 1st bucket, whose weight is 20.
+ # price 5. is in the 3rd bucket, whose weight is 40.
+ # price 6. is in the 4th bucket, whose weight is 50.
+ self.assertAllClose([[10.], [20.], [40.], [50.]], predictions.eval())
+ sess.run(bias.assign([1.]))
+ self.assertAllClose([[11.], [21.], [41.], [51.]], predictions.eval())
+
+ def test_linear_model_two_input_values(self):
+ """Tests linear_model() for input with shape=[2]."""
+ price = fc_old.numeric_column('price', shape=[2])
+ bucketized_price = fc_old.bucketized_column(price, boundaries=[0, 2, 4, 6])
+ with ops.Graph().as_default():
+ features = {'price': [[-1., 1.], [5., 6.]]}
+ predictions = fc.linear_model(features, [bucketized_price])
+ bias = get_linear_model_bias()
+ bucketized_price_var = get_linear_model_column_var(bucketized_price)
+ with _initialized_session() as sess:
+ self.assertAllClose([0.], bias.eval())
+ # One weight per bucket per input column, all initialized to zero.
+ self.assertAllClose(
+ [[0.], [0.], [0.], [0.], [0.], [0.], [0.], [0.], [0.], [0.]],
+ bucketized_price_var.eval())
+ self.assertAllClose([[0.], [0.]], predictions.eval())
+ sess.run(bucketized_price_var.assign(
+ [[10.], [20.], [30.], [40.], [50.],
+ [60.], [70.], [80.], [90.], [100.]]))
+ # 1st example:
+ # price -1. is in the 0th bucket, whose weight is 10.
+ # price 1. is in the 6th bucket, whose weight is 70.
+ # 2nd example:
+ # price 5. is in the 3rd bucket, whose weight is 40.
+ # price 6. is in the 9th bucket, whose weight is 100.
+ self.assertAllClose([[80.], [140.]], predictions.eval())
+ sess.run(bias.assign([1.]))
+ self.assertAllClose([[81.], [141.]], predictions.eval())
+
+ def test_keras_linear_model_one_input_value(self):
+ """Tests _LinearModel for input with shape=[1]."""
+ price = fc_old.numeric_column('price', shape=[1])
+ bucketized_price = fc_old.bucketized_column(price, boundaries=[0, 2, 4, 6])
+ with ops.Graph().as_default():
+ features = {'price': [[-1.], [1.], [5.], [6.]]}
+ predictions = get_keras_linear_model_predictions(features,
+ [bucketized_price])
+ bias = get_linear_model_bias()
+ bucketized_price_var = get_linear_model_column_var(bucketized_price)
+ with _initialized_session() as sess:
+ self.assertAllClose([0.], bias.eval())
+ # One weight variable per bucket, all initialized to zero.
+ self.assertAllClose([[0.], [0.], [0.], [0.], [0.]],
+ bucketized_price_var.eval())
+ self.assertAllClose([[0.], [0.], [0.], [0.]], predictions.eval())
+ sess.run(
+ bucketized_price_var.assign([[10.], [20.], [30.], [40.], [50.]]))
+ # price -1. is in the 0th bucket, whose weight is 10.
+ # price 1. is in the 1st bucket, whose weight is 20.
+ # price 5. is in the 3rd bucket, whose weight is 40.
+ # price 6. is in the 4th bucket, whose weight is 50.
+ self.assertAllClose([[10.], [20.], [40.], [50.]], predictions.eval())
+ sess.run(bias.assign([1.]))
+ self.assertAllClose([[11.], [21.], [41.], [51.]], predictions.eval())
+
+ def test_keras_linear_model_two_input_values(self):
+ """Tests _LinearModel for input with shape=[2]."""
+ price = fc_old.numeric_column('price', shape=[2])
+ bucketized_price = fc_old.bucketized_column(price, boundaries=[0, 2, 4, 6])
+ with ops.Graph().as_default():
+ features = {'price': [[-1., 1.], [5., 6.]]}
+ predictions = get_keras_linear_model_predictions(features,
+ [bucketized_price])
+ bias = get_linear_model_bias()
+ bucketized_price_var = get_linear_model_column_var(bucketized_price)
+ with _initialized_session() as sess:
+ self.assertAllClose([0.], bias.eval())
+ # One weight per bucket per input column, all initialized to zero.
+ self.assertAllClose(
+ [[0.], [0.], [0.], [0.], [0.], [0.], [0.], [0.], [0.], [0.]],
+ bucketized_price_var.eval())
+ self.assertAllClose([[0.], [0.]], predictions.eval())
+ sess.run(
+ bucketized_price_var.assign([[10.], [20.], [30.], [40.], [50.],
+ [60.], [70.], [80.], [90.], [100.]]))
+ # 1st example:
+ # price -1. is in the 0th bucket, whose weight is 10.
+ # price 1. is in the 6th bucket, whose weight is 70.
+ # 2nd example:
+ # price 5. is in the 3rd bucket, whose weight is 40.
+ # price 6. is in the 9th bucket, whose weight is 100.
+ self.assertAllClose([[80.], [140.]], predictions.eval())
+ sess.run(bias.assign([1.]))
+ self.assertAllClose([[81.], [141.]], predictions.eval())
+
+
+class HashedCategoricalColumnTest(test.TestCase):
+
+ def test_defaults(self):
+ a = fc.categorical_column_with_hash_bucket('aaa', 10)
+ self.assertEqual('aaa', a.name)
+ self.assertEqual('aaa', a.key)
+ self.assertEqual(10, a.hash_bucket_size)
+ self.assertEqual(dtypes.string, a.dtype)
+
+ def test_key_should_be_string(self):
+ with self.assertRaisesRegexp(ValueError, 'key must be a string.'):
+ fc.categorical_column_with_hash_bucket(('key',), 10)
+
+ def test_bucket_size_should_be_given(self):
+ with self.assertRaisesRegexp(ValueError, 'hash_bucket_size must be set.'):
+ fc.categorical_column_with_hash_bucket('aaa', None)
+
+ def test_bucket_size_should_be_positive(self):
+ with self.assertRaisesRegexp(ValueError,
+ 'hash_bucket_size must be at least 1'):
+ fc.categorical_column_with_hash_bucket('aaa', 0)
+
+ def test_dtype_should_be_string_or_integer(self):
+ fc.categorical_column_with_hash_bucket('aaa', 10, dtype=dtypes.string)
+ fc.categorical_column_with_hash_bucket('aaa', 10, dtype=dtypes.int32)
+ with self.assertRaisesRegexp(ValueError, 'dtype must be string or integer'):
+ fc.categorical_column_with_hash_bucket('aaa', 10, dtype=dtypes.float32)
+
+ def test_deep_copy(self):
+ original = fc.categorical_column_with_hash_bucket('aaa', 10)
+ for column in (original, copy.deepcopy(original)):
+ self.assertEqual('aaa', column.name)
+ self.assertEqual(10, column.hash_bucket_size)
+ self.assertEqual(10, column.num_buckets)
+ self.assertEqual(dtypes.string, column.dtype)
+
+ def test_parse_spec_string(self):
+ a = fc.categorical_column_with_hash_bucket('aaa', 10)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.string)
+ }, a.parse_example_spec)
+
+ def test_parse_spec_int(self):
+ a = fc.categorical_column_with_hash_bucket('aaa', 10, dtype=dtypes.int32)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int32)
+ }, a.parse_example_spec)
+
+ def test_parse_example(self):
+ a = fc.categorical_column_with_hash_bucket('aaa', 10)
+ data = example_pb2.Example(features=feature_pb2.Features(
+ feature={
+ 'aaa':
+ feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
+ value=[b'omar', b'stringer']))
+ }))
+ features = parsing_ops.parse_example(
+ serialized=[data.SerializeToString()],
+ features=fc.make_parse_example_spec([a]))
+ self.assertIn('aaa', features)
+ with self.test_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=[[0, 0], [0, 1]],
+ values=np.array([b'omar', b'stringer'], dtype=np.object_),
+ dense_shape=[1, 2]),
+ features['aaa'].eval())
+
+ def test_strings_should_be_hashed(self):
+ hashed_sparse = fc.categorical_column_with_hash_bucket('wire', 10)
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'],
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ outputs = _transform_features({'wire': wire_tensor}, [hashed_sparse], None)
+ output = outputs[hashed_sparse]
+ # Check exact hashed output. If hashing changes this test will break.
+ expected_values = [6, 4, 1]
+ with self.test_session():
+ self.assertEqual(dtypes.int64, output.values.dtype)
+ self.assertAllEqual(expected_values, output.values.eval())
+ self.assertAllEqual(wire_tensor.indices.eval(), output.indices.eval())
+ self.assertAllEqual(wire_tensor.dense_shape.eval(),
+ output.dense_shape.eval())
+
+ def test_tensor_dtype_should_be_string_or_integer(self):
+ string_fc = fc.categorical_column_with_hash_bucket(
+ 'a_string', 10, dtype=dtypes.string)
+ int_fc = fc.categorical_column_with_hash_bucket(
+ 'a_int', 10, dtype=dtypes.int32)
+ float_fc = fc.categorical_column_with_hash_bucket(
+ 'a_float', 10, dtype=dtypes.string)
+ int_tensor = sparse_tensor.SparseTensor(
+ values=[101],
+ indices=[[0, 0]],
+ dense_shape=[1, 1])
+ string_tensor = sparse_tensor.SparseTensor(
+ values=['101'],
+ indices=[[0, 0]],
+ dense_shape=[1, 1])
+ float_tensor = sparse_tensor.SparseTensor(
+ values=[101.],
+ indices=[[0, 0]],
+ dense_shape=[1, 1])
+ transformation_cache = FeatureTransformationCache({
+ 'a_int': int_tensor,
+ 'a_string': string_tensor,
+ 'a_float': float_tensor
+ })
+ transformation_cache.get(string_fc, None)
+ transformation_cache.get(int_fc, None)
+ with self.assertRaisesRegexp(ValueError, 'dtype must be string or integer'):
+ transformation_cache.get(float_fc, None)
+
+ def test_dtype_should_match_with_tensor(self):
+ hashed_sparse = fc.categorical_column_with_hash_bucket(
+ 'wire', 10, dtype=dtypes.int64)
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar'], indices=[[0, 0]], dense_shape=[1, 1])
+ transformation_cache = FeatureTransformationCache({'wire': wire_tensor})
+ with self.assertRaisesRegexp(ValueError, 'dtype must be compatible'):
+ transformation_cache.get(hashed_sparse, None)
+
+ def test_ints_should_be_hashed(self):
+ hashed_sparse = fc.categorical_column_with_hash_bucket(
+ 'wire', 10, dtype=dtypes.int64)
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=[101, 201, 301],
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ transformation_cache = FeatureTransformationCache({'wire': wire_tensor})
+ output = transformation_cache.get(hashed_sparse, None)
+ # Check exact hashed output. If hashing changes this test will break.
+ expected_values = [3, 7, 5]
+ with self.test_session():
+ self.assertAllEqual(expected_values, output.values.eval())
+
+ def test_int32_64_is_compatible(self):
+ hashed_sparse = fc.categorical_column_with_hash_bucket(
+ 'wire', 10, dtype=dtypes.int64)
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=constant_op.constant([101, 201, 301], dtype=dtypes.int32),
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ transformation_cache = FeatureTransformationCache({'wire': wire_tensor})
+ output = transformation_cache.get(hashed_sparse, None)
+ # Check exact hashed output. If hashing changes this test will break.
+ expected_values = [3, 7, 5]
+ with self.test_session():
+ self.assertAllEqual(expected_values, output.values.eval())
+
+ def test_get_sparse_tensors(self):
+ hashed_sparse = fc.categorical_column_with_hash_bucket('wire', 10)
+ transformation_cache = FeatureTransformationCache({
+ 'wire':
+ sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'],
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ })
+ id_weight_pair = hashed_sparse.get_sparse_tensors(transformation_cache,
+ None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ self.assertEqual(
+ transformation_cache.get(hashed_sparse, None), id_weight_pair.id_tensor)
+
+ def DISABLED_test_get_sparse_tensors_weight_collections(self):
+ column = fc.categorical_column_with_hash_bucket('aaa', 10)
+ inputs = sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'],
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ column._get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }),
+ weight_collections=('my_weights',))
+
+ self.assertItemsEqual(
+ [], ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES))
+ self.assertItemsEqual([], ops.get_collection('my_weights'))
+
+ def test_get_sparse_tensors_dense_input(self):
+ hashed_sparse = fc.categorical_column_with_hash_bucket('wire', 10)
+ transformation_cache = FeatureTransformationCache({
+ 'wire': (('omar', ''), ('stringer', 'marlo'))
+ })
+ id_weight_pair = hashed_sparse.get_sparse_tensors(transformation_cache,
+ None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ self.assertEqual(
+ transformation_cache.get(hashed_sparse, None), id_weight_pair.id_tensor)
+
+ def test_linear_model(self):
+ wire_column = fc_old.categorical_column_with_hash_bucket('wire', 4)
+ self.assertEqual(4, wire_column._num_buckets)
+ with ops.Graph().as_default():
+ predictions = fc.linear_model({
+ wire_column.name: sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ }, (wire_column,))
+ bias = get_linear_model_bias()
+ wire_var = get_linear_model_column_var(wire_column)
+ with _initialized_session():
+ self.assertAllClose((0.,), bias.eval())
+ self.assertAllClose(((0.,), (0.,), (0.,), (0.,)), wire_var.eval())
+ self.assertAllClose(((0.,), (0.,)), predictions.eval())
+ wire_var.assign(((1.,), (2.,), (3.,), (4.,))).eval()
+ # 'marlo' -> 3: wire_var[3] = 4
+ # 'skywalker' -> 2, 'omar' -> 2: wire_var[2] + wire_var[2] = 3+3 = 6
+ self.assertAllClose(((4.,), (6.,)), predictions.eval())
+
+ def test_keras_linear_model(self):
+ wire_column = fc_old.categorical_column_with_hash_bucket('wire', 4)
+ self.assertEqual(4, wire_column._num_buckets)
+ with ops.Graph().as_default():
+ predictions = get_keras_linear_model_predictions({
+ wire_column.name:
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ }, (wire_column,))
+ bias = get_linear_model_bias()
+ wire_var = get_linear_model_column_var(wire_column)
+ with _initialized_session():
+ self.assertAllClose((0.,), bias.eval())
+ self.assertAllClose(((0.,), (0.,), (0.,), (0.,)), wire_var.eval())
+ self.assertAllClose(((0.,), (0.,)), predictions.eval())
+ wire_var.assign(((1.,), (2.,), (3.,), (4.,))).eval()
+ # 'marlo' -> 3: wire_var[3] = 4
+ # 'skywalker' -> 2, 'omar' -> 2: wire_var[2] + wire_var[2] = 3+3 = 6
+ self.assertAllClose(((4.,), (6.,)), predictions.eval())
+
+
+class CrossedColumnTest(test.TestCase):
+
+ def test_keys_empty(self):
+ with self.assertRaisesRegexp(
+ ValueError, 'keys must be a list with length > 1'):
+ fc.crossed_column([], 10)
+
+ def test_keys_length_one(self):
+ with self.assertRaisesRegexp(
+ ValueError, 'keys must be a list with length > 1'):
+ fc.crossed_column(['a'], 10)
+
+ def test_key_type_unsupported(self):
+ with self.assertRaisesRegexp(ValueError, 'Unsupported key type'):
+ fc.crossed_column(['a', fc.numeric_column('c')], 10)
+
+ with self.assertRaisesRegexp(
+ ValueError, 'categorical_column_with_hash_bucket is not supported'):
+ fc.crossed_column(
+ ['a', fc.categorical_column_with_hash_bucket('c', 10)], 10)
+
+ def test_hash_bucket_size_negative(self):
+ with self.assertRaisesRegexp(
+ ValueError, 'hash_bucket_size must be > 1'):
+ fc.crossed_column(['a', 'c'], -1)
+
+ def test_hash_bucket_size_zero(self):
+ with self.assertRaisesRegexp(
+ ValueError, 'hash_bucket_size must be > 1'):
+ fc.crossed_column(['a', 'c'], 0)
+
+ def test_hash_bucket_size_none(self):
+ with self.assertRaisesRegexp(
+ ValueError, 'hash_bucket_size must be > 1'):
+ fc.crossed_column(['a', 'c'], None)
+
+ def test_name(self):
+ a = fc.numeric_column('a', dtype=dtypes.int32)
+ b = fc.bucketized_column(a, boundaries=[0, 1])
+ crossed1 = fc.crossed_column(['d1', 'd2'], 10)
+
+ crossed2 = fc.crossed_column([b, 'c', crossed1], 10)
+ self.assertEqual('a_bucketized_X_c_X_d1_X_d2', crossed2.name)
+
+ def test_name_ordered_alphabetically(self):
+ """Tests that the name does not depend on the order of given columns."""
+ a = fc.numeric_column('a', dtype=dtypes.int32)
+ b = fc.bucketized_column(a, boundaries=[0, 1])
+ crossed1 = fc.crossed_column(['d1', 'd2'], 10)
+
+ crossed2 = fc.crossed_column([crossed1, 'c', b], 10)
+ self.assertEqual('a_bucketized_X_c_X_d1_X_d2', crossed2.name)
+
+ def test_name_leaf_keys_ordered_alphabetically(self):
+ """Tests that the name does not depend on the order of given columns."""
+ a = fc.numeric_column('a', dtype=dtypes.int32)
+ b = fc.bucketized_column(a, boundaries=[0, 1])
+ crossed1 = fc.crossed_column(['d2', 'c'], 10)
+
+ crossed2 = fc.crossed_column([crossed1, 'd1', b], 10)
+ self.assertEqual('a_bucketized_X_c_X_d1_X_d2', crossed2.name)
+
+ def test_parse_spec(self):
+ a = fc.numeric_column('a', shape=[2], dtype=dtypes.int32)
+ b = fc.bucketized_column(a, boundaries=[0, 1])
+ crossed = fc.crossed_column([b, 'c'], 10)
+ self.assertEqual({
+ 'a': parsing_ops.FixedLenFeature((2,), dtype=dtypes.int32),
+ 'c': parsing_ops.VarLenFeature(dtypes.string),
+ }, crossed.parse_example_spec)
+
+ def test_num_buckets(self):
+ a = fc.numeric_column('a', shape=[2], dtype=dtypes.int32)
+ b = fc.bucketized_column(a, boundaries=[0, 1])
+ crossed = fc.crossed_column([b, 'c'], 15)
+ self.assertEqual(15, crossed.num_buckets)
+
+ def test_deep_copy(self):
+ a = fc.numeric_column('a', dtype=dtypes.int32)
+ b = fc.bucketized_column(a, boundaries=[0, 1])
+ crossed1 = fc.crossed_column(['d1', 'd2'], 10)
+ crossed2 = fc.crossed_column([b, 'c', crossed1], 15, hash_key=5)
+ crossed2_copy = copy.deepcopy(crossed2)
+ self.assertEqual('a_bucketized_X_c_X_d1_X_d2', crossed2_copy.name,)
+ self.assertEqual(15, crossed2_copy.hash_bucket_size)
+ self.assertEqual(5, crossed2_copy.hash_key)
+
+ def test_parse_example(self):
+ price = fc.numeric_column('price', shape=[2])
+ bucketized_price = fc.bucketized_column(price, boundaries=[0, 50])
+ price_cross_wire = fc.crossed_column([bucketized_price, 'wire'], 10)
+ data = example_pb2.Example(features=feature_pb2.Features(
+ feature={
+ 'price':
+ feature_pb2.Feature(float_list=feature_pb2.FloatList(
+ value=[20., 110.])),
+ 'wire':
+ feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
+ value=[b'omar', b'stringer'])),
+ }))
+ features = parsing_ops.parse_example(
+ serialized=[data.SerializeToString()],
+ features=fc.make_parse_example_spec([price_cross_wire]))
+ self.assertIn('price', features)
+ self.assertIn('wire', features)
+ with self.test_session():
+ self.assertAllEqual([[20., 110.]], features['price'].eval())
+ wire_sparse = features['wire']
+ self.assertAllEqual([[0, 0], [0, 1]], wire_sparse.indices.eval())
+ # Use byte constants to pass the open-source test.
+ self.assertAllEqual([b'omar', b'stringer'], wire_sparse.values.eval())
+ self.assertAllEqual([1, 2], wire_sparse.dense_shape.eval())
+
+ def test_transform_feature(self):
+ price = fc.numeric_column('price', shape=[2])
+ bucketized_price = fc.bucketized_column(price, boundaries=[0, 50])
+ hash_bucket_size = 10
+ price_cross_wire = fc.crossed_column(
+ [bucketized_price, 'wire'], hash_bucket_size)
+ features = {
+ 'price': constant_op.constant([[1., 2.], [5., 6.]]),
+ 'wire': sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'],
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2]),
+ }
+ outputs = _transform_features(features, [price_cross_wire], None)
+ output = outputs[price_cross_wire]
+ with self.test_session() as sess:
+ output_val = sess.run(output)
+ self.assertAllEqual(
+ [[0, 0], [0, 1], [1, 0], [1, 1], [1, 2], [1, 3]], output_val.indices)
+ for val in output_val.values:
+ self.assertIn(val, list(range(hash_bucket_size)))
+ self.assertAllEqual([2, 4], output_val.dense_shape)
+
+ def test_get_sparse_tensors(self):
+ a = fc.numeric_column('a', dtype=dtypes.int32, shape=(2,))
+ b = fc.bucketized_column(a, boundaries=(0, 1))
+ crossed1 = fc.crossed_column(['d1', 'd2'], 10)
+ crossed2 = fc.crossed_column([b, 'c', crossed1], 15, hash_key=5)
+ with ops.Graph().as_default():
+ transformation_cache = FeatureTransformationCache({
+ 'a':
+ constant_op.constant(((-1., .5), (.5, 1.))),
+ 'c':
+ sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=['cA', 'cB', 'cC'],
+ dense_shape=(2, 2)),
+ 'd1':
+ sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=['d1A', 'd1B', 'd1C'],
+ dense_shape=(2, 2)),
+ 'd2':
+ sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=['d2A', 'd2B', 'd2C'],
+ dense_shape=(2, 2)),
+ })
+ id_weight_pair = crossed2.get_sparse_tensors(transformation_cache, None)
+ with _initialized_session():
+ id_tensor_eval = id_weight_pair.id_tensor.eval()
+ self.assertAllEqual(
+ ((0, 0), (0, 1), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5),
+ (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (1, 12), (1, 13),
+ (1, 14), (1, 15)),
+ id_tensor_eval.indices)
+ # Check exact hashed output. If hashing changes this test will break.
+ # All values are within [0, hash_bucket_size).
+ expected_values = (
+ 6, 14, 0, 13, 8, 8, 10, 12, 2, 0, 1, 9, 8, 12, 2, 0, 10, 11)
+ self.assertAllEqual(expected_values, id_tensor_eval.values)
+ self.assertAllEqual((2, 16), id_tensor_eval.dense_shape)
+
+ def test_get_sparse_tensors_simple(self):
+ """Same as test_get_sparse_tensors, but with simpler values."""
+ a = fc.numeric_column('a', dtype=dtypes.int32, shape=(2,))
+ b = fc.bucketized_column(a, boundaries=(0, 1))
+ crossed = fc.crossed_column([b, 'c'], hash_bucket_size=5, hash_key=5)
+ with ops.Graph().as_default():
+ transformation_cache = FeatureTransformationCache({
+ 'a':
+ constant_op.constant(((-1., .5), (.5, 1.))),
+ 'c':
+ sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=['cA', 'cB', 'cC'],
+ dense_shape=(2, 2)),
+ })
+ id_weight_pair = crossed.get_sparse_tensors(transformation_cache, None)
+ with _initialized_session():
+ id_tensor_eval = id_weight_pair.id_tensor.eval()
+ self.assertAllEqual(
+ ((0, 0), (0, 1), (1, 0), (1, 1), (1, 2), (1, 3)),
+ id_tensor_eval.indices)
+ # Check exact hashed output. If hashing changes this test will break.
+ # All values are within [0, hash_bucket_size).
+ expected_values = (1, 0, 1, 3, 4, 2)
+ self.assertAllEqual(expected_values, id_tensor_eval.values)
+ self.assertAllEqual((2, 4), id_tensor_eval.dense_shape)
+
+ def test_linear_model(self):
+ """Tests linear_model.
+
+ Uses data from test_get_sparse_tesnsors_simple.
+ """
+ a = fc_old.numeric_column('a', dtype=dtypes.int32, shape=(2,))
+ b = fc_old.bucketized_column(a, boundaries=(0, 1))
+ crossed = fc_old.crossed_column([b, 'c'], hash_bucket_size=5, hash_key=5)
+ with ops.Graph().as_default():
+ predictions = fc.linear_model({
+ 'a': constant_op.constant(((-1., .5), (.5, 1.))),
+ 'c': sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=['cA', 'cB', 'cC'],
+ dense_shape=(2, 2)),
+ }, (crossed,))
+ bias = get_linear_model_bias()
+ crossed_var = get_linear_model_column_var(crossed)
+ with _initialized_session() as sess:
+ self.assertAllClose((0.,), bias.eval())
+ self.assertAllClose(
+ ((0.,), (0.,), (0.,), (0.,), (0.,)), crossed_var.eval())
+ self.assertAllClose(((0.,), (0.,)), predictions.eval())
+ sess.run(crossed_var.assign(((1.,), (2.,), (3.,), (4.,), (5.,))))
+ # Expected ids after cross = (1, 0, 1, 3, 4, 2)
+ self.assertAllClose(((3.,), (14.,)), predictions.eval())
+ sess.run(bias.assign((.1,)))
+ self.assertAllClose(((3.1,), (14.1,)), predictions.eval())
+
+ def test_linear_model_with_weights(self):
+
+ class _TestColumnWithWeights(fc_old._CategoricalColumn):
+ """Produces sparse IDs and sparse weights."""
+
+ @property
+ def name(self):
+ return 'test_column'
+
+ @property
+ def _parse_example_spec(self):
+ return {
+ self.name: parsing_ops.VarLenFeature(dtypes.int32),
+ '{}_weights'.format(self.name): parsing_ops.VarLenFeature(
+ dtypes.float32),
+ }
+
+ @property
+ def _num_buckets(self):
+ return 5
+
+ def _transform_feature(self, inputs):
+ return (inputs.get(self.name),
+ inputs.get('{}_weights'.format(self.name)))
+
+ def _get_sparse_tensors(self, inputs, weight_collections=None,
+ trainable=None):
+ """Populates both id_tensor and weight_tensor."""
+ ids_and_weights = inputs.get(self)
+ return fc_old._CategoricalColumn.IdWeightPair(
+ id_tensor=ids_and_weights[0], weight_tensor=ids_and_weights[1])
+
+ t = _TestColumnWithWeights()
+ crossed = fc_old.crossed_column([t, 'c'], hash_bucket_size=5, hash_key=5)
+ with ops.Graph().as_default():
+ with self.assertRaisesRegexp(
+ ValueError,
+ 'crossed_column does not support weight_tensor.*{}'.format(t.name)):
+ fc.linear_model({
+ t.name: sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=[0, 1, 2],
+ dense_shape=(2, 2)),
+ '{}_weights'.format(t.name): sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=[1., 10., 2.],
+ dense_shape=(2, 2)),
+ 'c': sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=['cA', 'cB', 'cC'],
+ dense_shape=(2, 2)),
+ }, (crossed,))
+
+ def test_keras_linear_model(self):
+ """Tests _LinearModel.
+
+ Uses data from test_get_sparse_tesnsors_simple.
+ """
+ a = fc_old.numeric_column('a', dtype=dtypes.int32, shape=(2,))
+ b = fc_old.bucketized_column(a, boundaries=(0, 1))
+ crossed = fc_old.crossed_column([b, 'c'], hash_bucket_size=5, hash_key=5)
+ with ops.Graph().as_default():
+ predictions = get_keras_linear_model_predictions({
+ 'a':
+ constant_op.constant(((-1., .5), (.5, 1.))),
+ 'c':
+ sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=['cA', 'cB', 'cC'],
+ dense_shape=(2, 2)),
+ }, (crossed,))
+ bias = get_linear_model_bias()
+ crossed_var = get_linear_model_column_var(crossed)
+ with _initialized_session() as sess:
+ self.assertAllClose((0.,), bias.eval())
+ self.assertAllClose(((0.,), (0.,), (0.,), (0.,), (0.,)),
+ crossed_var.eval())
+ self.assertAllClose(((0.,), (0.,)), predictions.eval())
+ sess.run(crossed_var.assign(((1.,), (2.,), (3.,), (4.,), (5.,))))
+ # Expected ids after cross = (1, 0, 1, 3, 4, 2)
+ self.assertAllClose(((3.,), (14.,)), predictions.eval())
+ sess.run(bias.assign((.1,)))
+ self.assertAllClose(((3.1,), (14.1,)), predictions.eval())
+
+ def test_keras_linear_model_with_weights(self):
+
+ class _TestColumnWithWeights(fc_old._CategoricalColumn):
+ """Produces sparse IDs and sparse weights."""
+
+ @property
+ def name(self):
+ return 'test_column'
+
+ @property
+ def _parse_example_spec(self):
+ return {
+ self.name:
+ parsing_ops.VarLenFeature(dtypes.int32),
+ '{}_weights'.format(self.name):
+ parsing_ops.VarLenFeature(dtypes.float32),
+ }
+
+ @property
+ def _num_buckets(self):
+ return 5
+
+ def _transform_feature(self, inputs):
+ return (inputs.get(self.name),
+ inputs.get('{}_weights'.format(self.name)))
+
+ def _get_sparse_tensors(self,
+ inputs,
+ weight_collections=None,
+ trainable=None):
+ """Populates both id_tensor and weight_tensor."""
+ ids_and_weights = inputs.get(self)
+ return fc_old._CategoricalColumn.IdWeightPair(
+ id_tensor=ids_and_weights[0], weight_tensor=ids_and_weights[1])
+
+ t = _TestColumnWithWeights()
+ crossed = fc_old.crossed_column([t, 'c'], hash_bucket_size=5, hash_key=5)
+ with ops.Graph().as_default():
+ with self.assertRaisesRegexp(
+ ValueError,
+ 'crossed_column does not support weight_tensor.*{}'.format(t.name)):
+ get_keras_linear_model_predictions({
+ t.name:
+ sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=[0, 1, 2],
+ dense_shape=(2, 2)),
+ '{}_weights'.format(t.name):
+ sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=[1., 10., 2.],
+ dense_shape=(2, 2)),
+ 'c':
+ sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=['cA', 'cB', 'cC'],
+ dense_shape=(2, 2)),
+ }, (crossed,))
+
+
+def get_linear_model_bias(name='linear_model'):
+ with variable_scope.variable_scope(name, reuse=True):
+ return variable_scope.get_variable('bias_weights')
+
+
+def get_linear_model_column_var(column, name='linear_model'):
+ return ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES,
+ name + '/' + column.name)[0]
+
+
+def get_keras_linear_model_predictions(features,
+ feature_columns,
+ units=1,
+ sparse_combiner='sum',
+ weight_collections=None,
+ trainable=True,
+ cols_to_vars=None):
+ keras_linear_model = _LinearModel(
+ feature_columns,
+ units,
+ sparse_combiner,
+ weight_collections,
+ trainable,
+ name='linear_model')
+ retval = keras_linear_model(features) # pylint: disable=not-callable
+ if cols_to_vars is not None:
+ cols_to_vars.update(keras_linear_model.cols_to_vars())
+ return retval
+
+
+class LinearModelTest(test.TestCase):
+
+ def test_raises_if_empty_feature_columns(self):
+ with self.assertRaisesRegexp(ValueError,
+ 'feature_columns must not be empty'):
+ fc.linear_model(features={}, feature_columns=[])
+
+ def test_should_be_feature_column(self):
+ with self.assertRaisesRegexp(ValueError, 'must be a _FeatureColumn'):
+ fc.linear_model(features={'a': [[0]]}, feature_columns='NotSupported')
+
+ def test_should_be_dense_or_categorical_column(self):
+
+ class NotSupportedColumn(fc_old._FeatureColumn):
+
+ @property
+ def name(self):
+ return 'NotSupportedColumn'
+
+ def _transform_feature(self, cache):
+ pass
+
+ @property
+ def _parse_example_spec(self):
+ pass
+
+ with self.assertRaisesRegexp(
+ ValueError, 'must be either a _DenseColumn or _CategoricalColumn'):
+ fc.linear_model(
+ features={'a': [[0]]}, feature_columns=[NotSupportedColumn()])
+
+ def test_does_not_support_dict_columns(self):
+ with self.assertRaisesRegexp(
+ ValueError, 'Expected feature_columns to be iterable, found dict.'):
+ fc.linear_model(
+ features={'a': [[0]]},
+ feature_columns={'a': fc_old.numeric_column('a')})
+
+ def test_raises_if_duplicate_name(self):
+ with self.assertRaisesRegexp(
+ ValueError, 'Duplicate feature column name found for columns'):
+ fc.linear_model(
+ features={'a': [[0]]},
+ feature_columns=[
+ fc_old.numeric_column('a'),
+ fc_old.numeric_column('a')
+ ])
+
+ def test_dense_bias(self):
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default():
+ features = {'price': [[1.], [5.]]}
+ predictions = fc.linear_model(features, [price])
+ bias = get_linear_model_bias()
+ price_var = get_linear_model_column_var(price)
+ with _initialized_session() as sess:
+ self.assertAllClose([0.], bias.eval())
+ sess.run(price_var.assign([[10.]]))
+ sess.run(bias.assign([5.]))
+ self.assertAllClose([[15.], [55.]], predictions.eval())
+
+ def test_sparse_bias(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default():
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'], # hashed to = [2, 0, 3]
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ features = {'wire_cast': wire_tensor}
+ predictions = fc.linear_model(features, [wire_cast])
+ bias = get_linear_model_bias()
+ wire_cast_var = get_linear_model_column_var(wire_cast)
+ with _initialized_session() as sess:
+ self.assertAllClose([0.], bias.eval())
+ self.assertAllClose([[0.], [0.], [0.], [0.]], wire_cast_var.eval())
+ sess.run(wire_cast_var.assign([[10.], [100.], [1000.], [10000.]]))
+ sess.run(bias.assign([5.]))
+ self.assertAllClose([[1005.], [10015.]], predictions.eval())
+
+ def test_dense_and_sparse_bias(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default():
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'], # hashed to = [2, 0, 3]
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ features = {'wire_cast': wire_tensor, 'price': [[1.], [5.]]}
+ predictions = fc.linear_model(features, [wire_cast, price])
+ bias = get_linear_model_bias()
+ wire_cast_var = get_linear_model_column_var(wire_cast)
+ price_var = get_linear_model_column_var(price)
+ with _initialized_session() as sess:
+ sess.run(wire_cast_var.assign([[10.], [100.], [1000.], [10000.]]))
+ sess.run(bias.assign([5.]))
+ sess.run(price_var.assign([[10.]]))
+ self.assertAllClose([[1015.], [10065.]], predictions.eval())
+
+ def test_dense_and_sparse_column(self):
+ """When the column is both dense and sparse, uses sparse tensors."""
+
+ class _DenseAndSparseColumn(fc_old._DenseColumn, fc_old._CategoricalColumn):
+
+ @property
+ def name(self):
+ return 'dense_and_sparse_column'
+
+ @property
+ def _parse_example_spec(self):
+ return {self.name: parsing_ops.VarLenFeature(self.dtype)}
+
+ def _transform_feature(self, inputs):
+ return inputs.get(self.name)
+
+ @property
+ def _variable_shape(self):
+ raise ValueError('Should not use this method.')
+
+ def _get_dense_tensor(self, inputs, weight_collections=None,
+ trainable=None):
+ raise ValueError('Should not use this method.')
+
+ @property
+ def _num_buckets(self):
+ return 4
+
+ def _get_sparse_tensors(self, inputs, weight_collections=None,
+ trainable=None):
+ sp_tensor = sparse_tensor.SparseTensor(
+ indices=[[0, 0], [1, 0], [1, 1]],
+ values=[2, 0, 3],
+ dense_shape=[2, 2])
+ return fc_old._CategoricalColumn.IdWeightPair(sp_tensor, None)
+
+ dense_and_sparse_column = _DenseAndSparseColumn()
+ with ops.Graph().as_default():
+ sp_tensor = sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'],
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ features = {dense_and_sparse_column.name: sp_tensor}
+ predictions = fc.linear_model(features, [dense_and_sparse_column])
+ bias = get_linear_model_bias()
+ dense_and_sparse_column_var = get_linear_model_column_var(
+ dense_and_sparse_column)
+ with _initialized_session() as sess:
+ sess.run(dense_and_sparse_column_var.assign(
+ [[10.], [100.], [1000.], [10000.]]))
+ sess.run(bias.assign([5.]))
+ self.assertAllClose([[1005.], [10015.]], predictions.eval())
+
+ def test_dense_multi_output(self):
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default():
+ features = {'price': [[1.], [5.]]}
+ predictions = fc.linear_model(features, [price], units=3)
+ bias = get_linear_model_bias()
+ price_var = get_linear_model_column_var(price)
+ with _initialized_session() as sess:
+ self.assertAllClose(np.zeros((3,)), bias.eval())
+ self.assertAllClose(np.zeros((1, 3)), price_var.eval())
+ sess.run(price_var.assign([[10., 100., 1000.]]))
+ sess.run(bias.assign([5., 6., 7.]))
+ self.assertAllClose([[15., 106., 1007.], [55., 506., 5007.]],
+ predictions.eval())
+
+ def test_sparse_multi_output(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default():
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'], # hashed to = [2, 0, 3]
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ features = {'wire_cast': wire_tensor}
+ predictions = fc.linear_model(features, [wire_cast], units=3)
+ bias = get_linear_model_bias()
+ wire_cast_var = get_linear_model_column_var(wire_cast)
+ with _initialized_session() as sess:
+ self.assertAllClose(np.zeros((3,)), bias.eval())
+ self.assertAllClose(np.zeros((4, 3)), wire_cast_var.eval())
+ sess.run(
+ wire_cast_var.assign([[10., 11., 12.], [100., 110., 120.], [
+ 1000., 1100., 1200.
+ ], [10000., 11000., 12000.]]))
+ sess.run(bias.assign([5., 6., 7.]))
+ self.assertAllClose([[1005., 1106., 1207.], [10015., 11017., 12019.]],
+ predictions.eval())
+
+ def test_dense_multi_dimension(self):
+ price = fc_old.numeric_column('price', shape=2)
+ with ops.Graph().as_default():
+ features = {'price': [[1., 2.], [5., 6.]]}
+ predictions = fc.linear_model(features, [price])
+ price_var = get_linear_model_column_var(price)
+ with _initialized_session() as sess:
+ self.assertAllClose([[0.], [0.]], price_var.eval())
+ sess.run(price_var.assign([[10.], [100.]]))
+ self.assertAllClose([[210.], [650.]], predictions.eval())
+
+ def test_sparse_multi_rank(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default():
+ wire_tensor = array_ops.sparse_placeholder(dtypes.string)
+ wire_value = sparse_tensor.SparseTensorValue(
+ values=['omar', 'stringer', 'marlo', 'omar'], # hashed = [2, 0, 3, 2]
+ indices=[[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 0, 1]],
+ dense_shape=[2, 2, 2])
+ features = {'wire_cast': wire_tensor}
+ predictions = fc.linear_model(features, [wire_cast])
+ wire_cast_var = get_linear_model_column_var(wire_cast)
+ with _initialized_session() as sess:
+ self.assertAllClose(np.zeros((4, 1)), wire_cast_var.eval())
+ self.assertAllClose(
+ np.zeros((2, 1)),
+ predictions.eval(feed_dict={wire_tensor: wire_value}))
+ sess.run(wire_cast_var.assign([[10.], [100.], [1000.], [10000.]]))
+ self.assertAllClose(
+ [[1010.], [11000.]],
+ predictions.eval(feed_dict={wire_tensor: wire_value}))
+
+ def test_sparse_combiner(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default():
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'], # hashed to = [2, 0, 3]
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ features = {'wire_cast': wire_tensor}
+ predictions = fc.linear_model(
+ features, [wire_cast], sparse_combiner='mean')
+ bias = get_linear_model_bias()
+ wire_cast_var = get_linear_model_column_var(wire_cast)
+ with _initialized_session() as sess:
+ sess.run(wire_cast_var.assign([[10.], [100.], [1000.], [10000.]]))
+ sess.run(bias.assign([5.]))
+ self.assertAllClose([[1005.], [5010.]], predictions.eval())
+
+ def test_sparse_combiner_with_negative_weights(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ wire_cast_weights = fc_old.weighted_categorical_column(wire_cast, 'weights')
+
+ with ops.Graph().as_default():
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'], # hashed to = [2, 0, 3]
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ features = {
+ 'wire_cast': wire_tensor,
+ 'weights': constant_op.constant([[1., 1., -1.0]])
+ }
+ predictions = fc.linear_model(
+ features, [wire_cast_weights], sparse_combiner='sum')
+ bias = get_linear_model_bias()
+ wire_cast_var = get_linear_model_column_var(wire_cast)
+ with _initialized_session() as sess:
+ sess.run(wire_cast_var.assign([[10.], [100.], [1000.], [10000.]]))
+ sess.run(bias.assign([5.]))
+ self.assertAllClose([[1005.], [-9985.]], predictions.eval())
+
+ def test_dense_multi_dimension_multi_output(self):
+ price = fc_old.numeric_column('price', shape=2)
+ with ops.Graph().as_default():
+ features = {'price': [[1., 2.], [5., 6.]]}
+ predictions = fc.linear_model(features, [price], units=3)
+ bias = get_linear_model_bias()
+ price_var = get_linear_model_column_var(price)
+ with _initialized_session() as sess:
+ self.assertAllClose(np.zeros((3,)), bias.eval())
+ self.assertAllClose(np.zeros((2, 3)), price_var.eval())
+ sess.run(price_var.assign([[1., 2., 3.], [10., 100., 1000.]]))
+ sess.run(bias.assign([2., 3., 4.]))
+ self.assertAllClose([[23., 205., 2007.], [67., 613., 6019.]],
+ predictions.eval())
+
+ def test_raises_if_shape_mismatch(self):
+ price = fc_old.numeric_column('price', shape=2)
+ with ops.Graph().as_default():
+ features = {'price': [[1.], [5.]]}
+ with self.assertRaisesRegexp(
+ Exception,
+ r'Cannot reshape a tensor with 2 elements to shape \[2,2\]'):
+ fc.linear_model(features, [price])
+
+ def test_dense_reshaping(self):
+ price = fc_old.numeric_column('price', shape=[1, 2])
+ with ops.Graph().as_default():
+ features = {'price': [[[1., 2.]], [[5., 6.]]]}
+ predictions = fc.linear_model(features, [price])
+ bias = get_linear_model_bias()
+ price_var = get_linear_model_column_var(price)
+ with _initialized_session() as sess:
+ self.assertAllClose([0.], bias.eval())
+ self.assertAllClose([[0.], [0.]], price_var.eval())
+ self.assertAllClose([[0.], [0.]], predictions.eval())
+ sess.run(price_var.assign([[10.], [100.]]))
+ self.assertAllClose([[210.], [650.]], predictions.eval())
+
+ def test_dense_multi_column(self):
+ price1 = fc_old.numeric_column('price1', shape=2)
+ price2 = fc_old.numeric_column('price2')
+ with ops.Graph().as_default():
+ features = {
+ 'price1': [[1., 2.], [5., 6.]],
+ 'price2': [[3.], [4.]]
+ }
+ predictions = fc.linear_model(features, [price1, price2])
+ bias = get_linear_model_bias()
+ price1_var = get_linear_model_column_var(price1)
+ price2_var = get_linear_model_column_var(price2)
+ with _initialized_session() as sess:
+ self.assertAllClose([0.], bias.eval())
+ self.assertAllClose([[0.], [0.]], price1_var.eval())
+ self.assertAllClose([[0.]], price2_var.eval())
+ self.assertAllClose([[0.], [0.]], predictions.eval())
+ sess.run(price1_var.assign([[10.], [100.]]))
+ sess.run(price2_var.assign([[1000.]]))
+ sess.run(bias.assign([7.]))
+ self.assertAllClose([[3217.], [4657.]], predictions.eval())
+
+ def test_fills_cols_to_vars(self):
+ price1 = fc_old.numeric_column('price1', shape=2)
+ price2 = fc_old.numeric_column('price2')
+ with ops.Graph().as_default():
+ features = {'price1': [[1., 2.], [5., 6.]], 'price2': [[3.], [4.]]}
+ cols_to_vars = {}
+ fc.linear_model(features, [price1, price2], cols_to_vars=cols_to_vars)
+ bias = get_linear_model_bias()
+ price1_var = get_linear_model_column_var(price1)
+ price2_var = get_linear_model_column_var(price2)
+ self.assertAllEqual(cols_to_vars['bias'], [bias])
+ self.assertAllEqual(cols_to_vars[price1], [price1_var])
+ self.assertAllEqual(cols_to_vars[price2], [price2_var])
+
+ def test_fills_cols_to_vars_partitioned_variables(self):
+ price1 = fc_old.numeric_column('price1', shape=2)
+ price2 = fc_old.numeric_column('price2', shape=3)
+ with ops.Graph().as_default():
+ features = {
+ 'price1': [[1., 2.], [6., 7.]],
+ 'price2': [[3., 4., 5.], [8., 9., 10.]]
+ }
+ cols_to_vars = {}
+ with variable_scope.variable_scope(
+ 'linear',
+ partitioner=partitioned_variables.fixed_size_partitioner(2, axis=0)):
+ fc.linear_model(features, [price1, price2], cols_to_vars=cols_to_vars)
+ with _initialized_session():
+ self.assertEqual([0.], cols_to_vars['bias'][0].eval())
+ # Partitioning shards the [2, 1] price1 var into 2 [1, 1] Variables.
+ self.assertAllEqual([[0.]], cols_to_vars[price1][0].eval())
+ self.assertAllEqual([[0.]], cols_to_vars[price1][1].eval())
+ # Partitioning shards the [3, 1] price2 var into a [2, 1] Variable and
+ # a [1, 1] Variable.
+ self.assertAllEqual([[0.], [0.]], cols_to_vars[price2][0].eval())
+ self.assertAllEqual([[0.]], cols_to_vars[price2][1].eval())
+
+ def test_dense_collection(self):
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default() as g:
+ features = {'price': [[1.], [5.]]}
+ fc.linear_model(features, [price], weight_collections=['my-vars'])
+ my_vars = g.get_collection('my-vars')
+ bias = get_linear_model_bias()
+ price_var = get_linear_model_column_var(price)
+ self.assertIn(bias, my_vars)
+ self.assertIn(price_var, my_vars)
+
+ def test_sparse_collection(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default() as g:
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar'], indices=[[0, 0]], dense_shape=[1, 1])
+ features = {'wire_cast': wire_tensor}
+ fc.linear_model(
+ features, [wire_cast], weight_collections=['my-vars'])
+ my_vars = g.get_collection('my-vars')
+ bias = get_linear_model_bias()
+ wire_cast_var = get_linear_model_column_var(wire_cast)
+ self.assertIn(bias, my_vars)
+ self.assertIn(wire_cast_var, my_vars)
+
+ def test_dense_trainable_default(self):
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default() as g:
+ features = {'price': [[1.], [5.]]}
+ fc.linear_model(features, [price])
+ bias = get_linear_model_bias()
+ price_var = get_linear_model_column_var(price)
+ trainable_vars = g.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)
+ self.assertIn(bias, trainable_vars)
+ self.assertIn(price_var, trainable_vars)
+
+ def test_sparse_trainable_default(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default() as g:
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar'], indices=[[0, 0]], dense_shape=[1, 1])
+ features = {'wire_cast': wire_tensor}
+ fc.linear_model(features, [wire_cast])
+ trainable_vars = g.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)
+ bias = get_linear_model_bias()
+ wire_cast_var = get_linear_model_column_var(wire_cast)
+ self.assertIn(bias, trainable_vars)
+ self.assertIn(wire_cast_var, trainable_vars)
+
+ def test_dense_trainable_false(self):
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default() as g:
+ features = {'price': [[1.], [5.]]}
+ fc.linear_model(features, [price], trainable=False)
+ trainable_vars = g.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)
+ self.assertEqual([], trainable_vars)
+
+ def test_sparse_trainable_false(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default() as g:
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar'], indices=[[0, 0]], dense_shape=[1, 1])
+ features = {'wire_cast': wire_tensor}
+ fc.linear_model(features, [wire_cast], trainable=False)
+ trainable_vars = g.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)
+ self.assertEqual([], trainable_vars)
+
+ def test_column_order(self):
+ price_a = fc_old.numeric_column('price_a')
+ price_b = fc_old.numeric_column('price_b')
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default() as g:
+ features = {
+ 'price_a': [[1.]],
+ 'price_b': [[3.]],
+ 'wire_cast':
+ sparse_tensor.SparseTensor(
+ values=['omar'], indices=[[0, 0]], dense_shape=[1, 1])
+ }
+ fc.linear_model(
+ features, [price_a, wire_cast, price_b],
+ weight_collections=['my-vars'])
+ my_vars = g.get_collection('my-vars')
+ self.assertIn('price_a', my_vars[0].name)
+ self.assertIn('price_b', my_vars[1].name)
+ self.assertIn('wire_cast', my_vars[2].name)
+
+ with ops.Graph().as_default() as g:
+ features = {
+ 'price_a': [[1.]],
+ 'price_b': [[3.]],
+ 'wire_cast':
+ sparse_tensor.SparseTensor(
+ values=['omar'], indices=[[0, 0]], dense_shape=[1, 1])
+ }
+ fc.linear_model(
+ features, [wire_cast, price_b, price_a],
+ weight_collections=['my-vars'])
+ my_vars = g.get_collection('my-vars')
+ self.assertIn('price_a', my_vars[0].name)
+ self.assertIn('price_b', my_vars[1].name)
+ self.assertIn('wire_cast', my_vars[2].name)
+
+ def test_static_batch_size_mismatch(self):
+ price1 = fc_old.numeric_column('price1')
+ price2 = fc_old.numeric_column('price2')
+ with ops.Graph().as_default():
+ features = {
+ 'price1': [[1.], [5.], [7.]], # batchsize = 3
+ 'price2': [[3.], [4.]] # batchsize = 2
+ }
+ with self.assertRaisesRegexp(
+ ValueError,
+ 'Batch size \(first dimension\) of each feature must be same.'): # pylint: disable=anomalous-backslash-in-string
+ fc.linear_model(features, [price1, price2])
+
+ def test_subset_of_static_batch_size_mismatch(self):
+ price1 = fc_old.numeric_column('price1')
+ price2 = fc_old.numeric_column('price2')
+ price3 = fc_old.numeric_column('price3')
+ with ops.Graph().as_default():
+ features = {
+ 'price1': array_ops.placeholder(dtype=dtypes.int64), # batchsize = 3
+ 'price2': [[3.], [4.]], # batchsize = 2
+ 'price3': [[3.], [4.], [5.]] # batchsize = 3
+ }
+ with self.assertRaisesRegexp(
+ ValueError,
+ 'Batch size \(first dimension\) of each feature must be same.'): # pylint: disable=anomalous-backslash-in-string
+ fc.linear_model(features, [price1, price2, price3])
+
+ def test_runtime_batch_size_mismatch(self):
+ price1 = fc_old.numeric_column('price1')
+ price2 = fc_old.numeric_column('price2')
+ with ops.Graph().as_default():
+ features = {
+ 'price1': array_ops.placeholder(dtype=dtypes.int64), # batchsize = 3
+ 'price2': [[3.], [4.]] # batchsize = 2
+ }
+ predictions = fc.linear_model(features, [price1, price2])
+ with _initialized_session() as sess:
+ with self.assertRaisesRegexp(errors.OpError,
+ 'must have the same size and shape'):
+ sess.run(
+ predictions, feed_dict={features['price1']: [[1.], [5.], [7.]]})
+
+ def test_runtime_batch_size_matches(self):
+ price1 = fc_old.numeric_column('price1')
+ price2 = fc_old.numeric_column('price2')
+ with ops.Graph().as_default():
+ features = {
+ 'price1': array_ops.placeholder(dtype=dtypes.int64), # batchsize = 2
+ 'price2': array_ops.placeholder(dtype=dtypes.int64), # batchsize = 2
+ }
+ predictions = fc.linear_model(features, [price1, price2])
+ with _initialized_session() as sess:
+ sess.run(
+ predictions,
+ feed_dict={
+ features['price1']: [[1.], [5.]],
+ features['price2']: [[1.], [5.]],
+ })
+
+ def test_with_numpy_input_fn(self):
+ price = fc_old.numeric_column('price')
+ price_buckets = fc_old.bucketized_column(
+ price, boundaries=[
+ 0.,
+ 10.,
+ 100.,
+ ])
+ body_style = fc_old.categorical_column_with_vocabulary_list(
+ 'body-style', vocabulary_list=['hardtop', 'wagon', 'sedan'])
+
+ input_fn = numpy_io.numpy_input_fn(
+ x={
+ 'price': np.array([-1., 2., 13., 104.]),
+ 'body-style': np.array(['sedan', 'hardtop', 'wagon', 'sedan']),
+ },
+ batch_size=2,
+ shuffle=False)
+ features = input_fn()
+ net = fc.linear_model(features, [price_buckets, body_style])
+ # self.assertEqual(1 + 3 + 5, net.shape[1])
+ with _initialized_session() as sess:
+ coord = coordinator.Coordinator()
+ threads = queue_runner_impl.start_queue_runners(sess, coord=coord)
+
+ bias = get_linear_model_bias()
+ price_buckets_var = get_linear_model_column_var(price_buckets)
+ body_style_var = get_linear_model_column_var(body_style)
+
+ sess.run(price_buckets_var.assign([[10.], [100.], [1000.], [10000.]]))
+ sess.run(body_style_var.assign([[-10.], [-100.], [-1000.]]))
+ sess.run(bias.assign([5.]))
+
+ self.assertAllClose([[10 - 1000 + 5.], [100 - 10 + 5.]], sess.run(net))
+
+ coord.request_stop()
+ coord.join(threads)
+
+ def test_with_1d_sparse_tensor(self):
+ price = fc_old.numeric_column('price')
+ price_buckets = fc_old.bucketized_column(
+ price, boundaries=[
+ 0.,
+ 10.,
+ 100.,
+ ])
+ body_style = fc_old.categorical_column_with_vocabulary_list(
+ 'body-style', vocabulary_list=['hardtop', 'wagon', 'sedan'])
+
+ # Provides 1-dim tensor and dense tensor.
+ features = {
+ 'price': constant_op.constant([-1., 12.,]),
+ 'body-style': sparse_tensor.SparseTensor(
+ indices=((0,), (1,)),
+ values=('sedan', 'hardtop'),
+ dense_shape=(2,)),
+ }
+ self.assertEqual(1, features['price'].shape.ndims)
+ self.assertEqual(1, features['body-style'].dense_shape.get_shape()[0])
+
+ net = fc.linear_model(features, [price_buckets, body_style])
+ with _initialized_session() as sess:
+ bias = get_linear_model_bias()
+ price_buckets_var = get_linear_model_column_var(price_buckets)
+ body_style_var = get_linear_model_column_var(body_style)
+
+ sess.run(price_buckets_var.assign([[10.], [100.], [1000.], [10000.]]))
+ sess.run(body_style_var.assign([[-10.], [-100.], [-1000.]]))
+ sess.run(bias.assign([5.]))
+
+ self.assertAllClose([[10 - 1000 + 5.], [1000 - 10 + 5.]], sess.run(net))
+
+ def test_with_1d_unknown_shape_sparse_tensor(self):
+ price = fc_old.numeric_column('price')
+ price_buckets = fc_old.bucketized_column(
+ price, boundaries=[
+ 0.,
+ 10.,
+ 100.,
+ ])
+ body_style = fc_old.categorical_column_with_vocabulary_list(
+ 'body-style', vocabulary_list=['hardtop', 'wagon', 'sedan'])
+ country = fc_old.categorical_column_with_vocabulary_list(
+ 'country', vocabulary_list=['US', 'JP', 'CA'])
+
+ # Provides 1-dim tensor and dense tensor.
+ features = {
+ 'price': array_ops.placeholder(dtypes.float32),
+ 'body-style': array_ops.sparse_placeholder(dtypes.string),
+ 'country': array_ops.placeholder(dtypes.string),
+ }
+ self.assertIsNone(features['price'].shape.ndims)
+ self.assertIsNone(features['body-style'].get_shape().ndims)
+
+ price_data = np.array([-1., 12.])
+ body_style_data = sparse_tensor.SparseTensorValue(
+ indices=((0,), (1,)),
+ values=('sedan', 'hardtop'),
+ dense_shape=(2,))
+ country_data = np.array(['US', 'CA'])
+
+ net = fc.linear_model(features, [price_buckets, body_style, country])
+ bias = get_linear_model_bias()
+ price_buckets_var = get_linear_model_column_var(price_buckets)
+ body_style_var = get_linear_model_column_var(body_style)
+ with _initialized_session() as sess:
+ sess.run(price_buckets_var.assign([[10.], [100.], [1000.], [10000.]]))
+ sess.run(body_style_var.assign([[-10.], [-100.], [-1000.]]))
+ sess.run(bias.assign([5.]))
+
+ self.assertAllClose([[10 - 1000 + 5.], [1000 - 10 + 5.]],
+ sess.run(
+ net,
+ feed_dict={
+ features['price']: price_data,
+ features['body-style']: body_style_data,
+ features['country']: country_data
+ }))
+
+ def test_with_rank_0_feature(self):
+ price = fc_old.numeric_column('price')
+ features = {
+ 'price': constant_op.constant(0),
+ }
+ self.assertEqual(0, features['price'].shape.ndims)
+
+ # Static rank 0 should fail
+ with self.assertRaisesRegexp(ValueError, 'Feature .* cannot have rank 0'):
+ fc.linear_model(features, [price])
+
+ # Dynamic rank 0 should fail
+ features = {
+ 'price': array_ops.placeholder(dtypes.float32),
+ }
+ net = fc.linear_model(features, [price])
+ self.assertEqual(1, net.shape[1])
+ with _initialized_session() as sess:
+ with self.assertRaisesOpError('Feature .* cannot have rank 0'):
+ sess.run(net, feed_dict={features['price']: np.array(1)})
+
+ def test_multiple_linear_models(self):
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default():
+ features1 = {'price': [[1.], [5.]]}
+ features2 = {'price': [[2.], [10.]]}
+ predictions1 = fc.linear_model(features1, [price])
+ predictions2 = fc.linear_model(features2, [price])
+ bias1 = get_linear_model_bias(name='linear_model')
+ bias2 = get_linear_model_bias(name='linear_model_1')
+ price_var1 = get_linear_model_column_var(price, name='linear_model')
+ price_var2 = get_linear_model_column_var(price, name='linear_model_1')
+ with _initialized_session() as sess:
+ self.assertAllClose([0.], bias1.eval())
+ sess.run(price_var1.assign([[10.]]))
+ sess.run(bias1.assign([5.]))
+ self.assertAllClose([[15.], [55.]], predictions1.eval())
+ self.assertAllClose([0.], bias2.eval())
+ sess.run(price_var2.assign([[10.]]))
+ sess.run(bias2.assign([5.]))
+ self.assertAllClose([[25.], [105.]], predictions2.eval())
+
+
+class _LinearModelTest(test.TestCase):
+
+ def test_raises_if_empty_feature_columns(self):
+ with self.assertRaisesRegexp(ValueError,
+ 'feature_columns must not be empty'):
+ get_keras_linear_model_predictions(features={}, feature_columns=[])
+
+ def test_should_be_feature_column(self):
+ with self.assertRaisesRegexp(ValueError, 'must be a _FeatureColumn'):
+ get_keras_linear_model_predictions(
+ features={'a': [[0]]}, feature_columns='NotSupported')
+
+ def test_should_be_dense_or_categorical_column(self):
+
+ class NotSupportedColumn(fc_old._FeatureColumn):
+
+ @property
+ def name(self):
+ return 'NotSupportedColumn'
+
+ def _transform_feature(self, cache):
+ pass
+
+ @property
+ def _parse_example_spec(self):
+ pass
+
+ with self.assertRaisesRegexp(
+ ValueError, 'must be either a _DenseColumn or _CategoricalColumn'):
+ get_keras_linear_model_predictions(
+ features={'a': [[0]]}, feature_columns=[NotSupportedColumn()])
+
+ def test_does_not_support_dict_columns(self):
+ with self.assertRaisesRegexp(
+ ValueError, 'Expected feature_columns to be iterable, found dict.'):
+ fc.linear_model(
+ features={'a': [[0]]},
+ feature_columns={'a': fc_old.numeric_column('a')})
+
+ def test_raises_if_duplicate_name(self):
+ with self.assertRaisesRegexp(
+ ValueError, 'Duplicate feature column name found for columns'):
+ get_keras_linear_model_predictions(
+ features={'a': [[0]]},
+ feature_columns=[
+ fc_old.numeric_column('a'),
+ fc_old.numeric_column('a')
+ ])
+
+ def test_dense_bias(self):
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default():
+ features = {'price': [[1.], [5.]]}
+ predictions = get_keras_linear_model_predictions(features, [price])
+ bias = get_linear_model_bias()
+ price_var = get_linear_model_column_var(price)
+ with _initialized_session() as sess:
+ self.assertAllClose([0.], bias.eval())
+ sess.run(price_var.assign([[10.]]))
+ sess.run(bias.assign([5.]))
+ self.assertAllClose([[15.], [55.]], predictions.eval())
+
+ def test_sparse_bias(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default():
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'], # hashed to = [2, 0, 3]
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ features = {'wire_cast': wire_tensor}
+ predictions = get_keras_linear_model_predictions(features, [wire_cast])
+ bias = get_linear_model_bias()
+ wire_cast_var = get_linear_model_column_var(wire_cast)
+ with _initialized_session() as sess:
+ self.assertAllClose([0.], bias.eval())
+ self.assertAllClose([[0.], [0.], [0.], [0.]], wire_cast_var.eval())
+ sess.run(wire_cast_var.assign([[10.], [100.], [1000.], [10000.]]))
+ sess.run(bias.assign([5.]))
+ self.assertAllClose([[1005.], [10015.]], predictions.eval())
+
+ def test_dense_and_sparse_bias(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default():
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'], # hashed to = [2, 0, 3]
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ features = {'wire_cast': wire_tensor, 'price': [[1.], [5.]]}
+ predictions = get_keras_linear_model_predictions(features,
+ [wire_cast, price])
+ bias = get_linear_model_bias()
+ wire_cast_var = get_linear_model_column_var(wire_cast)
+ price_var = get_linear_model_column_var(price)
+ with _initialized_session() as sess:
+ sess.run(wire_cast_var.assign([[10.], [100.], [1000.], [10000.]]))
+ sess.run(bias.assign([5.]))
+ sess.run(price_var.assign([[10.]]))
+ self.assertAllClose([[1015.], [10065.]], predictions.eval())
+
+ def test_dense_and_sparse_column(self):
+ """When the column is both dense and sparse, uses sparse tensors."""
+
+ class _DenseAndSparseColumn(fc_old._DenseColumn, fc_old._CategoricalColumn):
+
+ @property
+ def name(self):
+ return 'dense_and_sparse_column'
+
+ @property
+ def _parse_example_spec(self):
+ return {self.name: parsing_ops.VarLenFeature(self.dtype)}
+
+ def _transform_feature(self, inputs):
+ return inputs.get(self.name)
+
+ @property
+ def _variable_shape(self):
+ raise ValueError('Should not use this method.')
+
+ def _get_dense_tensor(self,
+ inputs,
+ weight_collections=None,
+ trainable=None):
+ raise ValueError('Should not use this method.')
+
+ @property
+ def _num_buckets(self):
+ return 4
+
+ def _get_sparse_tensors(self,
+ inputs,
+ weight_collections=None,
+ trainable=None):
+ sp_tensor = sparse_tensor.SparseTensor(
+ indices=[[0, 0], [1, 0], [1, 1]],
+ values=[2, 0, 3],
+ dense_shape=[2, 2])
+ return fc_old._CategoricalColumn.IdWeightPair(sp_tensor, None)
+
+ dense_and_sparse_column = _DenseAndSparseColumn()
+ with ops.Graph().as_default():
+ sp_tensor = sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'],
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ features = {dense_and_sparse_column.name: sp_tensor}
+ predictions = get_keras_linear_model_predictions(
+ features, [dense_and_sparse_column])
+ bias = get_linear_model_bias()
+ dense_and_sparse_column_var = get_linear_model_column_var(
+ dense_and_sparse_column)
+ with _initialized_session() as sess:
+ sess.run(
+ dense_and_sparse_column_var.assign([[10.], [100.], [1000.],
+ [10000.]]))
+ sess.run(bias.assign([5.]))
+ self.assertAllClose([[1005.], [10015.]], predictions.eval())
+
+ def test_dense_multi_output(self):
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default():
+ features = {'price': [[1.], [5.]]}
+ predictions = get_keras_linear_model_predictions(
+ features, [price], units=3)
+ bias = get_linear_model_bias()
+ price_var = get_linear_model_column_var(price)
+ with _initialized_session() as sess:
+ self.assertAllClose(np.zeros((3,)), bias.eval())
+ self.assertAllClose(np.zeros((1, 3)), price_var.eval())
+ sess.run(price_var.assign([[10., 100., 1000.]]))
+ sess.run(bias.assign([5., 6., 7.]))
+ self.assertAllClose([[15., 106., 1007.], [55., 506., 5007.]],
+ predictions.eval())
+
+ def test_sparse_multi_output(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default():
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'], # hashed to = [2, 0, 3]
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ features = {'wire_cast': wire_tensor}
+ predictions = get_keras_linear_model_predictions(
+ features, [wire_cast], units=3)
+ bias = get_linear_model_bias()
+ wire_cast_var = get_linear_model_column_var(wire_cast)
+ with _initialized_session() as sess:
+ self.assertAllClose(np.zeros((3,)), bias.eval())
+ self.assertAllClose(np.zeros((4, 3)), wire_cast_var.eval())
+ sess.run(
+ wire_cast_var.assign([[10., 11., 12.], [100., 110., 120.],
+ [1000., 1100.,
+ 1200.], [10000., 11000., 12000.]]))
+ sess.run(bias.assign([5., 6., 7.]))
+ self.assertAllClose([[1005., 1106., 1207.], [10015., 11017., 12019.]],
+ predictions.eval())
+
+ def test_dense_multi_dimension(self):
+ price = fc_old.numeric_column('price', shape=2)
+ with ops.Graph().as_default():
+ features = {'price': [[1., 2.], [5., 6.]]}
+ predictions = get_keras_linear_model_predictions(features, [price])
+ price_var = get_linear_model_column_var(price)
+ with _initialized_session() as sess:
+ self.assertAllClose([[0.], [0.]], price_var.eval())
+ sess.run(price_var.assign([[10.], [100.]]))
+ self.assertAllClose([[210.], [650.]], predictions.eval())
+
+ def test_sparse_multi_rank(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default():
+ wire_tensor = array_ops.sparse_placeholder(dtypes.string)
+ wire_value = sparse_tensor.SparseTensorValue(
+ values=['omar', 'stringer', 'marlo', 'omar'], # hashed = [2, 0, 3, 2]
+ indices=[[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 0, 1]],
+ dense_shape=[2, 2, 2])
+ features = {'wire_cast': wire_tensor}
+ predictions = get_keras_linear_model_predictions(features, [wire_cast])
+ wire_cast_var = get_linear_model_column_var(wire_cast)
+ with _initialized_session() as sess:
+ self.assertAllClose(np.zeros((4, 1)), wire_cast_var.eval())
+ self.assertAllClose(
+ np.zeros((2, 1)),
+ predictions.eval(feed_dict={wire_tensor: wire_value}))
+ sess.run(wire_cast_var.assign([[10.], [100.], [1000.], [10000.]]))
+ self.assertAllClose(
+ [[1010.], [11000.]],
+ predictions.eval(feed_dict={wire_tensor: wire_value}))
+
+ def test_sparse_combiner(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default():
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'], # hashed to = [2, 0, 3]
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ features = {'wire_cast': wire_tensor}
+ predictions = get_keras_linear_model_predictions(
+ features, [wire_cast], sparse_combiner='mean')
+ bias = get_linear_model_bias()
+ wire_cast_var = get_linear_model_column_var(wire_cast)
+ with _initialized_session() as sess:
+ sess.run(wire_cast_var.assign([[10.], [100.], [1000.], [10000.]]))
+ sess.run(bias.assign([5.]))
+ self.assertAllClose([[1005.], [5010.]], predictions.eval())
+
+ def test_dense_multi_dimension_multi_output(self):
+ price = fc_old.numeric_column('price', shape=2)
+ with ops.Graph().as_default():
+ features = {'price': [[1., 2.], [5., 6.]]}
+ predictions = get_keras_linear_model_predictions(
+ features, [price], units=3)
+ bias = get_linear_model_bias()
+ price_var = get_linear_model_column_var(price)
+ with _initialized_session() as sess:
+ self.assertAllClose(np.zeros((3,)), bias.eval())
+ self.assertAllClose(np.zeros((2, 3)), price_var.eval())
+ sess.run(price_var.assign([[1., 2., 3.], [10., 100., 1000.]]))
+ sess.run(bias.assign([2., 3., 4.]))
+ self.assertAllClose([[23., 205., 2007.], [67., 613., 6019.]],
+ predictions.eval())
+
+ def test_raises_if_shape_mismatch(self):
+ price = fc_old.numeric_column('price', shape=2)
+ with ops.Graph().as_default():
+ features = {'price': [[1.], [5.]]}
+ with self.assertRaisesRegexp(
+ Exception,
+ r'Cannot reshape a tensor with 2 elements to shape \[2,2\]'):
+ get_keras_linear_model_predictions(features, [price])
+
+ def test_dense_reshaping(self):
+ price = fc_old.numeric_column('price', shape=[1, 2])
+ with ops.Graph().as_default():
+ features = {'price': [[[1., 2.]], [[5., 6.]]]}
+ predictions = get_keras_linear_model_predictions(features, [price])
+ bias = get_linear_model_bias()
+ price_var = get_linear_model_column_var(price)
+ with _initialized_session() as sess:
+ self.assertAllClose([0.], bias.eval())
+ self.assertAllClose([[0.], [0.]], price_var.eval())
+ self.assertAllClose([[0.], [0.]], predictions.eval())
+ sess.run(price_var.assign([[10.], [100.]]))
+ self.assertAllClose([[210.], [650.]], predictions.eval())
+
+ def test_dense_multi_column(self):
+ price1 = fc_old.numeric_column('price1', shape=2)
+ price2 = fc_old.numeric_column('price2')
+ with ops.Graph().as_default():
+ features = {'price1': [[1., 2.], [5., 6.]], 'price2': [[3.], [4.]]}
+ predictions = get_keras_linear_model_predictions(features,
+ [price1, price2])
+ bias = get_linear_model_bias()
+ price1_var = get_linear_model_column_var(price1)
+ price2_var = get_linear_model_column_var(price2)
+ with _initialized_session() as sess:
+ self.assertAllClose([0.], bias.eval())
+ self.assertAllClose([[0.], [0.]], price1_var.eval())
+ self.assertAllClose([[0.]], price2_var.eval())
+ self.assertAllClose([[0.], [0.]], predictions.eval())
+ sess.run(price1_var.assign([[10.], [100.]]))
+ sess.run(price2_var.assign([[1000.]]))
+ sess.run(bias.assign([7.]))
+ self.assertAllClose([[3217.], [4657.]], predictions.eval())
+
+ def test_fills_cols_to_vars(self):
+ price1 = fc_old.numeric_column('price1', shape=2)
+ price2 = fc_old.numeric_column('price2')
+ with ops.Graph().as_default():
+ features = {'price1': [[1., 2.], [5., 6.]], 'price2': [[3.], [4.]]}
+ cols_to_vars = {}
+ get_keras_linear_model_predictions(
+ features, [price1, price2], cols_to_vars=cols_to_vars)
+ bias = get_linear_model_bias()
+ price1_var = get_linear_model_column_var(price1)
+ price2_var = get_linear_model_column_var(price2)
+ self.assertAllEqual(cols_to_vars['bias'], [bias])
+ self.assertAllEqual(cols_to_vars[price1], [price1_var])
+ self.assertAllEqual(cols_to_vars[price2], [price2_var])
+
+ def test_fills_cols_to_vars_partitioned_variables(self):
+ price1 = fc_old.numeric_column('price1', shape=2)
+ price2 = fc_old.numeric_column('price2', shape=3)
+ with ops.Graph().as_default():
+ features = {
+ 'price1': [[1., 2.], [6., 7.]],
+ 'price2': [[3., 4., 5.], [8., 9., 10.]]
+ }
+ cols_to_vars = {}
+ with variable_scope.variable_scope(
+ 'linear',
+ partitioner=partitioned_variables.fixed_size_partitioner(2, axis=0)):
+ get_keras_linear_model_predictions(
+ features, [price1, price2], cols_to_vars=cols_to_vars)
+ with _initialized_session():
+ self.assertEqual([0.], cols_to_vars['bias'][0].eval())
+ # Partitioning shards the [2, 1] price1 var into 2 [1, 1] Variables.
+ self.assertAllEqual([[0.]], cols_to_vars[price1][0].eval())
+ self.assertAllEqual([[0.]], cols_to_vars[price1][1].eval())
+ # Partitioning shards the [3, 1] price2 var into a [2, 1] Variable and
+ # a [1, 1] Variable.
+ self.assertAllEqual([[0.], [0.]], cols_to_vars[price2][0].eval())
+ self.assertAllEqual([[0.]], cols_to_vars[price2][1].eval())
+
+ def test_dense_collection(self):
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default() as g:
+ features = {'price': [[1.], [5.]]}
+ get_keras_linear_model_predictions(
+ features, [price], weight_collections=['my-vars'])
+ my_vars = g.get_collection('my-vars')
+ bias = get_linear_model_bias()
+ price_var = get_linear_model_column_var(price)
+ self.assertIn(bias, my_vars)
+ self.assertIn(price_var, my_vars)
+
+ def test_sparse_collection(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default() as g:
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar'], indices=[[0, 0]], dense_shape=[1, 1])
+ features = {'wire_cast': wire_tensor}
+ get_keras_linear_model_predictions(
+ features, [wire_cast], weight_collections=['my-vars'])
+ my_vars = g.get_collection('my-vars')
+ bias = get_linear_model_bias()
+ wire_cast_var = get_linear_model_column_var(wire_cast)
+ self.assertIn(bias, my_vars)
+ self.assertIn(wire_cast_var, my_vars)
+
+ def test_dense_trainable_default(self):
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default() as g:
+ features = {'price': [[1.], [5.]]}
+ get_keras_linear_model_predictions(features, [price])
+ bias = get_linear_model_bias()
+ price_var = get_linear_model_column_var(price)
+ trainable_vars = g.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)
+ self.assertIn(bias, trainable_vars)
+ self.assertIn(price_var, trainable_vars)
+
+ def test_sparse_trainable_default(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default() as g:
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar'], indices=[[0, 0]], dense_shape=[1, 1])
+ features = {'wire_cast': wire_tensor}
+ get_keras_linear_model_predictions(features, [wire_cast])
+ trainable_vars = g.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)
+ bias = get_linear_model_bias()
+ wire_cast_var = get_linear_model_column_var(wire_cast)
+ self.assertIn(bias, trainable_vars)
+ self.assertIn(wire_cast_var, trainable_vars)
+
+ def test_dense_trainable_false(self):
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default() as g:
+ features = {'price': [[1.], [5.]]}
+ get_keras_linear_model_predictions(features, [price], trainable=False)
+ trainable_vars = g.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)
+ self.assertEqual([], trainable_vars)
+
+ def test_sparse_trainable_false(self):
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default() as g:
+ wire_tensor = sparse_tensor.SparseTensor(
+ values=['omar'], indices=[[0, 0]], dense_shape=[1, 1])
+ features = {'wire_cast': wire_tensor}
+ get_keras_linear_model_predictions(features, [wire_cast], trainable=False)
+ trainable_vars = g.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)
+ self.assertEqual([], trainable_vars)
+
+ def test_column_order(self):
+ price_a = fc_old.numeric_column('price_a')
+ price_b = fc_old.numeric_column('price_b')
+ wire_cast = fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ with ops.Graph().as_default() as g:
+ features = {
+ 'price_a': [[1.]],
+ 'price_b': [[3.]],
+ 'wire_cast':
+ sparse_tensor.SparseTensor(
+ values=['omar'], indices=[[0, 0]], dense_shape=[1, 1])
+ }
+ get_keras_linear_model_predictions(
+ features, [price_a, wire_cast, price_b],
+ weight_collections=['my-vars'])
+ my_vars = g.get_collection('my-vars')
+ self.assertIn('price_a', my_vars[0].name)
+ self.assertIn('price_b', my_vars[1].name)
+ self.assertIn('wire_cast', my_vars[2].name)
+
+ with ops.Graph().as_default() as g:
+ features = {
+ 'price_a': [[1.]],
+ 'price_b': [[3.]],
+ 'wire_cast':
+ sparse_tensor.SparseTensor(
+ values=['omar'], indices=[[0, 0]], dense_shape=[1, 1])
+ }
+ get_keras_linear_model_predictions(
+ features, [wire_cast, price_b, price_a],
+ weight_collections=['my-vars'])
+ my_vars = g.get_collection('my-vars')
+ self.assertIn('price_a', my_vars[0].name)
+ self.assertIn('price_b', my_vars[1].name)
+ self.assertIn('wire_cast', my_vars[2].name)
+
+ def test_static_batch_size_mismatch(self):
+ price1 = fc_old.numeric_column('price1')
+ price2 = fc_old.numeric_column('price2')
+ with ops.Graph().as_default():
+ features = {
+ 'price1': [[1.], [5.], [7.]], # batchsize = 3
+ 'price2': [[3.], [4.]] # batchsize = 2
+ }
+ with self.assertRaisesRegexp(
+ ValueError,
+ 'Batch size \(first dimension\) of each feature must be same.'): # pylint: disable=anomalous-backslash-in-string
+ get_keras_linear_model_predictions(features, [price1, price2])
+
+ def test_subset_of_static_batch_size_mismatch(self):
+ price1 = fc_old.numeric_column('price1')
+ price2 = fc_old.numeric_column('price2')
+ price3 = fc_old.numeric_column('price3')
+ with ops.Graph().as_default():
+ features = {
+ 'price1': array_ops.placeholder(dtype=dtypes.int64), # batchsize = 3
+ 'price2': [[3.], [4.]], # batchsize = 2
+ 'price3': [[3.], [4.], [5.]] # batchsize = 3
+ }
+ with self.assertRaisesRegexp(
+ ValueError,
+ 'Batch size \(first dimension\) of each feature must be same.'): # pylint: disable=anomalous-backslash-in-string
+ get_keras_linear_model_predictions(features, [price1, price2, price3])
+
+ def test_runtime_batch_size_mismatch(self):
+ price1 = fc_old.numeric_column('price1')
+ price2 = fc_old.numeric_column('price2')
+ with ops.Graph().as_default():
+ features = {
+ 'price1': array_ops.placeholder(dtype=dtypes.int64), # batchsize = 3
+ 'price2': [[3.], [4.]] # batchsize = 2
+ }
+ predictions = get_keras_linear_model_predictions(features,
+ [price1, price2])
+ with _initialized_session() as sess:
+ with self.assertRaisesRegexp(errors.OpError,
+ 'must have the same size and shape'):
+ sess.run(
+ predictions, feed_dict={features['price1']: [[1.], [5.], [7.]]})
+
+ def test_runtime_batch_size_matches(self):
+ price1 = fc_old.numeric_column('price1')
+ price2 = fc_old.numeric_column('price2')
+ with ops.Graph().as_default():
+ features = {
+ 'price1': array_ops.placeholder(dtype=dtypes.int64), # batchsize = 2
+ 'price2': array_ops.placeholder(dtype=dtypes.int64), # batchsize = 2
+ }
+ predictions = get_keras_linear_model_predictions(features,
+ [price1, price2])
+ with _initialized_session() as sess:
+ sess.run(
+ predictions,
+ feed_dict={
+ features['price1']: [[1.], [5.]],
+ features['price2']: [[1.], [5.]],
+ })
+
+ def test_with_numpy_input_fn(self):
+ price = fc_old.numeric_column('price')
+ price_buckets = fc_old.bucketized_column(
+ price, boundaries=[
+ 0.,
+ 10.,
+ 100.,
+ ])
+ body_style = fc_old.categorical_column_with_vocabulary_list(
+ 'body-style', vocabulary_list=['hardtop', 'wagon', 'sedan'])
+
+ input_fn = numpy_io.numpy_input_fn(
+ x={
+ 'price': np.array([-1., 2., 13., 104.]),
+ 'body-style': np.array(['sedan', 'hardtop', 'wagon', 'sedan']),
+ },
+ batch_size=2,
+ shuffle=False)
+ features = input_fn()
+ net = get_keras_linear_model_predictions(features,
+ [price_buckets, body_style])
+ # self.assertEqual(1 + 3 + 5, net.shape[1])
+ with _initialized_session() as sess:
+ coord = coordinator.Coordinator()
+ threads = queue_runner_impl.start_queue_runners(sess, coord=coord)
+
+ bias = get_linear_model_bias()
+ price_buckets_var = get_linear_model_column_var(price_buckets)
+ body_style_var = get_linear_model_column_var(body_style)
+
+ sess.run(price_buckets_var.assign([[10.], [100.], [1000.], [10000.]]))
+ sess.run(body_style_var.assign([[-10.], [-100.], [-1000.]]))
+ sess.run(bias.assign([5.]))
+
+ self.assertAllClose([[10 - 1000 + 5.], [100 - 10 + 5.]], sess.run(net))
+
+ coord.request_stop()
+ coord.join(threads)
+
+ def test_with_1d_sparse_tensor(self):
+ price = fc_old.numeric_column('price')
+ price_buckets = fc_old.bucketized_column(
+ price, boundaries=[
+ 0.,
+ 10.,
+ 100.,
+ ])
+ body_style = fc_old.categorical_column_with_vocabulary_list(
+ 'body-style', vocabulary_list=['hardtop', 'wagon', 'sedan'])
+
+ # Provides 1-dim tensor and dense tensor.
+ features = {
+ 'price':
+ constant_op.constant([
+ -1.,
+ 12.,
+ ]),
+ 'body-style':
+ sparse_tensor.SparseTensor(
+ indices=((0,), (1,)),
+ values=('sedan', 'hardtop'),
+ dense_shape=(2,)),
+ }
+ self.assertEqual(1, features['price'].shape.ndims)
+ self.assertEqual(1, features['body-style'].dense_shape.get_shape()[0])
+
+ net = get_keras_linear_model_predictions(features,
+ [price_buckets, body_style])
+ with _initialized_session() as sess:
+ bias = get_linear_model_bias()
+ price_buckets_var = get_linear_model_column_var(price_buckets)
+ body_style_var = get_linear_model_column_var(body_style)
+
+ sess.run(price_buckets_var.assign([[10.], [100.], [1000.], [10000.]]))
+ sess.run(body_style_var.assign([[-10.], [-100.], [-1000.]]))
+ sess.run(bias.assign([5.]))
+
+ self.assertAllClose([[10 - 1000 + 5.], [1000 - 10 + 5.]], sess.run(net))
+
+ def test_with_1d_unknown_shape_sparse_tensor(self):
+ price = fc_old.numeric_column('price')
+ price_buckets = fc_old.bucketized_column(
+ price, boundaries=[
+ 0.,
+ 10.,
+ 100.,
+ ])
+ body_style = fc_old.categorical_column_with_vocabulary_list(
+ 'body-style', vocabulary_list=['hardtop', 'wagon', 'sedan'])
+ country = fc_old.categorical_column_with_vocabulary_list(
+ 'country', vocabulary_list=['US', 'JP', 'CA'])
+
+ # Provides 1-dim tensor and dense tensor.
+ features = {
+ 'price': array_ops.placeholder(dtypes.float32),
+ 'body-style': array_ops.sparse_placeholder(dtypes.string),
+ 'country': array_ops.placeholder(dtypes.string),
+ }
+ self.assertIsNone(features['price'].shape.ndims)
+ self.assertIsNone(features['body-style'].get_shape().ndims)
+
+ price_data = np.array([-1., 12.])
+ body_style_data = sparse_tensor.SparseTensorValue(
+ indices=((0,), (1,)), values=('sedan', 'hardtop'), dense_shape=(2,))
+ country_data = np.array(['US', 'CA'])
+
+ net = get_keras_linear_model_predictions(
+ features, [price_buckets, body_style, country])
+ bias = get_linear_model_bias()
+ price_buckets_var = get_linear_model_column_var(price_buckets)
+ body_style_var = get_linear_model_column_var(body_style)
+ with _initialized_session() as sess:
+ sess.run(price_buckets_var.assign([[10.], [100.], [1000.], [10000.]]))
+ sess.run(body_style_var.assign([[-10.], [-100.], [-1000.]]))
+ sess.run(bias.assign([5.]))
+
+ self.assertAllClose([[10 - 1000 + 5.], [1000 - 10 + 5.]],
+ sess.run(
+ net,
+ feed_dict={
+ features['price']: price_data,
+ features['body-style']: body_style_data,
+ features['country']: country_data
+ }))
+
+ def test_with_rank_0_feature(self):
+ price = fc_old.numeric_column('price')
+ features = {
+ 'price': constant_op.constant(0),
+ }
+ self.assertEqual(0, features['price'].shape.ndims)
+
+ # Static rank 0 should fail
+ with self.assertRaisesRegexp(ValueError, 'Feature .* cannot have rank 0'):
+ get_keras_linear_model_predictions(features, [price])
+
+ # Dynamic rank 0 should fail
+ features = {
+ 'price': array_ops.placeholder(dtypes.float32),
+ }
+ net = get_keras_linear_model_predictions(features, [price])
+ self.assertEqual(1, net.shape[1])
+ with _initialized_session() as sess:
+ with self.assertRaisesOpError('Feature .* cannot have rank 0'):
+ sess.run(net, feed_dict={features['price']: np.array(1)})
+
+
+class InputLayerTest(test.TestCase):
+
+ @test_util.run_in_graph_and_eager_modes()
+ def test_retrieving_input(self):
+ features = {'a': [0.]}
+ input_layer = InputLayer(fc_old.numeric_column('a'))
+ inputs = self.evaluate(input_layer(features))
+ self.assertAllClose([[0.]], inputs)
+
+ def test_reuses_variables(self):
+ with context.eager_mode():
+ sparse_input = sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (2, 0)),
+ values=(0, 1, 2),
+ dense_shape=(3, 3))
+
+ # Create feature columns (categorical and embedding).
+ categorical_column = fc_old.categorical_column_with_identity(
+ key='a', num_buckets=3)
+ embedding_dimension = 2
+ def _embedding_column_initializer(shape, dtype, partition_info):
+ del shape # unused
+ del dtype # unused
+ del partition_info # unused
+ embedding_values = (
+ (1, 0), # id 0
+ (0, 1), # id 1
+ (1, 1)) # id 2
+ return embedding_values
+
+ embedding_column = fc_old.embedding_column(
+ categorical_column,
+ dimension=embedding_dimension,
+ initializer=_embedding_column_initializer)
+
+ input_layer = InputLayer([embedding_column])
+ features = {'a': sparse_input}
+
+ inputs = input_layer(features)
+ variables = input_layer.variables
+
+ # Sanity check: test that the inputs are correct.
+ self.assertAllEqual([[1, 0], [0, 1], [1, 1]], inputs)
+
+ # Check that only one variable was created.
+ self.assertEqual(1, len(variables))
+
+ # Check that invoking input_layer on the same features does not create
+ # additional variables
+ _ = input_layer(features)
+ self.assertEqual(1, len(variables))
+ self.assertEqual(variables[0], input_layer.variables[0])
+
+ def test_feature_column_input_layer_gradient(self):
+ with context.eager_mode():
+ sparse_input = sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (2, 0)),
+ values=(0, 1, 2),
+ dense_shape=(3, 3))
+
+ # Create feature columns (categorical and embedding).
+ categorical_column = fc_old.categorical_column_with_identity(
+ key='a', num_buckets=3)
+ embedding_dimension = 2
+
+ def _embedding_column_initializer(shape, dtype, partition_info):
+ del shape # unused
+ del dtype # unused
+ del partition_info # unused
+ embedding_values = (
+ (1, 0), # id 0
+ (0, 1), # id 1
+ (1, 1)) # id 2
+ return embedding_values
+
+ embedding_column = fc_old.embedding_column(
+ categorical_column,
+ dimension=embedding_dimension,
+ initializer=_embedding_column_initializer)
+
+ input_layer = InputLayer([embedding_column])
+ features = {'a': sparse_input}
+
+ def scale_matrix():
+ matrix = input_layer(features)
+ return 2 * matrix
+
+ # Sanity check: Verify that scale_matrix returns the correct output.
+ self.assertAllEqual([[2, 0], [0, 2], [2, 2]], scale_matrix())
+
+ # Check that the returned gradient is correct.
+ grad_function = backprop.implicit_grad(scale_matrix)
+ grads_and_vars = grad_function()
+ indexed_slice = grads_and_vars[0][0]
+ gradient = grads_and_vars[0][0].values
+
+ self.assertAllEqual([0, 1, 2], indexed_slice.indices)
+ self.assertAllEqual([[2, 2], [2, 2], [2, 2]], gradient)
+
+
+class FunctionalInputLayerTest(test.TestCase):
+
+ def test_raises_if_empty_feature_columns(self):
+ with self.assertRaisesRegexp(ValueError,
+ 'feature_columns must not be empty'):
+ fc.input_layer(features={}, feature_columns=[])
+
+ def test_should_be_dense_column(self):
+ with self.assertRaisesRegexp(ValueError, 'must be a _DenseColumn'):
+ fc.input_layer(
+ features={'a': [[0]]},
+ feature_columns=[
+ fc_old.categorical_column_with_hash_bucket('wire_cast', 4)
+ ])
+
+ def test_does_not_support_dict_columns(self):
+ with self.assertRaisesRegexp(
+ ValueError, 'Expected feature_columns to be iterable, found dict.'):
+ fc.input_layer(
+ features={'a': [[0]]},
+ feature_columns={'a': fc_old.numeric_column('a')})
+
+ def test_bare_column(self):
+ with ops.Graph().as_default():
+ features = features = {'a': [0.]}
+ net = fc.input_layer(features, fc_old.numeric_column('a'))
+ with _initialized_session():
+ self.assertAllClose([[0.]], net.eval())
+
+ def test_column_generator(self):
+ with ops.Graph().as_default():
+ features = features = {'a': [0.], 'b': [1.]}
+ columns = (fc_old.numeric_column(key) for key in features)
+ net = fc.input_layer(features, columns)
+ with _initialized_session():
+ self.assertAllClose([[0., 1.]], net.eval())
+
+ def test_raises_if_duplicate_name(self):
+ with self.assertRaisesRegexp(
+ ValueError, 'Duplicate feature column name found for columns'):
+ fc.input_layer(
+ features={'a': [[0]]},
+ feature_columns=[
+ fc_old.numeric_column('a'),
+ fc_old.numeric_column('a')
+ ])
+
+ def test_one_column(self):
+ price = fc_old.numeric_column('price')
+ with ops.Graph().as_default():
+ features = {'price': [[1.], [5.]]}
+ net = fc.input_layer(features, [price])
+ with _initialized_session():
+ self.assertAllClose([[1.], [5.]], net.eval())
+
+ def test_multi_dimension(self):
+ price = fc_old.numeric_column('price', shape=2)
+ with ops.Graph().as_default():
+ features = {'price': [[1., 2.], [5., 6.]]}
+ net = fc.input_layer(features, [price])
+ with _initialized_session():
+ self.assertAllClose([[1., 2.], [5., 6.]], net.eval())
+
+ def test_raises_if_shape_mismatch(self):
+ price = fc_old.numeric_column('price', shape=2)
+ with ops.Graph().as_default():
+ features = {'price': [[1.], [5.]]}
+ with self.assertRaisesRegexp(
+ Exception,
+ r'Cannot reshape a tensor with 2 elements to shape \[2,2\]'):
+ fc.input_layer(features, [price])
+
+ def test_reshaping(self):
+ price = fc_old.numeric_column('price', shape=[1, 2])
+ with ops.Graph().as_default():
+ features = {'price': [[[1., 2.]], [[5., 6.]]]}
+ net = fc.input_layer(features, [price])
+ with _initialized_session():
+ self.assertAllClose([[1., 2.], [5., 6.]], net.eval())
+
+ def test_multi_column(self):
+ price1 = fc_old.numeric_column('price1', shape=2)
+ price2 = fc_old.numeric_column('price2')
+ with ops.Graph().as_default():
+ features = {
+ 'price1': [[1., 2.], [5., 6.]],
+ 'price2': [[3.], [4.]]
+ }
+ net = fc.input_layer(features, [price1, price2])
+ with _initialized_session():
+ self.assertAllClose([[1., 2., 3.], [5., 6., 4.]], net.eval())
+
+ def test_fills_cols_to_vars(self):
+ # Provide three _DenseColumn's to input_layer: a _NumericColumn, a
+ # _BucketizedColumn, and an _EmbeddingColumn. Only the _EmbeddingColumn
+ # creates a Variable.
+ price1 = fc_old.numeric_column('price1')
+ dense_feature = fc_old.numeric_column('dense_feature')
+ dense_feature_bucketized = fc_old.bucketized_column(
+ dense_feature, boundaries=[0.])
+ some_sparse_column = fc_old.categorical_column_with_hash_bucket(
+ 'sparse_feature', hash_bucket_size=5)
+ some_embedding_column = fc_old.embedding_column(
+ some_sparse_column, dimension=10)
+ with ops.Graph().as_default():
+ features = {
+ 'price1': [[3.], [4.]],
+ 'dense_feature': [[-1.], [4.]],
+ 'sparse_feature': [['a'], ['x']],
+ }
+ cols_to_vars = {}
+ all_cols = [price1, dense_feature_bucketized, some_embedding_column]
+ fc.input_layer(features, all_cols, cols_to_vars=cols_to_vars)
+ self.assertItemsEqual(list(cols_to_vars.keys()), all_cols)
+ self.assertEqual(0, len(cols_to_vars[price1]))
+ self.assertEqual(0, len(cols_to_vars[dense_feature_bucketized]))
+ self.assertEqual(1, len(cols_to_vars[some_embedding_column]))
+ self.assertIsInstance(cols_to_vars[some_embedding_column][0],
+ variables_lib.Variable)
+ self.assertAllEqual(cols_to_vars[some_embedding_column][0].shape, [5, 10])
+
+ def test_fills_cols_to_vars_partitioned_variables(self):
+ price1 = fc_old.numeric_column('price1')
+ dense_feature = fc_old.numeric_column('dense_feature')
+ dense_feature_bucketized = fc_old.bucketized_column(
+ dense_feature, boundaries=[0.])
+ some_sparse_column = fc_old.categorical_column_with_hash_bucket(
+ 'sparse_feature', hash_bucket_size=5)
+ some_embedding_column = fc_old.embedding_column(
+ some_sparse_column, dimension=10)
+ with ops.Graph().as_default():
+ features = {
+ 'price1': [[3.], [4.]],
+ 'dense_feature': [[-1.], [4.]],
+ 'sparse_feature': [['a'], ['x']],
+ }
+ cols_to_vars = {}
+ all_cols = [price1, dense_feature_bucketized, some_embedding_column]
+ with variable_scope.variable_scope(
+ 'input_from_feature_columns',
+ partitioner=partitioned_variables.fixed_size_partitioner(3, axis=0)):
+ fc.input_layer(features, all_cols, cols_to_vars=cols_to_vars)
+ self.assertItemsEqual(list(cols_to_vars.keys()), all_cols)
+ self.assertEqual(0, len(cols_to_vars[price1]))
+ self.assertEqual(0, len(cols_to_vars[dense_feature_bucketized]))
+ self.assertEqual(3, len(cols_to_vars[some_embedding_column]))
+ self.assertAllEqual(cols_to_vars[some_embedding_column][0].shape, [2, 10])
+ self.assertAllEqual(cols_to_vars[some_embedding_column][1].shape, [2, 10])
+ self.assertAllEqual(cols_to_vars[some_embedding_column][2].shape, [1, 10])
+
+ def test_column_order(self):
+ price_a = fc_old.numeric_column('price_a')
+ price_b = fc_old.numeric_column('price_b')
+ with ops.Graph().as_default():
+ features = {
+ 'price_a': [[1.]],
+ 'price_b': [[3.]],
+ }
+ net1 = fc.input_layer(features, [price_a, price_b])
+ net2 = fc.input_layer(features, [price_b, price_a])
+ with _initialized_session():
+ self.assertAllClose([[1., 3.]], net1.eval())
+ self.assertAllClose([[1., 3.]], net2.eval())
+
+ def test_fails_for_categorical_column(self):
+ animal = fc_old.categorical_column_with_identity('animal', num_buckets=4)
+ with ops.Graph().as_default():
+ features = {
+ 'animal':
+ sparse_tensor.SparseTensor(
+ indices=[[0, 0], [0, 1]], values=[1, 2], dense_shape=[1, 2])
+ }
+ with self.assertRaisesRegexp(Exception, 'must be a _DenseColumn'):
+ fc.input_layer(features, [animal])
+
+ def test_static_batch_size_mismatch(self):
+ price1 = fc_old.numeric_column('price1')
+ price2 = fc_old.numeric_column('price2')
+ with ops.Graph().as_default():
+ features = {
+ 'price1': [[1.], [5.], [7.]], # batchsize = 3
+ 'price2': [[3.], [4.]] # batchsize = 2
+ }
+ with self.assertRaisesRegexp(
+ ValueError,
+ 'Batch size \(first dimension\) of each feature must be same.'): # pylint: disable=anomalous-backslash-in-string
+ fc.input_layer(features, [price1, price2])
+
+ def test_subset_of_static_batch_size_mismatch(self):
+ price1 = fc_old.numeric_column('price1')
+ price2 = fc_old.numeric_column('price2')
+ price3 = fc_old.numeric_column('price3')
+ with ops.Graph().as_default():
+ features = {
+ 'price1': array_ops.placeholder(dtype=dtypes.int64), # batchsize = 3
+ 'price2': [[3.], [4.]], # batchsize = 2
+ 'price3': [[3.], [4.], [5.]] # batchsize = 3
+ }
+ with self.assertRaisesRegexp(
+ ValueError,
+ 'Batch size \(first dimension\) of each feature must be same.'): # pylint: disable=anomalous-backslash-in-string
+ fc.input_layer(features, [price1, price2, price3])
+
+ def test_runtime_batch_size_mismatch(self):
+ price1 = fc_old.numeric_column('price1')
+ price2 = fc_old.numeric_column('price2')
+ with ops.Graph().as_default():
+ features = {
+ 'price1': array_ops.placeholder(dtype=dtypes.int64), # batchsize = 3
+ 'price2': [[3.], [4.]] # batchsize = 2
+ }
+ net = fc.input_layer(features, [price1, price2])
+ with _initialized_session() as sess:
+ with self.assertRaisesRegexp(errors.OpError,
+ 'Dimensions of inputs should match'):
+ sess.run(net, feed_dict={features['price1']: [[1.], [5.], [7.]]})
+
+ def test_runtime_batch_size_matches(self):
+ price1 = fc_old.numeric_column('price1')
+ price2 = fc_old.numeric_column('price2')
+ with ops.Graph().as_default():
+ features = {
+ 'price1': array_ops.placeholder(dtype=dtypes.int64), # batchsize = 2
+ 'price2': array_ops.placeholder(dtype=dtypes.int64), # batchsize = 2
+ }
+ net = fc.input_layer(features, [price1, price2])
+ with _initialized_session() as sess:
+ sess.run(
+ net,
+ feed_dict={
+ features['price1']: [[1.], [5.]],
+ features['price2']: [[1.], [5.]],
+ })
+
+ def test_multiple_layers_with_same_embedding_column(self):
+ some_sparse_column = fc_old.categorical_column_with_hash_bucket(
+ 'sparse_feature', hash_bucket_size=5)
+ some_embedding_column = fc_old.embedding_column(
+ some_sparse_column, dimension=10)
+
+ with ops.Graph().as_default():
+ features = {
+ 'sparse_feature': [['a'], ['x']],
+ }
+ all_cols = [some_embedding_column]
+ fc.input_layer(features, all_cols)
+ fc.input_layer(features, all_cols)
+ # Make sure that 2 variables get created in this case.
+ self.assertEqual(2, len(
+ ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)))
+ expected_var_names = [
+ 'input_layer/sparse_feature_embedding/embedding_weights:0',
+ 'input_layer_1/sparse_feature_embedding/embedding_weights:0'
+ ]
+ self.assertItemsEqual(
+ expected_var_names,
+ [v.name for v in ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)])
+
+ def test_multiple_layers_with_same_shared_embedding_column(self):
+ categorical_column_a = fc_old.categorical_column_with_identity(
+ key='aaa', num_buckets=3)
+ categorical_column_b = fc_old.categorical_column_with_identity(
+ key='bbb', num_buckets=3)
+ embedding_dimension = 2
+ embedding_column_b, embedding_column_a = fc_old.shared_embedding_columns(
+ [categorical_column_b, categorical_column_a],
+ dimension=embedding_dimension)
+
+ with ops.Graph().as_default():
+ features = {
+ 'aaa':
+ sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 1, 0),
+ dense_shape=(2, 2)),
+ 'bbb':
+ sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(1, 2, 1),
+ dense_shape=(2, 2)),
+ }
+ all_cols = [embedding_column_a, embedding_column_b]
+ fc.input_layer(features, all_cols)
+ fc.input_layer(features, all_cols)
+ # Make sure that only 1 variable gets created in this case.
+ self.assertEqual(1, len(
+ ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)))
+ self.assertItemsEqual(
+ ['input_layer/aaa_bbb_shared_embedding/embedding_weights:0'],
+ [v.name for v in ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)])
+
+ def test_multiple_layers_with_same_shared_embedding_column_diff_graphs(self):
+ categorical_column_a = fc_old.categorical_column_with_identity(
+ key='aaa', num_buckets=3)
+ categorical_column_b = fc_old.categorical_column_with_identity(
+ key='bbb', num_buckets=3)
+ embedding_dimension = 2
+ embedding_column_b, embedding_column_a = fc_old.shared_embedding_columns(
+ [categorical_column_b, categorical_column_a],
+ dimension=embedding_dimension)
+ all_cols = [embedding_column_a, embedding_column_b]
+
+ with ops.Graph().as_default():
+ features = {
+ 'aaa':
+ sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 1, 0),
+ dense_shape=(2, 2)),
+ 'bbb':
+ sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(1, 2, 1),
+ dense_shape=(2, 2)),
+ }
+ fc.input_layer(features, all_cols)
+ # Make sure that only 1 variable gets created in this case.
+ self.assertEqual(1, len(
+ ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)))
+
+ with ops.Graph().as_default():
+ features1 = {
+ 'aaa':
+ sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 1, 0),
+ dense_shape=(2, 2)),
+ 'bbb':
+ sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(1, 2, 1),
+ dense_shape=(2, 2)),
+ }
+
+ fc.input_layer(features1, all_cols)
+ # Make sure that only 1 variable gets created in this case.
+ self.assertEqual(1, len(
+ ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)))
+ self.assertItemsEqual(
+ ['input_layer/aaa_bbb_shared_embedding/embedding_weights:0'],
+ [v.name for v in ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)])
+
+ def test_with_numpy_input_fn(self):
+ embedding_values = (
+ (1., 2., 3., 4., 5.), # id 0
+ (6., 7., 8., 9., 10.), # id 1
+ (11., 12., 13., 14., 15.) # id 2
+ )
+ def _initializer(shape, dtype, partition_info):
+ del shape, dtype, partition_info
+ return embedding_values
+
+ # price has 1 dimension in input_layer
+ price = fc_old.numeric_column('price')
+ body_style = fc_old.categorical_column_with_vocabulary_list(
+ 'body-style', vocabulary_list=['hardtop', 'wagon', 'sedan'])
+ # one_hot_body_style has 3 dims in input_layer.
+ one_hot_body_style = fc_old.indicator_column(body_style)
+ # embedded_body_style has 5 dims in input_layer.
+ embedded_body_style = fc_old.embedding_column(
+ body_style, dimension=5, initializer=_initializer)
+
+ input_fn = numpy_io.numpy_input_fn(
+ x={
+ 'price': np.array([11., 12., 13., 14.]),
+ 'body-style': np.array(['sedan', 'hardtop', 'wagon', 'sedan']),
+ },
+ batch_size=2,
+ shuffle=False)
+ features = input_fn()
+ net = fc.input_layer(features,
+ [price, one_hot_body_style, embedded_body_style])
+ self.assertEqual(1 + 3 + 5, net.shape[1])
+ with _initialized_session() as sess:
+ coord = coordinator.Coordinator()
+ threads = queue_runner_impl.start_queue_runners(sess, coord=coord)
+
+ # Each row is formed by concatenating `embedded_body_style`,
+ # `one_hot_body_style`, and `price` in order.
+ self.assertAllEqual(
+ [[11., 12., 13., 14., 15., 0., 0., 1., 11.],
+ [1., 2., 3., 4., 5., 1., 0., 0., 12]],
+ sess.run(net))
+
+ coord.request_stop()
+ coord.join(threads)
+
+ def test_with_1d_sparse_tensor(self):
+ embedding_values = (
+ (1., 2., 3., 4., 5.), # id 0
+ (6., 7., 8., 9., 10.), # id 1
+ (11., 12., 13., 14., 15.) # id 2
+ )
+ def _initializer(shape, dtype, partition_info):
+ del shape, dtype, partition_info
+ return embedding_values
+
+ # price has 1 dimension in input_layer
+ price = fc_old.numeric_column('price')
+
+ # one_hot_body_style has 3 dims in input_layer.
+ body_style = fc_old.categorical_column_with_vocabulary_list(
+ 'body-style', vocabulary_list=['hardtop', 'wagon', 'sedan'])
+ one_hot_body_style = fc_old.indicator_column(body_style)
+
+ # embedded_body_style has 5 dims in input_layer.
+ country = fc_old.categorical_column_with_vocabulary_list(
+ 'country', vocabulary_list=['US', 'JP', 'CA'])
+ embedded_country = fc_old.embedding_column(
+ country, dimension=5, initializer=_initializer)
+
+ # Provides 1-dim tensor and dense tensor.
+ features = {
+ 'price': constant_op.constant([11., 12.,]),
+ 'body-style': sparse_tensor.SparseTensor(
+ indices=((0,), (1,)),
+ values=('sedan', 'hardtop'),
+ dense_shape=(2,)),
+ # This is dense tensor for the categorical_column.
+ 'country': constant_op.constant(['CA', 'US']),
+ }
+ self.assertEqual(1, features['price'].shape.ndims)
+ self.assertEqual(1, features['body-style'].dense_shape.get_shape()[0])
+ self.assertEqual(1, features['country'].shape.ndims)
+
+ net = fc.input_layer(features,
+ [price, one_hot_body_style, embedded_country])
+ self.assertEqual(1 + 3 + 5, net.shape[1])
+ with _initialized_session() as sess:
+
+ # Each row is formed by concatenating `embedded_body_style`,
+ # `one_hot_body_style`, and `price` in order.
+ self.assertAllEqual(
+ [[0., 0., 1., 11., 12., 13., 14., 15., 11.],
+ [1., 0., 0., 1., 2., 3., 4., 5., 12.]],
+ sess.run(net))
+
+ def test_with_1d_unknown_shape_sparse_tensor(self):
+ embedding_values = (
+ (1., 2.), # id 0
+ (6., 7.), # id 1
+ (11., 12.) # id 2
+ )
+ def _initializer(shape, dtype, partition_info):
+ del shape, dtype, partition_info
+ return embedding_values
+
+ # price has 1 dimension in input_layer
+ price = fc_old.numeric_column('price')
+
+ # one_hot_body_style has 3 dims in input_layer.
+ body_style = fc_old.categorical_column_with_vocabulary_list(
+ 'body-style', vocabulary_list=['hardtop', 'wagon', 'sedan'])
+ one_hot_body_style = fc_old.indicator_column(body_style)
+
+ # embedded_body_style has 5 dims in input_layer.
+ country = fc_old.categorical_column_with_vocabulary_list(
+ 'country', vocabulary_list=['US', 'JP', 'CA'])
+ embedded_country = fc_old.embedding_column(
+ country, dimension=2, initializer=_initializer)
+
+ # Provides 1-dim tensor and dense tensor.
+ features = {
+ 'price': array_ops.placeholder(dtypes.float32),
+ 'body-style': array_ops.sparse_placeholder(dtypes.string),
+ # This is dense tensor for the categorical_column.
+ 'country': array_ops.placeholder(dtypes.string),
+ }
+ self.assertIsNone(features['price'].shape.ndims)
+ self.assertIsNone(features['body-style'].get_shape().ndims)
+ self.assertIsNone(features['country'].shape.ndims)
+
+ price_data = np.array([11., 12.])
+ body_style_data = sparse_tensor.SparseTensorValue(
+ indices=((0,), (1,)),
+ values=('sedan', 'hardtop'),
+ dense_shape=(2,))
+ country_data = np.array([['US'], ['CA']])
+
+ net = fc.input_layer(features,
+ [price, one_hot_body_style, embedded_country])
+ self.assertEqual(1 + 3 + 2, net.shape[1])
+ with _initialized_session() as sess:
+
+ # Each row is formed by concatenating `embedded_body_style`,
+ # `one_hot_body_style`, and `price` in order.
+ self.assertAllEqual(
+ [[0., 0., 1., 1., 2., 11.], [1., 0., 0., 11., 12., 12.]],
+ sess.run(
+ net,
+ feed_dict={
+ features['price']: price_data,
+ features['body-style']: body_style_data,
+ features['country']: country_data
+ }))
+
+ def test_with_rank_0_feature(self):
+ # price has 1 dimension in input_layer
+ price = fc_old.numeric_column('price')
+ features = {
+ 'price': constant_op.constant(0),
+ }
+ self.assertEqual(0, features['price'].shape.ndims)
+
+ # Static rank 0 should fail
+ with self.assertRaisesRegexp(ValueError, 'Feature .* cannot have rank 0'):
+ fc.input_layer(features, [price])
+
+ # Dynamic rank 0 should fail
+ features = {
+ 'price': array_ops.placeholder(dtypes.float32),
+ }
+ net = fc.input_layer(features, [price])
+ self.assertEqual(1, net.shape[1])
+ with _initialized_session() as sess:
+ with self.assertRaisesOpError('Feature .* cannot have rank 0'):
+ sess.run(net, feed_dict={features['price']: np.array(1)})
+
+
+class MakeParseExampleSpecTest(test.TestCase):
+
+ class _TestFeatureColumn(FeatureColumn,
+ collections.namedtuple('_TestFeatureColumn',
+ ('parse_spec'))):
+
+ @property
+ def name(self):
+ return "_TestFeatureColumn"
+
+ def transform_feature(self, transformation_cache, state_manager):
+ pass
+
+ @property
+ def parse_example_spec(self):
+ return self.parse_spec
+
+ def test_no_feature_columns(self):
+ actual = fc.make_parse_example_spec([])
+ self.assertDictEqual({}, actual)
+
+ def test_invalid_type(self):
+ key1 = 'key1'
+ parse_spec1 = parsing_ops.FixedLenFeature(
+ shape=(2,), dtype=dtypes.float32, default_value=0.)
+ with self.assertRaisesRegexp(
+ ValueError,
+ 'All feature_columns must be FeatureColumn instances.*invalid_column'):
+ fc.make_parse_example_spec(
+ (self._TestFeatureColumn({key1: parse_spec1}), 'invalid_column'))
+
+ def test_one_feature_column(self):
+ key1 = 'key1'
+ parse_spec1 = parsing_ops.FixedLenFeature(
+ shape=(2,), dtype=dtypes.float32, default_value=0.)
+ actual = fc.make_parse_example_spec(
+ (self._TestFeatureColumn({key1: parse_spec1}),))
+ self.assertDictEqual({key1: parse_spec1}, actual)
+
+ def test_two_feature_columns(self):
+ key1 = 'key1'
+ parse_spec1 = parsing_ops.FixedLenFeature(
+ shape=(2,), dtype=dtypes.float32, default_value=0.)
+ key2 = 'key2'
+ parse_spec2 = parsing_ops.VarLenFeature(dtype=dtypes.string)
+ actual = fc.make_parse_example_spec(
+ (self._TestFeatureColumn({key1: parse_spec1}),
+ self._TestFeatureColumn({key2: parse_spec2})))
+ self.assertDictEqual({key1: parse_spec1, key2: parse_spec2}, actual)
+
+ def test_equal_keys_different_parse_spec(self):
+ key1 = 'key1'
+ parse_spec1 = parsing_ops.FixedLenFeature(
+ shape=(2,), dtype=dtypes.float32, default_value=0.)
+ parse_spec2 = parsing_ops.VarLenFeature(dtype=dtypes.string)
+ with self.assertRaisesRegexp(
+ ValueError,
+ 'feature_columns contain different parse_spec for key key1'):
+ fc.make_parse_example_spec(
+ (self._TestFeatureColumn({key1: parse_spec1}),
+ self._TestFeatureColumn({key1: parse_spec2})))
+
+ def test_equal_keys_equal_parse_spec(self):
+ key1 = 'key1'
+ parse_spec1 = parsing_ops.FixedLenFeature(
+ shape=(2,), dtype=dtypes.float32, default_value=0.)
+ actual = fc.make_parse_example_spec(
+ (self._TestFeatureColumn({key1: parse_spec1}),
+ self._TestFeatureColumn({key1: parse_spec1})))
+ self.assertDictEqual({key1: parse_spec1}, actual)
+
+ def test_multiple_features_dict(self):
+ """parse_spc for one column is a dict with length > 1."""
+ key1 = 'key1'
+ parse_spec1 = parsing_ops.FixedLenFeature(
+ shape=(2,), dtype=dtypes.float32, default_value=0.)
+ key2 = 'key2'
+ parse_spec2 = parsing_ops.VarLenFeature(dtype=dtypes.string)
+ key3 = 'key3'
+ parse_spec3 = parsing_ops.VarLenFeature(dtype=dtypes.int32)
+ actual = fc.make_parse_example_spec(
+ (self._TestFeatureColumn({key1: parse_spec1}),
+ self._TestFeatureColumn({key2: parse_spec2, key3: parse_spec3})))
+ self.assertDictEqual(
+ {key1: parse_spec1, key2: parse_spec2, key3: parse_spec3}, actual)
+
+
+def _assert_sparse_tensor_value(test_case, expected, actual):
+ test_case.assertEqual(np.int64, np.array(actual.indices).dtype)
+ test_case.assertAllEqual(expected.indices, actual.indices)
+
+ test_case.assertEqual(
+ np.array(expected.values).dtype, np.array(actual.values).dtype)
+ test_case.assertAllEqual(expected.values, actual.values)
+
+ test_case.assertEqual(np.int64, np.array(actual.dense_shape).dtype)
+ test_case.assertAllEqual(expected.dense_shape, actual.dense_shape)
+
+
+class VocabularyFileCategoricalColumnTest(test.TestCase):
+
+ def setUp(self):
+ super(VocabularyFileCategoricalColumnTest, self).setUp()
+
+ # Contains ints, Golden State Warriors jersey numbers: 30, 35, 11, 23, 22
+ self._warriors_vocabulary_file_name = test.test_src_dir_path(
+ 'python/feature_column/testdata/warriors_vocabulary.txt')
+ self._warriors_vocabulary_size = 5
+
+ # Contains strings, character names from 'The Wire': omar, stringer, marlo
+ self._wire_vocabulary_file_name = test.test_src_dir_path(
+ 'python/feature_column/testdata/wire_vocabulary.txt')
+ self._wire_vocabulary_size = 3
+
+ def test_defaults(self):
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa', vocabulary_file='path_to_file', vocabulary_size=3)
+ self.assertEqual('aaa', column.name)
+ self.assertEqual('aaa', column.key)
+ self.assertEqual(3, column.num_buckets)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.string)
+ }, column.parse_example_spec)
+
+ def test_key_should_be_string(self):
+ with self.assertRaisesRegexp(ValueError, 'key must be a string.'):
+ fc.categorical_column_with_vocabulary_file(
+ key=('aaa',), vocabulary_file='path_to_file', vocabulary_size=3)
+
+ def test_all_constructor_args(self):
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa', vocabulary_file='path_to_file', vocabulary_size=3,
+ num_oov_buckets=4, dtype=dtypes.int32)
+ self.assertEqual(7, column.num_buckets)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int32)
+ }, column.parse_example_spec)
+
+ def test_deep_copy(self):
+ original = fc.categorical_column_with_vocabulary_file(
+ key='aaa', vocabulary_file='path_to_file', vocabulary_size=3,
+ num_oov_buckets=4, dtype=dtypes.int32)
+ for column in (original, copy.deepcopy(original)):
+ self.assertEqual('aaa', column.name)
+ self.assertEqual(7, column.num_buckets)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int32)
+ }, column.parse_example_spec)
+
+ def test_vocabulary_file_none(self):
+ with self.assertRaisesRegexp(ValueError, 'Missing vocabulary_file'):
+ fc.categorical_column_with_vocabulary_file(
+ key='aaa', vocabulary_file=None, vocabulary_size=3)
+
+ def test_vocabulary_file_empty_string(self):
+ with self.assertRaisesRegexp(ValueError, 'Missing vocabulary_file'):
+ fc.categorical_column_with_vocabulary_file(
+ key='aaa', vocabulary_file='', vocabulary_size=3)
+
+ def test_invalid_vocabulary_file(self):
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa', vocabulary_file='file_does_not_exist', vocabulary_size=10)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ column.get_sparse_tensors(FeatureTransformationCache({'aaa': inputs}), None)
+ with self.assertRaisesRegexp(errors.OpError, 'file_does_not_exist'):
+ with self.test_session():
+ lookup_ops.tables_initializer().run()
+
+ def test_invalid_vocabulary_size(self):
+ with self.assertRaisesRegexp(ValueError, 'Invalid vocabulary_size'):
+ fc.categorical_column_with_vocabulary_file(
+ key='aaa', vocabulary_file=self._wire_vocabulary_file_name,
+ vocabulary_size=-1)
+ with self.assertRaisesRegexp(ValueError, 'Invalid vocabulary_size'):
+ fc.categorical_column_with_vocabulary_file(
+ key='aaa', vocabulary_file=self._wire_vocabulary_file_name,
+ vocabulary_size=0)
+
+ def test_too_large_vocabulary_size(self):
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa',
+ vocabulary_file=self._wire_vocabulary_file_name,
+ vocabulary_size=self._wire_vocabulary_size + 1)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ column.get_sparse_tensors(FeatureTransformationCache({'aaa': inputs}), None)
+ with self.assertRaisesRegexp(errors.OpError, 'Invalid vocab_size'):
+ with self.test_session():
+ lookup_ops.tables_initializer().run()
+
+ def test_invalid_num_oov_buckets(self):
+ with self.assertRaisesRegexp(ValueError, 'Invalid num_oov_buckets'):
+ fc.categorical_column_with_vocabulary_file(
+ key='aaa', vocabulary_file='path', vocabulary_size=3,
+ num_oov_buckets=-1)
+
+ def test_invalid_dtype(self):
+ with self.assertRaisesRegexp(ValueError, 'dtype must be string or integer'):
+ fc.categorical_column_with_vocabulary_file(
+ key='aaa', vocabulary_file='path', vocabulary_size=3,
+ dtype=dtypes.float64)
+
+ def test_invalid_buckets_and_default_value(self):
+ with self.assertRaisesRegexp(
+ ValueError, 'both num_oov_buckets and default_value'):
+ fc.categorical_column_with_vocabulary_file(
+ key='aaa',
+ vocabulary_file=self._wire_vocabulary_file_name,
+ vocabulary_size=self._wire_vocabulary_size,
+ num_oov_buckets=100,
+ default_value=2)
+
+ def test_invalid_input_dtype_int32(self):
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa',
+ vocabulary_file=self._wire_vocabulary_file_name,
+ vocabulary_size=self._wire_vocabulary_size,
+ dtype=dtypes.string)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(12, 24, 36),
+ dense_shape=(2, 2))
+ with self.assertRaisesRegexp(ValueError, 'dtype must be compatible'):
+ column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+
+ def test_invalid_input_dtype_string(self):
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa',
+ vocabulary_file=self._warriors_vocabulary_file_name,
+ vocabulary_size=self._warriors_vocabulary_size,
+ dtype=dtypes.int32)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('omar', 'stringer', 'marlo'),
+ dense_shape=(2, 2))
+ with self.assertRaisesRegexp(ValueError, 'dtype must be compatible'):
+ column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+
+ def test_parse_example(self):
+ a = fc.categorical_column_with_vocabulary_file(
+ key='aaa', vocabulary_file='path_to_file', vocabulary_size=3)
+ data = example_pb2.Example(features=feature_pb2.Features(
+ feature={
+ 'aaa':
+ feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
+ value=[b'omar', b'stringer']))
+ }))
+ features = parsing_ops.parse_example(
+ serialized=[data.SerializeToString()],
+ features=fc.make_parse_example_spec([a]))
+ self.assertIn('aaa', features)
+ with self.test_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=[[0, 0], [0, 1]],
+ values=np.array([b'omar', b'stringer'], dtype=np.object_),
+ dense_shape=[1, 2]),
+ features['aaa'].eval())
+
+ def test_get_sparse_tensors(self):
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa',
+ vocabulary_file=self._wire_vocabulary_file_name,
+ vocabulary_size=self._wire_vocabulary_size)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array((2, -1, 0), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_weight_pair.id_tensor.eval())
+
+ def test_get_sparse_tensors_none_vocabulary_size(self):
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa', vocabulary_file=self._wire_vocabulary_file_name)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array(
+ (2, -1, 0), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_weight_pair.id_tensor.eval())
+
+ def test_transform_feature(self):
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa',
+ vocabulary_file=self._wire_vocabulary_file_name,
+ vocabulary_size=self._wire_vocabulary_size)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ id_tensor = _transform_features({'aaa': inputs}, [column], None)[column]
+ with _initialized_session():
+ _assert_sparse_tensor_value(self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array(
+ (2, -1, 0), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_tensor.eval())
+
+ def DISABLED_test_get_sparse_tensors_weight_collections(self):
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa',
+ vocabulary_file=self._wire_vocabulary_file_name,
+ vocabulary_size=self._wire_vocabulary_size)
+ inputs = sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'],
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }),
+ weight_collections=('my_weights',))
+
+ self.assertItemsEqual(
+ [], ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES))
+ self.assertItemsEqual([], ops.get_collection('my_weights'))
+
+ def test_get_sparse_tensors_dense_input(self):
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa',
+ vocabulary_file=self._wire_vocabulary_file_name,
+ vocabulary_size=self._wire_vocabulary_size)
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': (('marlo', ''), ('skywalker', 'omar'))
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=np.array((2, -1, 0), dtype=np.int64),
+ dense_shape=(2, 2)),
+ id_weight_pair.id_tensor.eval())
+
+ def test_get_sparse_tensors_default_value_in_vocabulary(self):
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa',
+ vocabulary_file=self._wire_vocabulary_file_name,
+ vocabulary_size=self._wire_vocabulary_size,
+ default_value=2)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array((2, 2, 0), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_weight_pair.id_tensor.eval())
+
+ def test_get_sparse_tensors_with_oov_buckets(self):
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa',
+ vocabulary_file=self._wire_vocabulary_file_name,
+ vocabulary_size=self._wire_vocabulary_size,
+ num_oov_buckets=100)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1), (1, 2)),
+ values=('marlo', 'skywalker', 'omar', 'heisenberg'),
+ dense_shape=(2, 3))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array((2, 33, 0, 62), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_weight_pair.id_tensor.eval())
+
+ def test_get_sparse_tensors_small_vocabulary_size(self):
+ # 'marlo' is the last entry in our vocabulary file, so be setting
+ # `vocabulary_size` to 1 less than number of entries in file, we take
+ # 'marlo' out of the vocabulary.
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa',
+ vocabulary_file=self._wire_vocabulary_file_name,
+ vocabulary_size=self._wire_vocabulary_size - 1)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array((-1, -1, 0), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_weight_pair.id_tensor.eval())
+
+ def test_get_sparse_tensors_int32(self):
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa',
+ vocabulary_file=self._warriors_vocabulary_file_name,
+ vocabulary_size=self._warriors_vocabulary_size,
+ dtype=dtypes.int32)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1), (2, 2)),
+ values=(11, 100, 30, 22),
+ dense_shape=(3, 3))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array((2, -1, 0, 4), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_weight_pair.id_tensor.eval())
+
+ def test_get_sparse_tensors_int32_dense_input(self):
+ default_value = -100
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa',
+ vocabulary_file=self._warriors_vocabulary_file_name,
+ vocabulary_size=self._warriors_vocabulary_size,
+ dtype=dtypes.int32,
+ default_value=default_value)
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': ((11, -1, -1), (100, 30, -1), (-1, -1, 22))
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1), (2, 2)),
+ values=np.array((2, default_value, 0, 4), dtype=np.int64),
+ dense_shape=(3, 3)),
+ id_weight_pair.id_tensor.eval())
+
+ def test_get_sparse_tensors_int32_with_oov_buckets(self):
+ column = fc.categorical_column_with_vocabulary_file(
+ key='aaa',
+ vocabulary_file=self._warriors_vocabulary_file_name,
+ vocabulary_size=self._warriors_vocabulary_size,
+ dtype=dtypes.int32,
+ num_oov_buckets=100)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1), (2, 2)),
+ values=(11, 100, 30, 22),
+ dense_shape=(3, 3))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array((2, 60, 0, 4), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_weight_pair.id_tensor.eval())
+
+ def test_linear_model(self):
+ wire_column = fc_old.categorical_column_with_vocabulary_file(
+ key='wire',
+ vocabulary_file=self._wire_vocabulary_file_name,
+ vocabulary_size=self._wire_vocabulary_size,
+ num_oov_buckets=1)
+ self.assertEqual(4, wire_column._num_buckets)
+ with ops.Graph().as_default():
+ predictions = fc.linear_model({
+ wire_column.name: sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ }, (wire_column,))
+ bias = get_linear_model_bias()
+ wire_var = get_linear_model_column_var(wire_column)
+ with _initialized_session():
+ self.assertAllClose((0.,), bias.eval())
+ self.assertAllClose(((0.,), (0.,), (0.,), (0.,)), wire_var.eval())
+ self.assertAllClose(((0.,), (0.,)), predictions.eval())
+ wire_var.assign(((1.,), (2.,), (3.,), (4.,))).eval()
+ # 'marlo' -> 2: wire_var[2] = 3
+ # 'skywalker' -> 3, 'omar' -> 0: wire_var[3] + wire_var[0] = 4+1 = 5
+ self.assertAllClose(((3.,), (5.,)), predictions.eval())
+
+ def test_keras_linear_model(self):
+ wire_column = fc_old.categorical_column_with_vocabulary_file(
+ key='wire',
+ vocabulary_file=self._wire_vocabulary_file_name,
+ vocabulary_size=self._wire_vocabulary_size,
+ num_oov_buckets=1)
+ self.assertEqual(4, wire_column._num_buckets)
+ with ops.Graph().as_default():
+ predictions = get_keras_linear_model_predictions({
+ wire_column.name:
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ }, (wire_column,))
+ bias = get_linear_model_bias()
+ wire_var = get_linear_model_column_var(wire_column)
+ with _initialized_session():
+ self.assertAllClose((0.,), bias.eval())
+ self.assertAllClose(((0.,), (0.,), (0.,), (0.,)), wire_var.eval())
+ self.assertAllClose(((0.,), (0.,)), predictions.eval())
+ wire_var.assign(((1.,), (2.,), (3.,), (4.,))).eval()
+ # 'marlo' -> 2: wire_var[2] = 3
+ # 'skywalker' -> 3, 'omar' -> 0: wire_var[3] + wire_var[0] = 4+1 = 5
+ self.assertAllClose(((3.,), (5.,)), predictions.eval())
+
+
+class VocabularyListCategoricalColumnTest(test.TestCase):
+
+ def test_defaults_string(self):
+ column = fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=('omar', 'stringer', 'marlo'))
+ self.assertEqual('aaa', column.name)
+ self.assertEqual('aaa', column.key)
+ self.assertEqual(3, column.num_buckets)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.string)
+ }, column.parse_example_spec)
+
+ def test_key_should_be_string(self):
+ with self.assertRaisesRegexp(ValueError, 'key must be a string.'):
+ fc.categorical_column_with_vocabulary_list(
+ key=('aaa',), vocabulary_list=('omar', 'stringer', 'marlo'))
+
+ def test_defaults_int(self):
+ column = fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=(12, 24, 36))
+ self.assertEqual('aaa', column.name)
+ self.assertEqual('aaa', column.key)
+ self.assertEqual(3, column.num_buckets)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int64)
+ }, column.parse_example_spec)
+
+ def test_all_constructor_args(self):
+ column = fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=(12, 24, 36), dtype=dtypes.int32,
+ default_value=-99)
+ self.assertEqual(3, column.num_buckets)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int32)
+ }, column.parse_example_spec)
+
+ def test_deep_copy(self):
+ original = fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=(12, 24, 36), dtype=dtypes.int32)
+ for column in (original, copy.deepcopy(original)):
+ self.assertEqual('aaa', column.name)
+ self.assertEqual(3, column.num_buckets)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int32)
+ }, column.parse_example_spec)
+
+ def test_invalid_dtype(self):
+ with self.assertRaisesRegexp(ValueError, 'dtype must be string or integer'):
+ fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=('omar', 'stringer', 'marlo'),
+ dtype=dtypes.float32)
+
+ def test_invalid_mapping_dtype(self):
+ with self.assertRaisesRegexp(
+ ValueError, r'vocabulary dtype must be string or integer'):
+ fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=(12., 24., 36.))
+
+ def test_mismatched_int_dtype(self):
+ with self.assertRaisesRegexp(
+ ValueError, r'dtype.*and vocabulary dtype.*do not match'):
+ fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=('omar', 'stringer', 'marlo'),
+ dtype=dtypes.int32)
+
+ def test_mismatched_string_dtype(self):
+ with self.assertRaisesRegexp(
+ ValueError, r'dtype.*and vocabulary dtype.*do not match'):
+ fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=(12, 24, 36), dtype=dtypes.string)
+
+ def test_none_mapping(self):
+ with self.assertRaisesRegexp(
+ ValueError, r'vocabulary_list.*must be non-empty'):
+ fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=None)
+
+ def test_empty_mapping(self):
+ with self.assertRaisesRegexp(
+ ValueError, r'vocabulary_list.*must be non-empty'):
+ fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=tuple([]))
+
+ def test_duplicate_mapping(self):
+ with self.assertRaisesRegexp(ValueError, 'Duplicate keys'):
+ fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=(12, 24, 12))
+
+ def test_invalid_num_oov_buckets(self):
+ with self.assertRaisesRegexp(ValueError, 'Invalid num_oov_buckets'):
+ fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=(12, 24, 36),
+ num_oov_buckets=-1)
+
+ def test_invalid_buckets_and_default_value(self):
+ with self.assertRaisesRegexp(
+ ValueError, 'both num_oov_buckets and default_value'):
+ fc.categorical_column_with_vocabulary_list(
+ key='aaa',
+ vocabulary_list=(12, 24, 36),
+ num_oov_buckets=100,
+ default_value=2)
+
+ def test_invalid_input_dtype_int32(self):
+ column = fc.categorical_column_with_vocabulary_list(
+ key='aaa',
+ vocabulary_list=('omar', 'stringer', 'marlo'))
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(12, 24, 36),
+ dense_shape=(2, 2))
+ with self.assertRaisesRegexp(ValueError, 'dtype must be compatible'):
+ column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+
+ def test_invalid_input_dtype_string(self):
+ column = fc.categorical_column_with_vocabulary_list(
+ key='aaa',
+ vocabulary_list=(12, 24, 36))
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('omar', 'stringer', 'marlo'),
+ dense_shape=(2, 2))
+ with self.assertRaisesRegexp(ValueError, 'dtype must be compatible'):
+ column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+
+ def test_parse_example_string(self):
+ a = fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=('omar', 'stringer', 'marlo'))
+ data = example_pb2.Example(features=feature_pb2.Features(
+ feature={
+ 'aaa':
+ feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
+ value=[b'omar', b'stringer']))
+ }))
+ features = parsing_ops.parse_example(
+ serialized=[data.SerializeToString()],
+ features=fc.make_parse_example_spec([a]))
+ self.assertIn('aaa', features)
+ with self.test_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=[[0, 0], [0, 1]],
+ values=np.array([b'omar', b'stringer'], dtype=np.object_),
+ dense_shape=[1, 2]),
+ features['aaa'].eval())
+
+ def test_parse_example_int(self):
+ a = fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=(11, 21, 31))
+ data = example_pb2.Example(features=feature_pb2.Features(
+ feature={
+ 'aaa':
+ feature_pb2.Feature(int64_list=feature_pb2.Int64List(
+ value=[11, 21]))
+ }))
+ features = parsing_ops.parse_example(
+ serialized=[data.SerializeToString()],
+ features=fc.make_parse_example_spec([a]))
+ self.assertIn('aaa', features)
+ with self.test_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=[[0, 0], [0, 1]],
+ values=[11, 21],
+ dense_shape=[1, 2]),
+ features['aaa'].eval())
+
+ def test_get_sparse_tensors(self):
+ column = fc.categorical_column_with_vocabulary_list(
+ key='aaa',
+ vocabulary_list=('omar', 'stringer', 'marlo'))
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array((2, -1, 0), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_weight_pair.id_tensor.eval())
+
+ def test_transform_feature(self):
+ column = fc.categorical_column_with_vocabulary_list(
+ key='aaa',
+ vocabulary_list=('omar', 'stringer', 'marlo'))
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ id_tensor = _transform_features({'aaa': inputs}, [column], None)[column]
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array((2, -1, 0), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_tensor.eval())
+
+ def DISABLED_test_get_sparse_tensors_weight_collections(self):
+ column = fc.categorical_column_with_vocabulary_list(
+ key='aaa',
+ vocabulary_list=('omar', 'stringer', 'marlo'))
+ inputs = sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'],
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }),
+ weight_collections=('my_weights',))
+
+ self.assertItemsEqual(
+ [], ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES))
+ self.assertItemsEqual([], ops.get_collection('my_weights'))
+
+ def test_get_sparse_tensors_dense_input(self):
+ column = fc.categorical_column_with_vocabulary_list(
+ key='aaa',
+ vocabulary_list=('omar', 'stringer', 'marlo'))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': (('marlo', ''), ('skywalker', 'omar'))
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=np.array((2, -1, 0), dtype=np.int64),
+ dense_shape=(2, 2)),
+ id_weight_pair.id_tensor.eval())
+
+ def test_get_sparse_tensors_default_value_in_vocabulary(self):
+ column = fc.categorical_column_with_vocabulary_list(
+ key='aaa',
+ vocabulary_list=('omar', 'stringer', 'marlo'),
+ default_value=2)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array((2, 2, 0), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_weight_pair.id_tensor.eval())
+
+ def test_get_sparse_tensors_with_oov_buckets(self):
+ column = fc.categorical_column_with_vocabulary_list(
+ key='aaa',
+ vocabulary_list=('omar', 'stringer', 'marlo'),
+ num_oov_buckets=100)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1), (1, 2)),
+ values=('marlo', 'skywalker', 'omar', 'heisenberg'),
+ dense_shape=(2, 3))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array((2, 33, 0, 62), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_weight_pair.id_tensor.eval())
+
+ def test_get_sparse_tensors_int32(self):
+ column = fc.categorical_column_with_vocabulary_list(
+ key='aaa',
+ vocabulary_list=np.array((30, 35, 11, 23, 22), dtype=np.int32),
+ dtype=dtypes.int32)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1), (2, 2)),
+ values=np.array((11, 100, 30, 22), dtype=np.int32),
+ dense_shape=(3, 3))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array((2, -1, 0, 4), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_weight_pair.id_tensor.eval())
+
+ def test_get_sparse_tensors_int32_dense_input(self):
+ default_value = -100
+ column = fc.categorical_column_with_vocabulary_list(
+ key='aaa',
+ vocabulary_list=np.array((30, 35, 11, 23, 22), dtype=np.int32),
+ dtype=dtypes.int32,
+ default_value=default_value)
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa':
+ np.array(
+ ((11, -1, -1), (100, 30, -1), (-1, -1, 22)), dtype=np.int32)
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1), (2, 2)),
+ values=np.array((2, default_value, 0, 4), dtype=np.int64),
+ dense_shape=(3, 3)),
+ id_weight_pair.id_tensor.eval())
+
+ def test_get_sparse_tensors_int32_with_oov_buckets(self):
+ column = fc.categorical_column_with_vocabulary_list(
+ key='aaa',
+ vocabulary_list=np.array((30, 35, 11, 23, 22), dtype=np.int32),
+ dtype=dtypes.int32,
+ num_oov_buckets=100)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1), (2, 2)),
+ values=(11, 100, 30, 22),
+ dense_shape=(3, 3))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array((2, 60, 0, 4), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_weight_pair.id_tensor.eval())
+
+ def test_linear_model(self):
+ wire_column = fc_old.categorical_column_with_vocabulary_list(
+ key='aaa',
+ vocabulary_list=('omar', 'stringer', 'marlo'),
+ num_oov_buckets=1)
+ self.assertEqual(4, wire_column._num_buckets)
+ with ops.Graph().as_default():
+ predictions = fc.linear_model({
+ wire_column.name: sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ }, (wire_column,))
+ bias = get_linear_model_bias()
+ wire_var = get_linear_model_column_var(wire_column)
+ with _initialized_session():
+ self.assertAllClose((0.,), bias.eval())
+ self.assertAllClose(((0.,), (0.,), (0.,), (0.,)), wire_var.eval())
+ self.assertAllClose(((0.,), (0.,)), predictions.eval())
+ wire_var.assign(((1.,), (2.,), (3.,), (4.,))).eval()
+ # 'marlo' -> 2: wire_var[2] = 3
+ # 'skywalker' -> 3, 'omar' -> 0: wire_var[3] + wire_var[0] = 4+1 = 5
+ self.assertAllClose(((3.,), (5.,)), predictions.eval())
+
+ def test_keras_linear_model(self):
+ wire_column = fc_old.categorical_column_with_vocabulary_list(
+ key='aaa',
+ vocabulary_list=('omar', 'stringer', 'marlo'),
+ num_oov_buckets=1)
+ self.assertEqual(4, wire_column._num_buckets)
+ with ops.Graph().as_default():
+ predictions = get_keras_linear_model_predictions({
+ wire_column.name:
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ }, (wire_column,))
+ bias = get_linear_model_bias()
+ wire_var = get_linear_model_column_var(wire_column)
+ with _initialized_session():
+ self.assertAllClose((0.,), bias.eval())
+ self.assertAllClose(((0.,), (0.,), (0.,), (0.,)), wire_var.eval())
+ self.assertAllClose(((0.,), (0.,)), predictions.eval())
+ wire_var.assign(((1.,), (2.,), (3.,), (4.,))).eval()
+ # 'marlo' -> 2: wire_var[2] = 3
+ # 'skywalker' -> 3, 'omar' -> 0: wire_var[3] + wire_var[0] = 4+1 = 5
+ self.assertAllClose(((3.,), (5.,)), predictions.eval())
+
+
+class IdentityCategoricalColumnTest(test.TestCase):
+
+ def test_constructor(self):
+ column = fc.categorical_column_with_identity(key='aaa', num_buckets=3)
+ self.assertEqual('aaa', column.name)
+ self.assertEqual('aaa', column.key)
+ self.assertEqual(3, column.num_buckets)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int64)
+ }, column.parse_example_spec)
+
+ def test_key_should_be_string(self):
+ with self.assertRaisesRegexp(ValueError, 'key must be a string.'):
+ fc.categorical_column_with_identity(key=('aaa',), num_buckets=3)
+
+ def test_deep_copy(self):
+ original = fc.categorical_column_with_identity(key='aaa', num_buckets=3)
+ for column in (original, copy.deepcopy(original)):
+ self.assertEqual('aaa', column.name)
+ self.assertEqual(3, column.num_buckets)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int64)
+ }, column.parse_example_spec)
+
+ def test_invalid_num_buckets_zero(self):
+ with self.assertRaisesRegexp(ValueError, 'num_buckets 0 < 1'):
+ fc.categorical_column_with_identity(key='aaa', num_buckets=0)
+
+ def test_invalid_num_buckets_negative(self):
+ with self.assertRaisesRegexp(ValueError, 'num_buckets -1 < 1'):
+ fc.categorical_column_with_identity(key='aaa', num_buckets=-1)
+
+ def test_invalid_default_value_too_small(self):
+ with self.assertRaisesRegexp(ValueError, 'default_value -1 not in range'):
+ fc.categorical_column_with_identity(
+ key='aaa', num_buckets=3, default_value=-1)
+
+ def test_invalid_default_value_too_big(self):
+ with self.assertRaisesRegexp(ValueError, 'default_value 3 not in range'):
+ fc.categorical_column_with_identity(
+ key='aaa', num_buckets=3, default_value=3)
+
+ def test_invalid_input_dtype(self):
+ column = fc.categorical_column_with_identity(key='aaa', num_buckets=3)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('omar', 'stringer', 'marlo'),
+ dense_shape=(2, 2))
+ with self.assertRaisesRegexp(ValueError, 'Invalid input, not integer'):
+ column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+
+ def test_parse_example(self):
+ a = fc.categorical_column_with_identity(key='aaa', num_buckets=30)
+ data = example_pb2.Example(features=feature_pb2.Features(
+ feature={
+ 'aaa':
+ feature_pb2.Feature(int64_list=feature_pb2.Int64List(
+ value=[11, 21]))
+ }))
+ features = parsing_ops.parse_example(
+ serialized=[data.SerializeToString()],
+ features=fc.make_parse_example_spec([a]))
+ self.assertIn('aaa', features)
+ with self.test_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=[[0, 0], [0, 1]],
+ values=np.array([11, 21], dtype=np.int64),
+ dense_shape=[1, 2]),
+ features['aaa'].eval())
+
+ def test_get_sparse_tensors(self):
+ column = fc.categorical_column_with_identity(key='aaa', num_buckets=3)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 1, 0),
+ dense_shape=(2, 2))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array((0, 1, 0), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_weight_pair.id_tensor.eval())
+
+ def test_transform_feature(self):
+ column = fc.categorical_column_with_identity(key='aaa', num_buckets=3)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 1, 0),
+ dense_shape=(2, 2))
+ id_tensor = _transform_features({'aaa': inputs}, [column], None)[column]
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array((0, 1, 0), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_tensor.eval())
+
+ def DISABLED_test_get_sparse_tensors_weight_collections(self):
+ column = fc.categorical_column_with_identity(key='aaa', num_buckets=3)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 1, 0),
+ dense_shape=(2, 2))
+ column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }),
+ weight_collections=('my_weights',))
+
+ self.assertItemsEqual(
+ [], ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES))
+ self.assertItemsEqual([], ops.get_collection('my_weights'))
+
+ def test_get_sparse_tensors_dense_input(self):
+ column = fc.categorical_column_with_identity(key='aaa', num_buckets=3)
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': ((0, -1), (1, 0))
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=np.array((0, 1, 0), dtype=np.int64),
+ dense_shape=(2, 2)),
+ id_weight_pair.id_tensor.eval())
+
+ def test_get_sparse_tensors_with_inputs_too_small(self):
+ column = fc.categorical_column_with_identity(key='aaa', num_buckets=3)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(1, -1, 0),
+ dense_shape=(2, 2))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ with self.assertRaisesRegexp(
+ errors.OpError, 'assert_greater_or_equal_0'):
+ id_weight_pair.id_tensor.eval()
+
+ def test_get_sparse_tensors_with_inputs_too_big(self):
+ column = fc.categorical_column_with_identity(key='aaa', num_buckets=3)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(1, 99, 0),
+ dense_shape=(2, 2))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ with self.assertRaisesRegexp(
+ errors.OpError, 'assert_less_than_num_buckets'):
+ id_weight_pair.id_tensor.eval()
+
+ def test_get_sparse_tensors_with_default_value(self):
+ column = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=4, default_value=3)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(1, -1, 99),
+ dense_shape=(2, 2))
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array((1, 3, 3), dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_weight_pair.id_tensor.eval())
+
+ def test_get_sparse_tensors_with_default_value_and_placeholder_inputs(self):
+ column = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=4, default_value=3)
+ input_indices = array_ops.placeholder(dtype=dtypes.int64)
+ input_values = array_ops.placeholder(dtype=dtypes.int32)
+ input_shape = array_ops.placeholder(dtype=dtypes.int64)
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=input_indices,
+ values=input_values,
+ dense_shape=input_shape)
+ id_weight_pair = column.get_sparse_tensors(
+ FeatureTransformationCache({
+ 'aaa': inputs
+ }), None)
+ self.assertIsNone(id_weight_pair.weight_tensor)
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=np.array(((0, 0), (1, 0), (1, 1)), dtype=np.int64),
+ values=np.array((1, 3, 3), dtype=np.int64),
+ dense_shape=np.array((2, 2), dtype=np.int64)),
+ id_weight_pair.id_tensor.eval(feed_dict={
+ input_indices: ((0, 0), (1, 0), (1, 1)),
+ input_values: (1, -1, 99),
+ input_shape: (2, 2),
+ }))
+
+ def test_linear_model(self):
+ column = fc_old.categorical_column_with_identity(key='aaa', num_buckets=3)
+ self.assertEqual(3, column.num_buckets)
+ with ops.Graph().as_default():
+ predictions = fc.linear_model({
+ column.name: sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 2, 1),
+ dense_shape=(2, 2))
+ }, (column,))
+ bias = get_linear_model_bias()
+ weight_var = get_linear_model_column_var(column)
+ with _initialized_session():
+ self.assertAllClose((0.,), bias.eval())
+ self.assertAllClose(((0.,), (0.,), (0.,)), weight_var.eval())
+ self.assertAllClose(((0.,), (0.,)), predictions.eval())
+ weight_var.assign(((1.,), (2.,), (3.,))).eval()
+ # weight_var[0] = 1
+ # weight_var[2] + weight_var[1] = 3+2 = 5
+ self.assertAllClose(((1.,), (5.,)), predictions.eval())
+
+ def test_keras_linear_model(self):
+ column = fc_old.categorical_column_with_identity(key='aaa', num_buckets=3)
+ self.assertEqual(3, column.num_buckets)
+ with ops.Graph().as_default():
+ predictions = get_keras_linear_model_predictions({
+ column.name:
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 2, 1),
+ dense_shape=(2, 2))
+ }, (column,))
+ bias = get_linear_model_bias()
+ weight_var = get_linear_model_column_var(column)
+ with _initialized_session():
+ self.assertAllClose((0.,), bias.eval())
+ self.assertAllClose(((0.,), (0.,), (0.,)), weight_var.eval())
+ self.assertAllClose(((0.,), (0.,)), predictions.eval())
+ weight_var.assign(((1.,), (2.,), (3.,))).eval()
+ # weight_var[0] = 1
+ # weight_var[2] + weight_var[1] = 3+2 = 5
+ self.assertAllClose(((1.,), (5.,)), predictions.eval())
+
+
+class TransformFeaturesTest(test.TestCase):
+
+ # All transform tests are distributed in column test.
+ # Here we only test multi column case and naming
+ def transform_multi_column(self):
+ bucketized_price = fc.bucketized_column(
+ fc.numeric_column('price'), boundaries=[0, 2, 4, 6])
+ hashed_sparse = fc.categorical_column_with_hash_bucket('wire', 10)
+ with ops.Graph().as_default():
+ features = {
+ 'price': [[-1.], [5.]],
+ 'wire':
+ sparse_tensor.SparseTensor(
+ values=['omar', 'stringer', 'marlo'],
+ indices=[[0, 0], [1, 0], [1, 1]],
+ dense_shape=[2, 2])
+ }
+ transformed = _transform_features(features,
+ [bucketized_price, hashed_sparse], None)
+ with _initialized_session():
+ self.assertIn(bucketized_price.name, transformed[bucketized_price].name)
+ self.assertAllEqual([[0], [3]], transformed[bucketized_price].eval())
+ self.assertIn(hashed_sparse.name, transformed[hashed_sparse].name)
+ self.assertAllEqual([6, 4, 1], transformed[hashed_sparse].values.eval())
+
+ def test_column_order(self):
+ """When the column is both dense and sparse, uses sparse tensors."""
+
+ class _LoggerColumn(FeatureColumn):
+
+ def __init__(self, name):
+ self._name = name
+
+ @property
+ def name(self):
+ return self._name
+
+ def transform_feature(self, transformation_cache, state_manager):
+ self.call_order = call_logger['count']
+ call_logger['count'] += 1
+ return 'Anything'
+
+ @property
+ def parse_example_spec(self):
+ pass
+
+ with ops.Graph().as_default():
+ column1 = _LoggerColumn('1')
+ column2 = _LoggerColumn('2')
+ call_logger = {'count': 0}
+ _transform_features({}, [column1, column2], None)
+ self.assertEqual(0, column1.call_order)
+ self.assertEqual(1, column2.call_order)
+
+ call_logger = {'count': 0}
+ _transform_features({}, [column2, column1], None)
+ self.assertEqual(0, column1.call_order)
+ self.assertEqual(1, column2.call_order)
+
+
+class IndicatorColumnTest(test.TestCase):
+
+ def test_indicator_column(self):
+ a = fc.categorical_column_with_hash_bucket('a', 4)
+ indicator_a = fc.indicator_column(a)
+ self.assertEqual(indicator_a.categorical_column.name, 'a')
+ self.assertEqual(indicator_a.name, 'a_indicator')
+ self.assertEqual(indicator_a.variable_shape, [1, 4])
+
+ b = fc.categorical_column_with_hash_bucket('b', hash_bucket_size=100)
+ indicator_b = fc.indicator_column(b)
+ self.assertEqual(indicator_b.categorical_column.name, 'b')
+ self.assertEqual(indicator_b.name, 'b_indicator')
+ self.assertEqual(indicator_b.variable_shape, [1, 100])
+
+ def test_1D_shape_succeeds(self):
+ animal = fc.indicator_column(
+ fc.categorical_column_with_hash_bucket('animal', 4))
+ transformation_cache = FeatureTransformationCache({
+ 'animal': ['fox', 'fox']
+ })
+ output = transformation_cache.get(animal, None)
+ with self.test_session():
+ self.assertAllEqual([[0., 0., 1., 0.], [0., 0., 1., 0.]], output.eval())
+
+ def test_2D_shape_succeeds(self):
+ # TODO(ispir/cassandrax): Swith to categorical_column_with_keys when ready.
+ animal = fc.indicator_column(
+ fc.categorical_column_with_hash_bucket('animal', 4))
+ transformation_cache = FeatureTransformationCache({
+ 'animal':
+ sparse_tensor.SparseTensor(
+ indices=[[0, 0], [1, 0]],
+ values=['fox', 'fox'],
+ dense_shape=[2, 1])
+ })
+ output = transformation_cache.get(animal, None)
+ with self.test_session():
+ self.assertAllEqual([[0., 0., 1., 0.], [0., 0., 1., 0.]], output.eval())
+
+ def test_multi_hot(self):
+ animal = fc.indicator_column(
+ fc.categorical_column_with_identity('animal', num_buckets=4))
+
+ transformation_cache = FeatureTransformationCache({
+ 'animal':
+ sparse_tensor.SparseTensor(
+ indices=[[0, 0], [0, 1]], values=[1, 1], dense_shape=[1, 2])
+ })
+ output = transformation_cache.get(animal, None)
+ with self.test_session():
+ self.assertAllEqual([[0., 2., 0., 0.]], output.eval())
+
+ def test_multi_hot2(self):
+ animal = fc.indicator_column(
+ fc.categorical_column_with_identity('animal', num_buckets=4))
+ transformation_cache = FeatureTransformationCache({
+ 'animal':
+ sparse_tensor.SparseTensor(
+ indices=[[0, 0], [0, 1]], values=[1, 2], dense_shape=[1, 2])
+ })
+ output = transformation_cache.get(animal, None)
+ with self.test_session():
+ self.assertAllEqual([[0., 1., 1., 0.]], output.eval())
+
+ def test_deep_copy(self):
+ a = fc.categorical_column_with_hash_bucket('a', 4)
+ column = fc.indicator_column(a)
+ column_copy = copy.deepcopy(column)
+ self.assertEqual(column_copy.categorical_column.name, 'a')
+ self.assertEqual(column.name, 'a_indicator')
+ self.assertEqual(column.variable_shape, [1, 4])
+
+ def test_parse_example(self):
+ a = fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=('omar', 'stringer', 'marlo'))
+ a_indicator = fc.indicator_column(a)
+ data = example_pb2.Example(features=feature_pb2.Features(
+ feature={
+ 'aaa':
+ feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
+ value=[b'omar', b'stringer']))
+ }))
+ features = parsing_ops.parse_example(
+ serialized=[data.SerializeToString()],
+ features=fc.make_parse_example_spec([a_indicator]))
+ self.assertIn('aaa', features)
+ with self.test_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=[[0, 0], [0, 1]],
+ values=np.array([b'omar', b'stringer'], dtype=np.object_),
+ dense_shape=[1, 2]),
+ features['aaa'].eval())
+
+ def test_transform(self):
+ a = fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=('omar', 'stringer', 'marlo'))
+ a_indicator = fc.indicator_column(a)
+ features = {
+ 'aaa': sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('marlo', 'skywalker', 'omar'),
+ dense_shape=(2, 2))
+ }
+ indicator_tensor = _transform_features(features, [a_indicator],
+ None)[a_indicator]
+ with _initialized_session():
+ self.assertAllEqual([[0, 0, 1], [1, 0, 0]], indicator_tensor.eval())
+
+ def test_transform_with_weighted_column(self):
+ # Github issue 12557
+ ids = fc.categorical_column_with_vocabulary_list(
+ key='ids', vocabulary_list=('a', 'b', 'c'))
+ weights = fc.weighted_categorical_column(ids, 'weights')
+ indicator = fc.indicator_column(weights)
+ features = {
+ 'ids': constant_op.constant([['c', 'b', 'a']]),
+ 'weights': constant_op.constant([[2., 4., 6.]])
+ }
+ indicator_tensor = _transform_features(features, [indicator],
+ None)[indicator]
+ with _initialized_session():
+ self.assertAllEqual([[6., 4., 2.]], indicator_tensor.eval())
+
+ def test_transform_with_missing_value_in_weighted_column(self):
+ # Github issue 12583
+ ids = fc.categorical_column_with_vocabulary_list(
+ key='ids', vocabulary_list=('a', 'b', 'c'))
+ weights = fc.weighted_categorical_column(ids, 'weights')
+ indicator = fc.indicator_column(weights)
+ features = {
+ 'ids': constant_op.constant([['c', 'b', 'unknown']]),
+ 'weights': constant_op.constant([[2., 4., 6.]])
+ }
+ indicator_tensor = _transform_features(features, [indicator],
+ None)[indicator]
+ with _initialized_session():
+ self.assertAllEqual([[0., 4., 2.]], indicator_tensor.eval())
+
+ def test_transform_with_missing_value_in_categorical_column(self):
+ # Github issue 12583
+ ids = fc.categorical_column_with_vocabulary_list(
+ key='ids', vocabulary_list=('a', 'b', 'c'))
+ indicator = fc.indicator_column(ids)
+ features = {
+ 'ids': constant_op.constant([['c', 'b', 'unknown']]),
+ }
+ indicator_tensor = _transform_features(features, [indicator],
+ None)[indicator]
+ with _initialized_session():
+ self.assertAllEqual([[0., 1., 1.]], indicator_tensor.eval())
+
+ def test_linear_model(self):
+ animal = fc_old.indicator_column(
+ fc_old.categorical_column_with_identity('animal', num_buckets=4))
+ with ops.Graph().as_default():
+ features = {
+ 'animal':
+ sparse_tensor.SparseTensor(
+ indices=[[0, 0], [0, 1]], values=[1, 2], dense_shape=[1, 2])
+ }
+
+ predictions = fc.linear_model(features, [animal])
+ weight_var = get_linear_model_column_var(animal)
+ with _initialized_session():
+ # All should be zero-initialized.
+ self.assertAllClose([[0.], [0.], [0.], [0.]], weight_var.eval())
+ self.assertAllClose([[0.]], predictions.eval())
+ weight_var.assign([[1.], [2.], [3.], [4.]]).eval()
+ self.assertAllClose([[2. + 3.]], predictions.eval())
+
+ def test_keras_linear_model(self):
+ animal = fc_old.indicator_column(
+ fc_old.categorical_column_with_identity('animal', num_buckets=4))
+ with ops.Graph().as_default():
+ features = {
+ 'animal':
+ sparse_tensor.SparseTensor(
+ indices=[[0, 0], [0, 1]], values=[1, 2], dense_shape=[1, 2])
+ }
+
+ predictions = get_keras_linear_model_predictions(features, [animal])
+ weight_var = get_linear_model_column_var(animal)
+ with _initialized_session():
+ # All should be zero-initialized.
+ self.assertAllClose([[0.], [0.], [0.], [0.]], weight_var.eval())
+ self.assertAllClose([[0.]], predictions.eval())
+ weight_var.assign([[1.], [2.], [3.], [4.]]).eval()
+ self.assertAllClose([[2. + 3.]], predictions.eval())
+
+ def test_input_layer(self):
+ animal = fc_old.indicator_column(
+ fc_old.categorical_column_with_identity('animal', num_buckets=4))
+ with ops.Graph().as_default():
+ features = {
+ 'animal':
+ sparse_tensor.SparseTensor(
+ indices=[[0, 0], [0, 1]], values=[1, 2], dense_shape=[1, 2])
+ }
+ net = fc.input_layer(features, [animal])
+ with _initialized_session():
+ self.assertAllClose([[0., 1., 1., 0.]], net.eval())
+
+
+class _TestStateManager(StateManager):
+
+ def __init__(self, trainable=True):
+ # Dict of feature_column to a dict of variables.
+ self._all_variables = {}
+ self._trainable = trainable
+
+ def get_variable(self,
+ feature_column,
+ name,
+ shape,
+ dtype=None,
+ initializer=None):
+ if feature_column not in self._all_variables:
+ self._all_variables[feature_column] = {}
+ var_dict = self._all_variables[feature_column]
+ if name in var_dict:
+ return var_dict[name]
+ else:
+ var = variable_scope.get_variable(
+ name=name,
+ shape=shape,
+ initializer=initializer,
+ trainable=self._trainable)
+ var_dict[name] = var
+ return var
+
+
+class EmbeddingColumnTest(test.TestCase):
+
+ def test_defaults(self):
+ categorical_column = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=3)
+ embedding_dimension = 2
+ embedding_column = fc.embedding_column(
+ categorical_column, dimension=embedding_dimension)
+ self.assertIs(categorical_column, embedding_column.categorical_column)
+ self.assertEqual(embedding_dimension, embedding_column.dimension)
+ self.assertEqual('mean', embedding_column.combiner)
+ self.assertIsNone(embedding_column.ckpt_to_load_from)
+ self.assertIsNone(embedding_column.tensor_name_in_ckpt)
+ self.assertIsNone(embedding_column.max_norm)
+ self.assertTrue(embedding_column.trainable)
+ self.assertEqual('aaa_embedding', embedding_column.name)
+ self.assertEqual((embedding_dimension,), embedding_column.variable_shape)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int64)
+ }, embedding_column.parse_example_spec)
+
+ def test_all_constructor_args(self):
+ categorical_column = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=3)
+ embedding_dimension = 2
+ embedding_column = fc.embedding_column(
+ categorical_column, dimension=embedding_dimension,
+ combiner='my_combiner', initializer=lambda: 'my_initializer',
+ ckpt_to_load_from='my_ckpt', tensor_name_in_ckpt='my_ckpt_tensor',
+ max_norm=42., trainable=False)
+ self.assertIs(categorical_column, embedding_column.categorical_column)
+ self.assertEqual(embedding_dimension, embedding_column.dimension)
+ self.assertEqual('my_combiner', embedding_column.combiner)
+ self.assertEqual('my_ckpt', embedding_column.ckpt_to_load_from)
+ self.assertEqual('my_ckpt_tensor', embedding_column.tensor_name_in_ckpt)
+ self.assertEqual(42., embedding_column.max_norm)
+ self.assertFalse(embedding_column.trainable)
+ self.assertEqual('aaa_embedding', embedding_column.name)
+ self.assertEqual((embedding_dimension,), embedding_column.variable_shape)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int64)
+ }, embedding_column.parse_example_spec)
+
+ def test_deep_copy(self):
+ categorical_column = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=3)
+ embedding_dimension = 2
+ original = fc.embedding_column(
+ categorical_column, dimension=embedding_dimension,
+ combiner='my_combiner', initializer=lambda: 'my_initializer',
+ ckpt_to_load_from='my_ckpt', tensor_name_in_ckpt='my_ckpt_tensor',
+ max_norm=42., trainable=False)
+ for embedding_column in (original, copy.deepcopy(original)):
+ self.assertEqual('aaa', embedding_column.categorical_column.name)
+ self.assertEqual(3, embedding_column.categorical_column.num_buckets)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int64)
+ }, embedding_column.categorical_column.parse_example_spec)
+
+ self.assertEqual(embedding_dimension, embedding_column.dimension)
+ self.assertEqual('my_combiner', embedding_column.combiner)
+ self.assertEqual('my_ckpt', embedding_column.ckpt_to_load_from)
+ self.assertEqual('my_ckpt_tensor', embedding_column.tensor_name_in_ckpt)
+ self.assertEqual(42., embedding_column.max_norm)
+ self.assertFalse(embedding_column.trainable)
+ self.assertEqual('aaa_embedding', embedding_column.name)
+ self.assertEqual((embedding_dimension,), embedding_column.variable_shape)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int64)
+ }, embedding_column.parse_example_spec)
+
+ def test_invalid_initializer(self):
+ categorical_column = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=3)
+ with self.assertRaisesRegexp(ValueError, 'initializer must be callable'):
+ fc.embedding_column(categorical_column, dimension=2, initializer='not_fn')
+
+ def test_parse_example(self):
+ a = fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=('omar', 'stringer', 'marlo'))
+ a_embedded = fc.embedding_column(a, dimension=2)
+ data = example_pb2.Example(features=feature_pb2.Features(
+ feature={
+ 'aaa':
+ feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
+ value=[b'omar', b'stringer']))
+ }))
+ features = parsing_ops.parse_example(
+ serialized=[data.SerializeToString()],
+ features=fc.make_parse_example_spec([a_embedded]))
+ self.assertIn('aaa', features)
+ with self.test_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=[[0, 0], [0, 1]],
+ values=np.array([b'omar', b'stringer'], dtype=np.object_),
+ dense_shape=[1, 2]),
+ features['aaa'].eval())
+
+ def test_transform_feature(self):
+ a = fc.categorical_column_with_identity(key='aaa', num_buckets=3)
+ a_embedded = fc.embedding_column(a, dimension=2)
+ features = {
+ 'aaa': sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 1, 0),
+ dense_shape=(2, 2))
+ }
+ outputs = _transform_features(features, [a, a_embedded], None)
+ output_a = outputs[a]
+ output_embedded = outputs[a_embedded]
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self, output_a.eval(), output_embedded.eval())
+
+ def test_get_dense_tensor(self):
+ # Inputs.
+ vocabulary_size = 3
+ sparse_input = sparse_tensor.SparseTensorValue(
+ # example 0, ids [2]
+ # example 1, ids [0, 1]
+ # example 2, ids []
+ # example 3, ids [1]
+ indices=((0, 0), (1, 0), (1, 4), (3, 0)),
+ values=(2, 0, 1, 1),
+ dense_shape=(4, 5))
+
+ # Embedding variable.
+ embedding_dimension = 2
+ embedding_values = (
+ (1., 2.), # id 0
+ (3., 5.), # id 1
+ (7., 11.) # id 2
+ )
+ def _initializer(shape, dtype, partition_info):
+ self.assertAllEqual((vocabulary_size, embedding_dimension), shape)
+ self.assertEqual(dtypes.float32, dtype)
+ self.assertIsNone(partition_info)
+ return embedding_values
+
+ # Expected lookup result, using combiner='mean'.
+ expected_lookups = (
+ # example 0, ids [2], embedding = [7, 11]
+ (7., 11.),
+ # example 1, ids [0, 1], embedding = mean([1, 2] + [3, 5]) = [2, 3.5]
+ (2., 3.5),
+ # example 2, ids [], embedding = [0, 0]
+ (0., 0.),
+ # example 3, ids [1], embedding = [3, 5]
+ (3., 5.),
+ )
+
+ # Build columns.
+ categorical_column = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=vocabulary_size)
+ embedding_column = fc.embedding_column(
+ categorical_column, dimension=embedding_dimension,
+ initializer=_initializer)
+ state_manager = _TestStateManager()
+
+ # Provide sparse input and get dense result.
+ embedding_lookup = embedding_column.get_dense_tensor(
+ FeatureTransformationCache({
+ 'aaa': sparse_input
+ }), state_manager)
+
+ # Assert expected embedding variable and lookups.
+ global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
+ self.assertItemsEqual(('embedding_weights:0',),
+ tuple([v.name for v in global_vars]))
+ with _initialized_session():
+ self.assertAllEqual(embedding_values, global_vars[0].eval())
+ self.assertAllEqual(expected_lookups, embedding_lookup.eval())
+
+ def test_get_dense_tensor_3d(self):
+ # Inputs.
+ vocabulary_size = 4
+ sparse_input = sparse_tensor.SparseTensorValue(
+ # example 0, ids [2]
+ # example 1, ids [0, 1]
+ # example 2, ids []
+ # example 3, ids [1]
+ indices=((0, 0, 0), (1, 1, 0), (1, 1, 4), (3, 0, 0), (3, 1, 2)),
+ values=(2, 0, 1, 1, 2),
+ dense_shape=(4, 2, 5))
+
+ # Embedding variable.
+ embedding_dimension = 3
+ embedding_values = (
+ (1., 2., 4.), # id 0
+ (3., 5., 1.), # id 1
+ (7., 11., 2.), # id 2
+ (2., 7., 12.) # id 3
+ )
+ def _initializer(shape, dtype, partition_info):
+ self.assertAllEqual((vocabulary_size, embedding_dimension), shape)
+ self.assertEqual(dtypes.float32, dtype)
+ self.assertIsNone(partition_info)
+ return embedding_values
+
+ # Expected lookup result, using combiner='mean'.
+ expected_lookups = (
+ # example 0, ids [[2], []], embedding = [[7, 11, 2], [0, 0, 0]]
+ ((7., 11., 2.), (0., 0., 0.)),
+ # example 1, ids [[], [0, 1]], embedding
+ # = mean([[], [1, 2, 4] + [3, 5, 1]]) = [[0, 0, 0], [2, 3.5, 2.5]]
+ ((0., 0., 0.), (2., 3.5, 2.5)),
+ # example 2, ids [[], []], embedding = [[0, 0, 0], [0, 0, 0]]
+ ((0., 0., 0.), (0., 0., 0.)),
+ # example 3, ids [[1], [2]], embedding = [[3, 5, 1], [7, 11, 2]]
+ ((3., 5., 1.), (7., 11., 2.)),
+ )
+
+ # Build columns.
+ categorical_column = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=vocabulary_size)
+ embedding_column = fc.embedding_column(
+ categorical_column, dimension=embedding_dimension,
+ initializer=_initializer)
+ state_manager = _TestStateManager()
+
+ # Provide sparse input and get dense result.
+ embedding_lookup = embedding_column.get_dense_tensor(
+ FeatureTransformationCache({
+ 'aaa': sparse_input
+ }), state_manager)
+
+ # Assert expected embedding variable and lookups.
+ global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
+ self.assertItemsEqual(('embedding_weights:0',),
+ tuple([v.name for v in global_vars]))
+ with _initialized_session():
+ self.assertAllEqual(embedding_values, global_vars[0].eval())
+ self.assertAllEqual(expected_lookups, embedding_lookup.eval())
+
+ def DISABLED_test_get_dense_tensor_weight_collections(self):
+ sparse_input = sparse_tensor.SparseTensorValue(
+ # example 0, ids [2]
+ # example 1, ids [0, 1]
+ # example 2, ids []
+ # example 3, ids [1]
+ indices=((0, 0), (1, 0), (1, 4), (3, 0)),
+ values=(2, 0, 1, 1),
+ dense_shape=(4, 5))
+
+ # Build columns.
+ categorical_column = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=3)
+ embedding_column = fc.embedding_column(categorical_column, dimension=2)
+
+ # Provide sparse input and get dense result.
+ embedding_column.get_dense_tensor(
+ FeatureTransformationCache({
+ 'aaa': sparse_input
+ }),
+ weight_collections=('my_vars',))
+
+ # Assert expected embedding variable and lookups.
+ global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
+ self.assertItemsEqual(('embedding_weights:0',),
+ tuple([v.name for v in global_vars]))
+ my_vars = ops.get_collection('my_vars')
+ self.assertItemsEqual(
+ ('embedding_weights:0',), tuple([v.name for v in my_vars]))
+
+ def test_get_dense_tensor_placeholder_inputs(self):
+ # Inputs.
+ vocabulary_size = 3
+ sparse_input = sparse_tensor.SparseTensorValue(
+ # example 0, ids [2]
+ # example 1, ids [0, 1]
+ # example 2, ids []
+ # example 3, ids [1]
+ indices=((0, 0), (1, 0), (1, 4), (3, 0)),
+ values=(2, 0, 1, 1),
+ dense_shape=(4, 5))
+
+ # Embedding variable.
+ embedding_dimension = 2
+ embedding_values = (
+ (1., 2.), # id 0
+ (3., 5.), # id 1
+ (7., 11.) # id 2
+ )
+ def _initializer(shape, dtype, partition_info):
+ self.assertAllEqual((vocabulary_size, embedding_dimension), shape)
+ self.assertEqual(dtypes.float32, dtype)
+ self.assertIsNone(partition_info)
+ return embedding_values
+
+ # Expected lookup result, using combiner='mean'.
+ expected_lookups = (
+ # example 0, ids [2], embedding = [7, 11]
+ (7., 11.),
+ # example 1, ids [0, 1], embedding = mean([1, 2] + [3, 5]) = [2, 3.5]
+ (2., 3.5),
+ # example 2, ids [], embedding = [0, 0]
+ (0., 0.),
+ # example 3, ids [1], embedding = [3, 5]
+ (3., 5.),
+ )
+
+ # Build columns.
+ categorical_column = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=vocabulary_size)
+ embedding_column = fc.embedding_column(
+ categorical_column, dimension=embedding_dimension,
+ initializer=_initializer)
+ state_manager = _TestStateManager()
+
+ # Provide sparse input and get dense result.
+ input_indices = array_ops.placeholder(dtype=dtypes.int64)
+ input_values = array_ops.placeholder(dtype=dtypes.int64)
+ input_shape = array_ops.placeholder(dtype=dtypes.int64)
+ embedding_lookup = embedding_column.get_dense_tensor(
+ FeatureTransformationCache({
+ 'aaa':
+ sparse_tensor.SparseTensorValue(
+ indices=input_indices,
+ values=input_values,
+ dense_shape=input_shape)
+ }), state_manager)
+
+ # Assert expected embedding variable and lookups.
+ global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
+ self.assertItemsEqual(
+ ('embedding_weights:0',), tuple([v.name for v in global_vars]))
+ with _initialized_session():
+ self.assertAllEqual(embedding_values, global_vars[0].eval())
+ self.assertAllEqual(expected_lookups, embedding_lookup.eval(
+ feed_dict={
+ input_indices: sparse_input.indices,
+ input_values: sparse_input.values,
+ input_shape: sparse_input.dense_shape,
+ }))
+
+ def test_get_dense_tensor_restore_from_ckpt(self):
+ # Inputs.
+ vocabulary_size = 3
+ sparse_input = sparse_tensor.SparseTensorValue(
+ # example 0, ids [2]
+ # example 1, ids [0, 1]
+ # example 2, ids []
+ # example 3, ids [1]
+ indices=((0, 0), (1, 0), (1, 4), (3, 0)),
+ values=(2, 0, 1, 1),
+ dense_shape=(4, 5))
+
+ # Embedding variable. The checkpoint file contains _embedding_values.
+ embedding_dimension = 2
+ embedding_values = (
+ (1., 2.), # id 0
+ (3., 5.), # id 1
+ (7., 11.) # id 2
+ )
+ ckpt_path = test.test_src_dir_path(
+ 'python/feature_column/testdata/embedding.ckpt')
+ ckpt_tensor = 'my_embedding'
+
+ # Expected lookup result, using combiner='mean'.
+ expected_lookups = (
+ # example 0, ids [2], embedding = [7, 11]
+ (7., 11.),
+ # example 1, ids [0, 1], embedding = mean([1, 2] + [3, 5]) = [2, 3.5]
+ (2., 3.5),
+ # example 2, ids [], embedding = [0, 0]
+ (0., 0.),
+ # example 3, ids [1], embedding = [3, 5]
+ (3., 5.),
+ )
+
+ # Build columns.
+ categorical_column = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=vocabulary_size)
+ embedding_column = fc.embedding_column(
+ categorical_column, dimension=embedding_dimension,
+ ckpt_to_load_from=ckpt_path,
+ tensor_name_in_ckpt=ckpt_tensor)
+ state_manager = _TestStateManager()
+
+ # Provide sparse input and get dense result.
+ embedding_lookup = embedding_column.get_dense_tensor(
+ FeatureTransformationCache({
+ 'aaa': sparse_input
+ }), state_manager)
+
+ # Assert expected embedding variable and lookups.
+ global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
+ self.assertItemsEqual(
+ ('embedding_weights:0',), tuple([v.name for v in global_vars]))
+ with _initialized_session():
+ self.assertAllEqual(embedding_values, global_vars[0].eval())
+ self.assertAllEqual(expected_lookups, embedding_lookup.eval())
+
+ def test_linear_model(self):
+ # Inputs.
+ batch_size = 4
+ vocabulary_size = 3
+ sparse_input = sparse_tensor.SparseTensorValue(
+ # example 0, ids [2]
+ # example 1, ids [0, 1]
+ # example 2, ids []
+ # example 3, ids [1]
+ indices=((0, 0), (1, 0), (1, 4), (3, 0)),
+ values=(2, 0, 1, 1),
+ dense_shape=(batch_size, 5))
+
+ # Embedding variable.
+ embedding_dimension = 2
+ embedding_shape = (vocabulary_size, embedding_dimension)
+ zeros_embedding_values = np.zeros(embedding_shape)
+ def _initializer(shape, dtype, partition_info):
+ self.assertAllEqual(embedding_shape, shape)
+ self.assertEqual(dtypes.float32, dtype)
+ self.assertIsNone(partition_info)
+ return zeros_embedding_values
+
+ # Build columns.
+ categorical_column = fc_old.categorical_column_with_identity(
+ key='aaa', num_buckets=vocabulary_size)
+ embedding_column = fc_old.embedding_column(
+ categorical_column,
+ dimension=embedding_dimension,
+ initializer=_initializer)
+
+ with ops.Graph().as_default():
+ predictions = fc.linear_model({
+ categorical_column.name: sparse_input
+ }, (embedding_column,))
+ expected_var_names = (
+ 'linear_model/bias_weights:0',
+ 'linear_model/aaa_embedding/weights:0',
+ 'linear_model/aaa_embedding/embedding_weights:0',
+ )
+ self.assertItemsEqual(
+ expected_var_names,
+ [v.name for v in ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)])
+ trainable_vars = {
+ v.name: v for v in ops.get_collection(
+ ops.GraphKeys.TRAINABLE_VARIABLES)
+ }
+ self.assertItemsEqual(expected_var_names, trainable_vars.keys())
+ bias = trainable_vars['linear_model/bias_weights:0']
+ embedding_weights = trainable_vars[
+ 'linear_model/aaa_embedding/embedding_weights:0']
+ linear_weights = trainable_vars[
+ 'linear_model/aaa_embedding/weights:0']
+ with _initialized_session():
+ # Predictions with all zero weights.
+ self.assertAllClose(np.zeros((1,)), bias.eval())
+ self.assertAllClose(zeros_embedding_values, embedding_weights.eval())
+ self.assertAllClose(
+ np.zeros((embedding_dimension, 1)), linear_weights.eval())
+ self.assertAllClose(np.zeros((batch_size, 1)), predictions.eval())
+
+ # Predictions with all non-zero weights.
+ embedding_weights.assign((
+ (1., 2.), # id 0
+ (3., 5.), # id 1
+ (7., 11.) # id 2
+ )).eval()
+ linear_weights.assign(((4.,), (6.,))).eval()
+ # example 0, ids [2], embedding[0] = [7, 11]
+ # example 1, ids [0, 1], embedding[1] = mean([1, 2] + [3, 5]) = [2, 3.5]
+ # example 2, ids [], embedding[2] = [0, 0]
+ # example 3, ids [1], embedding[3] = [3, 5]
+ # sum(embeddings * linear_weights)
+ # = [4*7 + 6*11, 4*2 + 6*3.5, 4*0 + 6*0, 4*3 + 6*5] = [94, 29, 0, 42]
+ self.assertAllClose(((94.,), (29.,), (0.,), (42.,)), predictions.eval())
+
+ def test_keras_linear_model(self):
+ # Inputs.
+ batch_size = 4
+ vocabulary_size = 3
+ sparse_input = sparse_tensor.SparseTensorValue(
+ # example 0, ids [2]
+ # example 1, ids [0, 1]
+ # example 2, ids []
+ # example 3, ids [1]
+ indices=((0, 0), (1, 0), (1, 4), (3, 0)),
+ values=(2, 0, 1, 1),
+ dense_shape=(batch_size, 5))
+
+ # Embedding variable.
+ embedding_dimension = 2
+ embedding_shape = (vocabulary_size, embedding_dimension)
+ zeros_embedding_values = np.zeros(embedding_shape)
+
+ def _initializer(shape, dtype, partition_info):
+ self.assertAllEqual(embedding_shape, shape)
+ self.assertEqual(dtypes.float32, dtype)
+ self.assertIsNone(partition_info)
+ return zeros_embedding_values
+
+ # Build columns.
+ categorical_column = fc_old.categorical_column_with_identity(
+ key='aaa', num_buckets=vocabulary_size)
+ embedding_column = fc_old.embedding_column(
+ categorical_column,
+ dimension=embedding_dimension,
+ initializer=_initializer)
+
+ with ops.Graph().as_default():
+ predictions = get_keras_linear_model_predictions({
+ categorical_column.name: sparse_input
+ }, (embedding_column,))
+ expected_var_names = (
+ 'linear_model/bias_weights:0',
+ 'linear_model/aaa_embedding/weights:0',
+ 'linear_model/aaa_embedding/embedding_weights:0',
+ )
+ self.assertItemsEqual(
+ expected_var_names,
+ [v.name for v in ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)])
+ trainable_vars = {
+ v.name: v
+ for v in ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)
+ }
+ self.assertItemsEqual(expected_var_names, trainable_vars.keys())
+ bias = trainable_vars['linear_model/bias_weights:0']
+ embedding_weights = trainable_vars[
+ 'linear_model/aaa_embedding/embedding_weights:0']
+ linear_weights = trainable_vars['linear_model/aaa_embedding/weights:0']
+ with _initialized_session():
+ # Predictions with all zero weights.
+ self.assertAllClose(np.zeros((1,)), bias.eval())
+ self.assertAllClose(zeros_embedding_values, embedding_weights.eval())
+ self.assertAllClose(
+ np.zeros((embedding_dimension, 1)), linear_weights.eval())
+ self.assertAllClose(np.zeros((batch_size, 1)), predictions.eval())
+
+ # Predictions with all non-zero weights.
+ embedding_weights.assign((
+ (1., 2.), # id 0
+ (3., 5.), # id 1
+ (7., 11.) # id 2
+ )).eval()
+ linear_weights.assign(((4.,), (6.,))).eval()
+ # example 0, ids [2], embedding[0] = [7, 11]
+ # example 1, ids [0, 1], embedding[1] = mean([1, 2] + [3, 5]) = [2, 3.5]
+ # example 2, ids [], embedding[2] = [0, 0]
+ # example 3, ids [1], embedding[3] = [3, 5]
+ # sum(embeddings * linear_weights)
+ # = [4*7 + 6*11, 4*2 + 6*3.5, 4*0 + 6*0, 4*3 + 6*5] = [94, 29, 0, 42]
+ self.assertAllClose(((94.,), (29.,), (0.,), (42.,)), predictions.eval())
+
+ def test_input_layer(self):
+ # Inputs.
+ vocabulary_size = 3
+ sparse_input = sparse_tensor.SparseTensorValue(
+ # example 0, ids [2]
+ # example 1, ids [0, 1]
+ # example 2, ids []
+ # example 3, ids [1]
+ indices=((0, 0), (1, 0), (1, 4), (3, 0)),
+ values=(2, 0, 1, 1),
+ dense_shape=(4, 5))
+
+ # Embedding variable.
+ embedding_dimension = 2
+ embedding_values = (
+ (1., 2.), # id 0
+ (3., 5.), # id 1
+ (7., 11.) # id 2
+ )
+ def _initializer(shape, dtype, partition_info):
+ self.assertAllEqual((vocabulary_size, embedding_dimension), shape)
+ self.assertEqual(dtypes.float32, dtype)
+ self.assertIsNone(partition_info)
+ return embedding_values
+
+ # Expected lookup result, using combiner='mean'.
+ expected_lookups = (
+ # example 0, ids [2], embedding = [7, 11]
+ (7., 11.),
+ # example 1, ids [0, 1], embedding = mean([1, 2] + [3, 5]) = [2, 3.5]
+ (2., 3.5),
+ # example 2, ids [], embedding = [0, 0]
+ (0., 0.),
+ # example 3, ids [1], embedding = [3, 5]
+ (3., 5.),
+ )
+
+ # Build columns.
+ categorical_column = fc_old.categorical_column_with_identity(
+ key='aaa', num_buckets=vocabulary_size)
+ embedding_column = fc_old.embedding_column(
+ categorical_column,
+ dimension=embedding_dimension,
+ initializer=_initializer)
+
+ # Provide sparse input and get dense result.
+ input_layer = fc.input_layer({'aaa': sparse_input}, (embedding_column,))
+
+ # Assert expected embedding variable and lookups.
+ global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
+ self.assertItemsEqual(
+ ('input_layer/aaa_embedding/embedding_weights:0',),
+ tuple([v.name for v in global_vars]))
+ trainable_vars = ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)
+ self.assertItemsEqual(
+ ('input_layer/aaa_embedding/embedding_weights:0',),
+ tuple([v.name for v in trainable_vars]))
+ with _initialized_session():
+ self.assertAllEqual(embedding_values, trainable_vars[0].eval())
+ self.assertAllEqual(expected_lookups, input_layer.eval())
+
+ def test_input_layer_not_trainable(self):
+ # Inputs.
+ vocabulary_size = 3
+ sparse_input = sparse_tensor.SparseTensorValue(
+ # example 0, ids [2]
+ # example 1, ids [0, 1]
+ # example 2, ids []
+ # example 3, ids [1]
+ indices=((0, 0), (1, 0), (1, 4), (3, 0)),
+ values=(2, 0, 1, 1),
+ dense_shape=(4, 5))
+
+ # Embedding variable.
+ embedding_dimension = 2
+ embedding_values = (
+ (1., 2.), # id 0
+ (3., 5.), # id 1
+ (7., 11.) # id 2
+ )
+ def _initializer(shape, dtype, partition_info):
+ self.assertAllEqual((vocabulary_size, embedding_dimension), shape)
+ self.assertEqual(dtypes.float32, dtype)
+ self.assertIsNone(partition_info)
+ return embedding_values
+
+ # Expected lookup result, using combiner='mean'.
+ expected_lookups = (
+ # example 0, ids [2], embedding = [7, 11]
+ (7., 11.),
+ # example 1, ids [0, 1], embedding = mean([1, 2] + [3, 5]) = [2, 3.5]
+ (2., 3.5),
+ # example 2, ids [], embedding = [0, 0]
+ (0., 0.),
+ # example 3, ids [1], embedding = [3, 5]
+ (3., 5.),
+ )
+
+ # Build columns.
+ categorical_column = fc_old.categorical_column_with_identity(
+ key='aaa', num_buckets=vocabulary_size)
+ embedding_column = fc_old.embedding_column(
+ categorical_column,
+ dimension=embedding_dimension,
+ initializer=_initializer,
+ trainable=False)
+
+ # Provide sparse input and get dense result.
+ input_layer = fc.input_layer({'aaa': sparse_input}, (embedding_column,))
+
+ # Assert expected embedding variable and lookups.
+ global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
+ self.assertItemsEqual(
+ ('input_layer/aaa_embedding/embedding_weights:0',),
+ tuple([v.name for v in global_vars]))
+ self.assertItemsEqual(
+ [], ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES))
+ with _initialized_session():
+ self.assertAllEqual(embedding_values, global_vars[0].eval())
+ self.assertAllEqual(expected_lookups, input_layer.eval())
+
+
+class _TestSharedEmbeddingStateManager(StateManager):
+ """Manages the state for shared embedding columns.
+
+ This can handle multiple groups of shared embedding columns.
+ """
+
+ def __init__(self, trainable=True):
+ # Dict of shared_embedding_collection_name to a dict of variables.
+ self._all_variables = {}
+ self._trainable = trainable
+
+ def get_variable(self,
+ feature_column,
+ name,
+ shape,
+ dtype=None,
+ initializer=None):
+ if not isinstance(feature_column, fc.SharedEmbeddingColumn):
+ raise ValueError(
+ 'SharedEmbeddingStateManager can only handle SharedEmbeddingColumns. '
+ 'Given type: {} '.format(type(feature_column)))
+
+ collection_name = feature_column.shared_collection_name
+ if collection_name not in self._all_variables:
+ self._all_variables[collection_name] = {}
+ var_dict = self._all_variables[collection_name]
+ if name in var_dict:
+ return var_dict[name]
+ else:
+ var = variable_scope.get_variable(
+ name=name,
+ shape=shape,
+ initializer=initializer,
+ trainable=self._trainable)
+ var_dict[name] = var
+ return var
+
+
+class SharedEmbeddingColumnTest(test.TestCase):
+
+ def test_defaults(self):
+ categorical_column_a = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=3)
+ categorical_column_b = fc.categorical_column_with_identity(
+ key='bbb', num_buckets=3)
+ embedding_dimension = 2
+ embedding_column_b, embedding_column_a = fc.shared_embedding_columns(
+ [categorical_column_b, categorical_column_a],
+ dimension=embedding_dimension)
+ self.assertIs(categorical_column_a, embedding_column_a.categorical_column)
+ self.assertIs(categorical_column_b, embedding_column_b.categorical_column)
+ self.assertEqual(embedding_dimension, embedding_column_a.dimension)
+ self.assertEqual(embedding_dimension, embedding_column_b.dimension)
+ self.assertEqual('mean', embedding_column_a.combiner)
+ self.assertEqual('mean', embedding_column_b.combiner)
+ self.assertIsNone(embedding_column_a.ckpt_to_load_from)
+ self.assertIsNone(embedding_column_b.ckpt_to_load_from)
+ self.assertEqual('aaa_bbb_shared_embedding',
+ embedding_column_a.shared_collection_name)
+ self.assertEqual('aaa_bbb_shared_embedding',
+ embedding_column_b.shared_collection_name)
+ self.assertIsNone(embedding_column_a.tensor_name_in_ckpt)
+ self.assertIsNone(embedding_column_b.tensor_name_in_ckpt)
+ self.assertIsNone(embedding_column_a.max_norm)
+ self.assertIsNone(embedding_column_b.max_norm)
+ self.assertTrue(embedding_column_a.trainable)
+ self.assertTrue(embedding_column_b.trainable)
+ self.assertEqual('aaa_shared_embedding', embedding_column_a.name)
+ self.assertEqual('bbb_shared_embedding', embedding_column_b.name)
+ self.assertEqual((embedding_dimension,), embedding_column_a.variable_shape)
+ self.assertEqual((embedding_dimension,), embedding_column_b.variable_shape)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int64)
+ }, embedding_column_a.parse_example_spec)
+ self.assertEqual({
+ 'bbb': parsing_ops.VarLenFeature(dtypes.int64)
+ }, embedding_column_b.parse_example_spec)
+
+ def test_all_constructor_args(self):
+ categorical_column_a = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=3)
+ categorical_column_b = fc.categorical_column_with_identity(
+ key='bbb', num_buckets=3)
+ embedding_dimension = 2
+ embedding_column_a, embedding_column_b = fc.shared_embedding_columns(
+ [categorical_column_a, categorical_column_b],
+ dimension=embedding_dimension,
+ combiner='my_combiner',
+ initializer=lambda: 'my_initializer',
+ shared_embedding_collection_name='shared_embedding_collection_name',
+ ckpt_to_load_from='my_ckpt',
+ tensor_name_in_ckpt='my_ckpt_tensor',
+ max_norm=42.,
+ trainable=False)
+ self.assertIs(categorical_column_a, embedding_column_a.categorical_column)
+ self.assertIs(categorical_column_b, embedding_column_b.categorical_column)
+ self.assertEqual(embedding_dimension, embedding_column_a.dimension)
+ self.assertEqual(embedding_dimension, embedding_column_b.dimension)
+ self.assertEqual('my_combiner', embedding_column_a.combiner)
+ self.assertEqual('my_combiner', embedding_column_b.combiner)
+ self.assertEqual('shared_embedding_collection_name',
+ embedding_column_a.shared_collection_name)
+ self.assertEqual('shared_embedding_collection_name',
+ embedding_column_b.shared_collection_name)
+ self.assertEqual('my_ckpt', embedding_column_a.ckpt_to_load_from)
+ self.assertEqual('my_ckpt', embedding_column_b.ckpt_to_load_from)
+ self.assertEqual('my_ckpt_tensor', embedding_column_a.tensor_name_in_ckpt)
+ self.assertEqual('my_ckpt_tensor', embedding_column_b.tensor_name_in_ckpt)
+ self.assertEqual(42., embedding_column_a.max_norm)
+ self.assertEqual(42., embedding_column_b.max_norm)
+ self.assertFalse(embedding_column_a.trainable)
+ self.assertFalse(embedding_column_b.trainable)
+ self.assertEqual('aaa_shared_embedding', embedding_column_a.name)
+ self.assertEqual('bbb_shared_embedding', embedding_column_b.name)
+ self.assertEqual((embedding_dimension,), embedding_column_a.variable_shape)
+ self.assertEqual((embedding_dimension,), embedding_column_b.variable_shape)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int64)
+ }, embedding_column_a.parse_example_spec)
+ self.assertEqual({
+ 'bbb': parsing_ops.VarLenFeature(dtypes.int64)
+ }, embedding_column_b.parse_example_spec)
+
+ def test_deep_copy(self):
+ categorical_column_a = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=3)
+ categorical_column_b = fc.categorical_column_with_identity(
+ key='bbb', num_buckets=3)
+ embedding_dimension = 2
+ original_a, _ = fc.shared_embedding_columns(
+ [categorical_column_a, categorical_column_b],
+ dimension=embedding_dimension,
+ combiner='my_combiner',
+ initializer=lambda: 'my_initializer',
+ shared_embedding_collection_name='shared_embedding_collection_name',
+ ckpt_to_load_from='my_ckpt',
+ tensor_name_in_ckpt='my_ckpt_tensor',
+ max_norm=42., trainable=False)
+ for embedding_column_a in (original_a, copy.deepcopy(original_a)):
+ self.assertEqual('aaa', embedding_column_a.categorical_column.name)
+ self.assertEqual(3, embedding_column_a.categorical_column.num_buckets)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int64)
+ }, embedding_column_a.categorical_column.parse_example_spec)
+
+ self.assertEqual(embedding_dimension, embedding_column_a.dimension)
+ self.assertEqual('my_combiner', embedding_column_a.combiner)
+ self.assertEqual('shared_embedding_collection_name',
+ embedding_column_a.shared_collection_name)
+ self.assertEqual('my_ckpt', embedding_column_a.ckpt_to_load_from)
+ self.assertEqual('my_ckpt_tensor', embedding_column_a.tensor_name_in_ckpt)
+ self.assertEqual(42., embedding_column_a.max_norm)
+ self.assertFalse(embedding_column_a.trainable)
+ self.assertEqual('aaa_shared_embedding', embedding_column_a.name)
+ self.assertEqual((embedding_dimension,),
+ embedding_column_a.variable_shape)
+ self.assertEqual({
+ 'aaa': parsing_ops.VarLenFeature(dtypes.int64)
+ }, embedding_column_a.parse_example_spec)
+
+ def test_invalid_initializer(self):
+ categorical_column_a = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=3)
+ categorical_column_b = fc.categorical_column_with_identity(
+ key='bbb', num_buckets=3)
+ with self.assertRaisesRegexp(ValueError, 'initializer must be callable'):
+ fc.shared_embedding_columns(
+ [categorical_column_a, categorical_column_b], dimension=2,
+ initializer='not_fn')
+
+ def test_incompatible_column_type(self):
+ categorical_column_a = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=3)
+ categorical_column_b = fc.categorical_column_with_identity(
+ key='bbb', num_buckets=3)
+ categorical_column_c = fc.categorical_column_with_hash_bucket(
+ key='ccc', hash_bucket_size=3)
+ with self.assertRaisesRegexp(
+ ValueError, 'all categorical_columns must have the same type.*'
+ 'IdentityCategoricalColumn.*HashedCategoricalColumn'):
+ fc.shared_embedding_columns(
+ [categorical_column_a, categorical_column_b, categorical_column_c],
+ dimension=2)
+
+ def test_weighted_categorical_column_ok(self):
+ categorical_column_a = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=3)
+ weighted_categorical_column_a = fc.weighted_categorical_column(
+ categorical_column_a, weight_feature_key='aaa_weights')
+ categorical_column_b = fc.categorical_column_with_identity(
+ key='bbb', num_buckets=3)
+ weighted_categorical_column_b = fc.weighted_categorical_column(
+ categorical_column_b, weight_feature_key='bbb_weights')
+ fc.shared_embedding_columns(
+ [weighted_categorical_column_a, categorical_column_b], dimension=2)
+ fc.shared_embedding_columns(
+ [categorical_column_a, weighted_categorical_column_b], dimension=2)
+ fc.shared_embedding_columns(
+ [weighted_categorical_column_a, weighted_categorical_column_b],
+ dimension=2)
+
+ def test_parse_example(self):
+ a = fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=('omar', 'stringer', 'marlo'))
+ b = fc.categorical_column_with_vocabulary_list(
+ key='bbb', vocabulary_list=('omar', 'stringer', 'marlo'))
+ a_embedded, b_embedded = fc.shared_embedding_columns(
+ [a, b], dimension=2)
+ data = example_pb2.Example(features=feature_pb2.Features(
+ feature={
+ 'aaa':
+ feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
+ value=[b'omar', b'stringer'])),
+ 'bbb':
+ feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
+ value=[b'stringer', b'marlo'])),
+ }))
+ features = parsing_ops.parse_example(
+ serialized=[data.SerializeToString()],
+ features=fc.make_parse_example_spec([a_embedded, b_embedded]))
+ self.assertIn('aaa', features)
+ self.assertIn('bbb', features)
+ with self.test_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=[[0, 0], [0, 1]],
+ values=np.array([b'omar', b'stringer'], dtype=np.object_),
+ dense_shape=[1, 2]),
+ features['aaa'].eval())
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=[[0, 0], [0, 1]],
+ values=np.array([b'stringer', b'marlo'], dtype=np.object_),
+ dense_shape=[1, 2]),
+ features['bbb'].eval())
+
+ def test_transform_feature(self):
+ a = fc.categorical_column_with_identity(key='aaa', num_buckets=3)
+ b = fc.categorical_column_with_identity(key='bbb', num_buckets=3)
+ a_embedded, b_embedded = fc.shared_embedding_columns(
+ [a, b], dimension=2)
+ features = {
+ 'aaa': sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 1, 0),
+ dense_shape=(2, 2)),
+ 'bbb': sparse_tensor.SparseTensor(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(1, 2, 1),
+ dense_shape=(2, 2)),
+ }
+ outputs = _transform_features(features, [a, a_embedded, b, b_embedded],
+ None)
+ output_a = outputs[a]
+ output_a_embedded = outputs[a_embedded]
+ output_b = outputs[b]
+ output_b_embedded = outputs[b_embedded]
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self, output_a.eval(), output_a_embedded.eval())
+ _assert_sparse_tensor_value(
+ self, output_b.eval(), output_b_embedded.eval())
+
+ def test_get_dense_tensor(self):
+ # Inputs.
+ vocabulary_size = 3
+ # -1 values are ignored.
+ input_a = np.array(
+ [[2, -1, -1], # example 0, ids [2]
+ [0, 1, -1]]) # example 1, ids [0, 1]
+ input_b = np.array(
+ [[0, -1, -1], # example 0, ids [0]
+ [-1, -1, -1]]) # example 1, ids []
+ input_features = {
+ 'aaa': input_a,
+ 'bbb': input_b
+ }
+
+ # Embedding variable.
+ embedding_dimension = 2
+ embedding_values = (
+ (1., 2.), # id 0
+ (3., 5.), # id 1
+ (7., 11.) # id 2
+ )
+ def _initializer(shape, dtype, partition_info):
+ self.assertAllEqual((vocabulary_size, embedding_dimension), shape)
+ self.assertEqual(dtypes.float32, dtype)
+ self.assertIsNone(partition_info)
+ return embedding_values
+
+ # Expected lookup result, using combiner='mean'.
+ expected_lookups_a = (
+ # example 0:
+ (7., 11.), # ids [2], embedding = [7, 11]
+ # example 1:
+ (2., 3.5), # ids [0, 1], embedding = mean([1, 2] + [3, 5]) = [2, 3.5]
+ )
+ expected_lookups_b = (
+ # example 0:
+ (1., 2.), # ids [0], embedding = [1, 2]
+ # example 1:
+ (0., 0.), # ids [], embedding = [0, 0]
+ )
+
+ # Build columns.
+ categorical_column_a = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=vocabulary_size)
+ categorical_column_b = fc.categorical_column_with_identity(
+ key='bbb', num_buckets=vocabulary_size)
+ embedding_column_a, embedding_column_b = fc.shared_embedding_columns(
+ [categorical_column_a, categorical_column_b],
+ dimension=embedding_dimension, initializer=_initializer)
+ state_manager = _TestSharedEmbeddingStateManager()
+
+ # Provide sparse input and get dense result.
+ embedding_lookup_a = embedding_column_a.get_dense_tensor(
+ FeatureTransformationCache(input_features), state_manager)
+ embedding_lookup_b = embedding_column_b.get_dense_tensor(
+ FeatureTransformationCache(input_features), state_manager)
+
+ # Assert expected embedding variable and lookups.
+ global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
+ self.assertItemsEqual(('embedding_weights:0',),
+ tuple([v.name for v in global_vars]))
+ embedding_var = global_vars[0]
+ with _initialized_session():
+ self.assertAllEqual(embedding_values, embedding_var.eval())
+ self.assertAllEqual(expected_lookups_a, embedding_lookup_a.eval())
+ self.assertAllEqual(expected_lookups_b, embedding_lookup_b.eval())
+
+ def DISABLED_test_get_dense_tensor_weight_collections(self):
+ # Inputs.
+ vocabulary_size = 3
+ # -1 values are ignored.
+ input_a = np.array([
+ [2, -1, -1], # example 0, ids [2]
+ [0, 1, -1]
+ ]) # example 1, ids [0, 1]
+ input_b = np.array([
+ [0, -1, -1], # example 0, ids [0]
+ [-1, -1, -1]
+ ]) # example 1, ids []
+ input_features = {'aaa': input_a, 'bbb': input_b}
+
+ # Embedding variable.
+ embedding_dimension = 2
+ embedding_values = (
+ (1., 2.), # id 0
+ (3., 5.), # id 1
+ (7., 11.) # id 2
+ )
+
+ def _initializer(shape, dtype, partition_info):
+ self.assertAllEqual((vocabulary_size, embedding_dimension), shape)
+ self.assertEqual(dtypes.float32, dtype)
+ self.assertIsNone(partition_info)
+ return embedding_values
+
+ # Build columns.
+ categorical_column_a = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=vocabulary_size)
+ categorical_column_b = fc.categorical_column_with_identity(
+ key='bbb', num_buckets=vocabulary_size)
+ embedding_column_a, embedding_column_b = fc.shared_embedding_columns(
+ [categorical_column_a, categorical_column_b],
+ dimension=embedding_dimension,
+ initializer=_initializer)
+
+ fc.input_layer(
+ input_features, [embedding_column_a, embedding_column_b],
+ weight_collections=('my_vars',))
+
+ # Assert expected embedding variable and lookups.
+ global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
+ self.assertItemsEqual(
+ ('input_layer/aaa_bbb_shared_embedding/embedding_weights:0',),
+ tuple(v.name for v in global_vars))
+ my_vars = ops.get_collection('my_vars')
+ self.assertItemsEqual(
+ ('input_layer/aaa_bbb_shared_embedding/embedding_weights:0',),
+ tuple(v.name for v in my_vars))
+
+ def test_get_dense_tensor_placeholder_inputs(self):
+ # Inputs.
+ vocabulary_size = 3
+ # -1 values are ignored.
+ input_a = np.array(
+ [[2, -1, -1], # example 0, ids [2]
+ [0, 1, -1]]) # example 1, ids [0, 1]
+ input_b = np.array(
+ [[0, -1, -1], # example 0, ids [0]
+ [-1, -1, -1]]) # example 1, ids []
+ # Specify shape, because dense input must have rank specified.
+ input_a_placeholder = array_ops.placeholder(
+ dtype=dtypes.int64, shape=[None, 3])
+ input_b_placeholder = array_ops.placeholder(
+ dtype=dtypes.int64, shape=[None, 3])
+ input_features = {
+ 'aaa': input_a_placeholder,
+ 'bbb': input_b_placeholder,
+ }
+ feed_dict = {
+ input_a_placeholder: input_a,
+ input_b_placeholder: input_b,
+ }
+
+ # Embedding variable.
+ embedding_dimension = 2
+ embedding_values = (
+ (1., 2.), # id 0
+ (3., 5.), # id 1
+ (7., 11.) # id 2
+ )
+ def _initializer(shape, dtype, partition_info):
+ self.assertAllEqual((vocabulary_size, embedding_dimension), shape)
+ self.assertEqual(dtypes.float32, dtype)
+ self.assertIsNone(partition_info)
+ return embedding_values
+
+ # Build columns.
+ categorical_column_a = fc.categorical_column_with_identity(
+ key='aaa', num_buckets=vocabulary_size)
+ categorical_column_b = fc.categorical_column_with_identity(
+ key='bbb', num_buckets=vocabulary_size)
+ embedding_column_a, embedding_column_b = fc.shared_embedding_columns(
+ [categorical_column_a, categorical_column_b],
+ dimension=embedding_dimension, initializer=_initializer)
+ state_manager = _TestSharedEmbeddingStateManager()
+
+ # Provide sparse input and get dense result.
+ embedding_lookup_a = embedding_column_a.get_dense_tensor(
+ FeatureTransformationCache(input_features), state_manager)
+ embedding_lookup_b = embedding_column_b.get_dense_tensor(
+ FeatureTransformationCache(input_features), state_manager)
+
+ with _initialized_session() as sess:
+ sess.run([embedding_lookup_a, embedding_lookup_b], feed_dict=feed_dict)
+
+ def test_linear_model(self):
+ # Inputs.
+ batch_size = 2
+ vocabulary_size = 3
+ # -1 values are ignored.
+ input_a = np.array(
+ [[2, -1, -1], # example 0, ids [2]
+ [0, 1, -1]]) # example 1, ids [0, 1]
+ input_b = np.array(
+ [[0, -1, -1], # example 0, ids [0]
+ [-1, -1, -1]]) # example 1, ids []
+
+ # Embedding variable.
+ embedding_dimension = 2
+ embedding_shape = (vocabulary_size, embedding_dimension)
+ zeros_embedding_values = np.zeros(embedding_shape)
+ def _initializer(shape, dtype, partition_info):
+ self.assertAllEqual(embedding_shape, shape)
+ self.assertEqual(dtypes.float32, dtype)
+ self.assertIsNone(partition_info)
+ return zeros_embedding_values
+
+ # Build columns.
+ categorical_column_a = fc_old.categorical_column_with_identity(
+ key='aaa', num_buckets=vocabulary_size)
+ categorical_column_b = fc_old.categorical_column_with_identity(
+ key='bbb', num_buckets=vocabulary_size)
+ embedding_column_a, embedding_column_b = fc_old.shared_embedding_columns(
+ [categorical_column_a, categorical_column_b],
+ dimension=embedding_dimension,
+ initializer=_initializer)
+
+ with ops.Graph().as_default():
+ predictions = fc.linear_model({
+ categorical_column_a.name: input_a,
+ categorical_column_b.name: input_b,
+ }, (embedding_column_a, embedding_column_b))
+ # Linear weights do not follow the column name. But this is a rare use
+ # case, and fixing it would add too much complexity to the code.
+ expected_var_names = (
+ 'linear_model/bias_weights:0',
+ 'linear_model/aaa_bbb_shared_embedding/weights:0',
+ 'linear_model/aaa_bbb_shared_embedding/embedding_weights:0',
+ 'linear_model/aaa_bbb_shared_embedding_1/weights:0',
+ )
+ self.assertItemsEqual(
+ expected_var_names,
+ [v.name for v in ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)])
+ trainable_vars = {
+ v.name: v for v in ops.get_collection(
+ ops.GraphKeys.TRAINABLE_VARIABLES)
+ }
+ self.assertItemsEqual(expected_var_names, trainable_vars.keys())
+ bias = trainable_vars['linear_model/bias_weights:0']
+ embedding_weights = trainable_vars[
+ 'linear_model/aaa_bbb_shared_embedding/embedding_weights:0']
+ linear_weights_a = trainable_vars[
+ 'linear_model/aaa_bbb_shared_embedding/weights:0']
+ linear_weights_b = trainable_vars[
+ 'linear_model/aaa_bbb_shared_embedding_1/weights:0']
+ with _initialized_session():
+ # Predictions with all zero weights.
+ self.assertAllClose(np.zeros((1,)), bias.eval())
+ self.assertAllClose(zeros_embedding_values, embedding_weights.eval())
+ self.assertAllClose(
+ np.zeros((embedding_dimension, 1)), linear_weights_a.eval())
+ self.assertAllClose(
+ np.zeros((embedding_dimension, 1)), linear_weights_b.eval())
+ self.assertAllClose(np.zeros((batch_size, 1)), predictions.eval())
+
+ # Predictions with all non-zero weights.
+ embedding_weights.assign((
+ (1., 2.), # id 0
+ (3., 5.), # id 1
+ (7., 11.) # id 2
+ )).eval()
+ linear_weights_a.assign(((4.,), (6.,))).eval()
+ # example 0, ids [2], embedding[0] = [7, 11]
+ # example 1, ids [0, 1], embedding[1] = mean([1, 2] + [3, 5]) = [2, 3.5]
+ # sum(embeddings * linear_weights)
+ # = [4*7 + 6*11, 4*2 + 6*3.5] = [94, 29]
+ linear_weights_b.assign(((3.,), (5.,))).eval()
+ # example 0, ids [0], embedding[0] = [1, 2]
+ # example 1, ids [], embedding[1] = 0, 0]
+ # sum(embeddings * linear_weights)
+ # = [3*1 + 5*2, 3*0 +5*0] = [13, 0]
+ self.assertAllClose([[94. + 13.], [29.]], predictions.eval())
+
+ def test_keras_linear_model(self):
+ # Inputs.
+ batch_size = 2
+ vocabulary_size = 3
+ # -1 values are ignored.
+ input_a = np.array([
+ [2, -1, -1], # example 0, ids [2]
+ [0, 1, -1]
+ ]) # example 1, ids [0, 1]
+ input_b = np.array([
+ [0, -1, -1], # example 0, ids [0]
+ [-1, -1, -1]
+ ]) # example 1, ids []
+
+ # Embedding variable.
+ embedding_dimension = 2
+ embedding_shape = (vocabulary_size, embedding_dimension)
+ zeros_embedding_values = np.zeros(embedding_shape)
+
+ def _initializer(shape, dtype, partition_info):
+ self.assertAllEqual(embedding_shape, shape)
+ self.assertEqual(dtypes.float32, dtype)
+ self.assertIsNone(partition_info)
+ return zeros_embedding_values
+
+ # Build columns.
+ categorical_column_a = fc_old.categorical_column_with_identity(
+ key='aaa', num_buckets=vocabulary_size)
+ categorical_column_b = fc_old.categorical_column_with_identity(
+ key='bbb', num_buckets=vocabulary_size)
+ embedding_column_a, embedding_column_b = fc_old.shared_embedding_columns(
+ [categorical_column_a, categorical_column_b],
+ dimension=embedding_dimension,
+ initializer=_initializer)
+
+ with ops.Graph().as_default():
+ predictions = get_keras_linear_model_predictions({
+ categorical_column_a.name: input_a,
+ categorical_column_b.name: input_b,
+ }, (embedding_column_a, embedding_column_b))
+ # Linear weights do not follow the column name. But this is a rare use
+ # case, and fixing it would add too much complexity to the code.
+ expected_var_names = (
+ 'linear_model/bias_weights:0',
+ 'linear_model/aaa_bbb_shared_embedding/weights:0',
+ 'linear_model/aaa_bbb_shared_embedding/embedding_weights:0',
+ 'linear_model/aaa_bbb_shared_embedding_1/weights:0',
+ )
+ self.assertItemsEqual(
+ expected_var_names,
+ [v.name for v in ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)])
+ trainable_vars = {
+ v.name: v
+ for v in ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)
+ }
+ self.assertItemsEqual(expected_var_names, trainable_vars.keys())
+ bias = trainable_vars['linear_model/bias_weights:0']
+ embedding_weights = trainable_vars[
+ 'linear_model/aaa_bbb_shared_embedding/embedding_weights:0']
+ linear_weights_a = trainable_vars[
+ 'linear_model/aaa_bbb_shared_embedding/weights:0']
+ linear_weights_b = trainable_vars[
+ 'linear_model/aaa_bbb_shared_embedding_1/weights:0']
+ with _initialized_session():
+ # Predictions with all zero weights.
+ self.assertAllClose(np.zeros((1,)), bias.eval())
+ self.assertAllClose(zeros_embedding_values, embedding_weights.eval())
+ self.assertAllClose(
+ np.zeros((embedding_dimension, 1)), linear_weights_a.eval())
+ self.assertAllClose(
+ np.zeros((embedding_dimension, 1)), linear_weights_b.eval())
+ self.assertAllClose(np.zeros((batch_size, 1)), predictions.eval())
+
+ # Predictions with all non-zero weights.
+ embedding_weights.assign((
+ (1., 2.), # id 0
+ (3., 5.), # id 1
+ (7., 11.) # id 2
+ )).eval()
+ linear_weights_a.assign(((4.,), (6.,))).eval()
+ # example 0, ids [2], embedding[0] = [7, 11]
+ # example 1, ids [0, 1], embedding[1] = mean([1, 2] + [3, 5]) = [2, 3.5]
+ # sum(embeddings * linear_weights)
+ # = [4*7 + 6*11, 4*2 + 6*3.5] = [94, 29]
+ linear_weights_b.assign(((3.,), (5.,))).eval()
+ # example 0, ids [0], embedding[0] = [1, 2]
+ # example 1, ids [], embedding[1] = 0, 0]
+ # sum(embeddings * linear_weights)
+ # = [3*1 + 5*2, 3*0 +5*0] = [13, 0]
+ self.assertAllClose([[94. + 13.], [29.]], predictions.eval())
+
+ def _test_input_layer(self, trainable=True):
+ # Inputs.
+ vocabulary_size = 3
+ sparse_input_a = sparse_tensor.SparseTensorValue(
+ # example 0, ids [2]
+ # example 1, ids [0, 1]
+ indices=((0, 0), (1, 0), (1, 4)),
+ values=(2, 0, 1),
+ dense_shape=(2, 5))
+ sparse_input_b = sparse_tensor.SparseTensorValue(
+ # example 0, ids [0]
+ # example 1, ids []
+ indices=((0, 0),),
+ values=(0,),
+ dense_shape=(2, 5))
+
+ # Embedding variable.
+ embedding_dimension = 2
+ embedding_values = (
+ (1., 2.), # id 0
+ (3., 5.), # id 1
+ (7., 11.) # id 2
+ )
+ def _initializer(shape, dtype, partition_info):
+ self.assertAllEqual((vocabulary_size, embedding_dimension), shape)
+ self.assertEqual(dtypes.float32, dtype)
+ self.assertIsNone(partition_info)
+ return embedding_values
+
+ # Expected lookup result, using combiner='mean'.
+ expected_lookups = (
+ # example 0:
+ # A ids [2], embedding = [7, 11]
+ # B ids [0], embedding = [1, 2]
+ (7., 11., 1., 2.),
+ # example 1:
+ # A ids [0, 1], embedding = mean([1, 2] + [3, 5]) = [2, 3.5]
+ # B ids [], embedding = [0, 0]
+ (2., 3.5, 0., 0.),
+ )
+
+ # Build columns.
+ categorical_column_a = fc_old.categorical_column_with_identity(
+ key='aaa', num_buckets=vocabulary_size)
+ categorical_column_b = fc_old.categorical_column_with_identity(
+ key='bbb', num_buckets=vocabulary_size)
+ embedding_column_a, embedding_column_b = fc_old.shared_embedding_columns(
+ [categorical_column_a, categorical_column_b],
+ dimension=embedding_dimension,
+ initializer=_initializer,
+ trainable=trainable)
+
+ # Provide sparse input and get dense result.
+ input_layer = fc.input_layer(
+ features={'aaa': sparse_input_a, 'bbb': sparse_input_b},
+ feature_columns=(embedding_column_b, embedding_column_a))
+
+ # Assert expected embedding variable and lookups.
+ global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
+ self.assertItemsEqual(
+ ['input_layer/aaa_bbb_shared_embedding/embedding_weights:0'],
+ tuple([v.name for v in global_vars]))
+ trainable_vars = ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)
+ if trainable:
+ self.assertItemsEqual(
+ ['input_layer/aaa_bbb_shared_embedding/embedding_weights:0'],
+ tuple([v.name for v in trainable_vars]))
+ else:
+ self.assertItemsEqual([], tuple([v.name for v in trainable_vars]))
+ shared_embedding_vars = global_vars
+ with _initialized_session():
+ self.assertAllEqual(embedding_values, shared_embedding_vars[0].eval())
+ self.assertAllEqual(expected_lookups, input_layer.eval())
+
+ def test_input_layer(self):
+ self._test_input_layer()
+
+ def test_input_layer_no_trainable(self):
+ self._test_input_layer(trainable=False)
+
+
+class WeightedCategoricalColumnTest(test.TestCase):
+
+ def test_defaults(self):
+ column = fc.weighted_categorical_column(
+ categorical_column=fc.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values')
+ self.assertEqual('ids_weighted_by_values', column.name)
+ self.assertEqual(3, column.num_buckets)
+ self.assertEqual({
+ 'ids': parsing_ops.VarLenFeature(dtypes.int64),
+ 'values': parsing_ops.VarLenFeature(dtypes.float32)
+ }, column.parse_example_spec)
+
+ def test_deep_copy(self):
+ """Tests deepcopy of categorical_column_with_hash_bucket."""
+ original = fc.weighted_categorical_column(
+ categorical_column=fc.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values')
+ for column in (original, copy.deepcopy(original)):
+ self.assertEqual('ids_weighted_by_values', column.name)
+ self.assertEqual(3, column.num_buckets)
+ self.assertEqual({
+ 'ids': parsing_ops.VarLenFeature(dtypes.int64),
+ 'values': parsing_ops.VarLenFeature(dtypes.float32)
+ }, column.parse_example_spec)
+
+ def test_invalid_dtype_none(self):
+ with self.assertRaisesRegexp(ValueError, 'is not convertible to float'):
+ fc.weighted_categorical_column(
+ categorical_column=fc.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values',
+ dtype=None)
+
+ def test_invalid_dtype_string(self):
+ with self.assertRaisesRegexp(ValueError, 'is not convertible to float'):
+ fc.weighted_categorical_column(
+ categorical_column=fc.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values',
+ dtype=dtypes.string)
+
+ def test_invalid_input_dtype(self):
+ column = fc.weighted_categorical_column(
+ categorical_column=fc.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values')
+ strings = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('omar', 'stringer', 'marlo'),
+ dense_shape=(2, 2))
+ with self.assertRaisesRegexp(ValueError, 'Bad dtype'):
+ _transform_features({'ids': strings, 'values': strings}, (column,), None)
+
+ def test_column_name_collision(self):
+ with self.assertRaisesRegexp(ValueError, r'Parse config.*already exists'):
+ fc.weighted_categorical_column(
+ categorical_column=fc.categorical_column_with_identity(
+ key='aaa', num_buckets=3),
+ weight_feature_key='aaa').parse_example_spec()
+
+ def test_missing_weights(self):
+ column = fc.weighted_categorical_column(
+ categorical_column=fc.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values')
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=('omar', 'stringer', 'marlo'),
+ dense_shape=(2, 2))
+ with self.assertRaisesRegexp(
+ ValueError, 'values is not in features dictionary'):
+ _transform_features({'ids': inputs}, (column,), None)
+
+ def test_parse_example(self):
+ a = fc.categorical_column_with_vocabulary_list(
+ key='aaa', vocabulary_list=('omar', 'stringer', 'marlo'))
+ a_weighted = fc.weighted_categorical_column(a, weight_feature_key='weights')
+ data = example_pb2.Example(features=feature_pb2.Features(
+ feature={
+ 'aaa':
+ feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
+ value=[b'omar', b'stringer'])),
+ 'weights':
+ feature_pb2.Feature(float_list=feature_pb2.FloatList(
+ value=[1., 10.]))
+ }))
+ features = parsing_ops.parse_example(
+ serialized=[data.SerializeToString()],
+ features=fc.make_parse_example_spec([a_weighted]))
+ self.assertIn('aaa', features)
+ self.assertIn('weights', features)
+ with self.test_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=[[0, 0], [0, 1]],
+ values=np.array([b'omar', b'stringer'], dtype=np.object_),
+ dense_shape=[1, 2]),
+ features['aaa'].eval())
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=[[0, 0], [0, 1]],
+ values=np.array([1., 10.], dtype=np.float32),
+ dense_shape=[1, 2]),
+ features['weights'].eval())
+
+ def test_transform_features(self):
+ column = fc.weighted_categorical_column(
+ categorical_column=fc.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values')
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 1, 0),
+ dense_shape=(2, 2))
+ weights = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0.5, 1.0, 0.1),
+ dense_shape=(2, 2))
+ id_tensor, weight_tensor = _transform_features({
+ 'ids': inputs,
+ 'values': weights,
+ }, (column,), None)[column]
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array(inputs.values, dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_tensor.eval())
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=weights.indices,
+ values=np.array(weights.values, dtype=np.float32),
+ dense_shape=weights.dense_shape),
+ weight_tensor.eval())
+
+ def test_transform_features_dense_input(self):
+ column = fc.weighted_categorical_column(
+ categorical_column=fc.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values')
+ weights = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0.5, 1.0, 0.1),
+ dense_shape=(2, 2))
+ id_tensor, weight_tensor = _transform_features({
+ 'ids': ((0, -1), (1, 0)),
+ 'values': weights,
+ }, (column,), None)[column]
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=np.array((0, 1, 0), dtype=np.int64),
+ dense_shape=(2, 2)),
+ id_tensor.eval())
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=weights.indices,
+ values=np.array(weights.values, dtype=np.float32),
+ dense_shape=weights.dense_shape),
+ weight_tensor.eval())
+
+ def test_transform_features_dense_weights(self):
+ column = fc.weighted_categorical_column(
+ categorical_column=fc.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values')
+ inputs = sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(2, 1, 0),
+ dense_shape=(2, 2))
+ id_tensor, weight_tensor = _transform_features({
+ 'ids': inputs,
+ 'values': ((.5, 0.), (1., .1)),
+ }, (column,), None)[column]
+ with _initialized_session():
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=inputs.indices,
+ values=np.array(inputs.values, dtype=np.int64),
+ dense_shape=inputs.dense_shape),
+ id_tensor.eval())
+ _assert_sparse_tensor_value(
+ self,
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=np.array((.5, 1., .1), dtype=np.float32),
+ dense_shape=(2, 2)),
+ weight_tensor.eval())
+
+ def test_keras_linear_model(self):
+ column = fc_old.weighted_categorical_column(
+ categorical_column=fc_old.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values')
+ with ops.Graph().as_default():
+ predictions = get_keras_linear_model_predictions({
+ 'ids':
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 2, 1),
+ dense_shape=(2, 2)),
+ 'values':
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(.5, 1., .1),
+ dense_shape=(2, 2))
+ }, (column,))
+ bias = get_linear_model_bias()
+ weight_var = get_linear_model_column_var(column)
+ with _initialized_session():
+ self.assertAllClose((0.,), bias.eval())
+ self.assertAllClose(((0.,), (0.,), (0.,)), weight_var.eval())
+ self.assertAllClose(((0.,), (0.,)), predictions.eval())
+ weight_var.assign(((1.,), (2.,), (3.,))).eval()
+ # weight_var[0] * weights[0, 0] = 1 * .5 = .5
+ # weight_var[2] * weights[1, 0] + weight_var[1] * weights[1, 1]
+ # = 3*1 + 2*.1 = 3+.2 = 3.2
+ self.assertAllClose(((.5,), (3.2,)), predictions.eval())
+
+ def test_keras_linear_model_mismatched_shape(self):
+ column = fc_old.weighted_categorical_column(
+ categorical_column=fc_old.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values')
+ with ops.Graph().as_default():
+ with self.assertRaisesRegexp(ValueError,
+ r'Dimensions.*are not compatible'):
+ get_keras_linear_model_predictions({
+ 'ids':
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 2, 1),
+ dense_shape=(2, 2)),
+ 'values':
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (0, 1), (1, 0), (1, 1)),
+ values=(.5, 11., 1., .1),
+ dense_shape=(2, 2))
+ }, (column,))
+
+ def test_keras_linear_model_mismatched_dense_values(self):
+ column = fc_old.weighted_categorical_column(
+ categorical_column=fc_old.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values')
+ with ops.Graph().as_default():
+ predictions = get_keras_linear_model_predictions(
+ {
+ 'ids':
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 2, 1),
+ dense_shape=(2, 2)),
+ 'values': ((.5,), (1.,))
+ }, (column,),
+ sparse_combiner='mean')
+ # Disabling the constant folding optimizer here since it changes the
+ # error message differently on CPU and GPU.
+ config = config_pb2.ConfigProto()
+ config.graph_options.rewrite_options.constant_folding = (
+ rewriter_config_pb2.RewriterConfig.OFF)
+ with _initialized_session(config):
+ with self.assertRaisesRegexp(errors.OpError, 'Incompatible shapes'):
+ predictions.eval()
+
+ def test_keras_linear_model_mismatched_dense_shape(self):
+ column = fc_old.weighted_categorical_column(
+ categorical_column=fc_old.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values')
+ with ops.Graph().as_default():
+ predictions = get_keras_linear_model_predictions({
+ 'ids':
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 2, 1),
+ dense_shape=(2, 2)),
+ 'values': ((.5,), (1.,), (.1,))
+ }, (column,))
+ bias = get_linear_model_bias()
+ weight_var = get_linear_model_column_var(column)
+ with _initialized_session():
+ self.assertAllClose((0.,), bias.eval())
+ self.assertAllClose(((0.,), (0.,), (0.,)), weight_var.eval())
+ self.assertAllClose(((0.,), (0.,)), predictions.eval())
+ weight_var.assign(((1.,), (2.,), (3.,))).eval()
+ # weight_var[0] * weights[0, 0] = 1 * .5 = .5
+ # weight_var[2] * weights[1, 0] + weight_var[1] * weights[1, 1]
+ # = 3*1 + 2*.1 = 3+.2 = 3.2
+ self.assertAllClose(((.5,), (3.2,)), predictions.eval())
+
+ def test_linear_model(self):
+ column = fc_old.weighted_categorical_column(
+ categorical_column=fc_old.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values')
+ with ops.Graph().as_default():
+ predictions = fc.linear_model({
+ 'ids': sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 2, 1),
+ dense_shape=(2, 2)),
+ 'values': sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(.5, 1., .1),
+ dense_shape=(2, 2))
+ }, (column,))
+ bias = get_linear_model_bias()
+ weight_var = get_linear_model_column_var(column)
+ with _initialized_session():
+ self.assertAllClose((0.,), bias.eval())
+ self.assertAllClose(((0.,), (0.,), (0.,)), weight_var.eval())
+ self.assertAllClose(((0.,), (0.,)), predictions.eval())
+ weight_var.assign(((1.,), (2.,), (3.,))).eval()
+ # weight_var[0] * weights[0, 0] = 1 * .5 = .5
+ # weight_var[2] * weights[1, 0] + weight_var[1] * weights[1, 1]
+ # = 3*1 + 2*.1 = 3+.2 = 3.2
+ self.assertAllClose(((.5,), (3.2,)), predictions.eval())
+
+ def test_linear_model_mismatched_shape(self):
+ column = fc_old.weighted_categorical_column(
+ categorical_column=fc_old.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values')
+ with ops.Graph().as_default():
+ with self.assertRaisesRegexp(
+ ValueError, r'Dimensions.*are not compatible'):
+ fc.linear_model({
+ 'ids': sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 2, 1),
+ dense_shape=(2, 2)),
+ 'values': sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (0, 1), (1, 0), (1, 1)),
+ values=(.5, 11., 1., .1),
+ dense_shape=(2, 2))
+ }, (column,))
+
+ def test_linear_model_mismatched_dense_values(self):
+ column = fc_old.weighted_categorical_column(
+ categorical_column=fc_old.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values')
+ with ops.Graph().as_default():
+ predictions = fc.linear_model(
+ {
+ 'ids':
+ sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 2, 1),
+ dense_shape=(2, 2)),
+ 'values': ((.5,), (1.,))
+ }, (column,),
+ sparse_combiner='mean')
+ # Disabling the constant folding optimizer here since it changes the
+ # error message differently on CPU and GPU.
+ config = config_pb2.ConfigProto()
+ config.graph_options.rewrite_options.constant_folding = (
+ rewriter_config_pb2.RewriterConfig.OFF)
+ with _initialized_session(config):
+ with self.assertRaisesRegexp(errors.OpError, 'Incompatible shapes'):
+ predictions.eval()
+
+ def test_linear_model_mismatched_dense_shape(self):
+ column = fc_old.weighted_categorical_column(
+ categorical_column=fc_old.categorical_column_with_identity(
+ key='ids', num_buckets=3),
+ weight_feature_key='values')
+ with ops.Graph().as_default():
+ predictions = fc.linear_model({
+ 'ids': sparse_tensor.SparseTensorValue(
+ indices=((0, 0), (1, 0), (1, 1)),
+ values=(0, 2, 1),
+ dense_shape=(2, 2)),
+ 'values': ((.5,), (1.,), (.1,))
+ }, (column,))
+ bias = get_linear_model_bias()
+ weight_var = get_linear_model_column_var(column)
+ with _initialized_session():
+ self.assertAllClose((0.,), bias.eval())
+ self.assertAllClose(((0.,), (0.,), (0.,)), weight_var.eval())
+ self.assertAllClose(((0.,), (0.,)), predictions.eval())
+ weight_var.assign(((1.,), (2.,), (3.,))).eval()
+ # weight_var[0] * weights[0, 0] = 1 * .5 = .5
+ # weight_var[2] * weights[1, 0] + weight_var[1] * weights[1, 1]
+ # = 3*1 + 2*.1 = 3+.2 = 3.2
+ self.assertAllClose(((.5,), (3.2,)), predictions.eval())
+
+ # TODO(ptucker): Add test with embedding of weighted categorical.
+
+if __name__ == '__main__':
+ test.main()