mirror of
https://github.com/zebrajr/tensorflow.git
synced 2025-12-08 07:38:39 +01:00
Add DNNLinearCombinedClassifier.
PiperOrigin-RevId: 158075939
This commit is contained in:
parent
3d52e4cb93
commit
68fdb7628f
|
|
@ -238,6 +238,144 @@ def _dnn_linear_combined_model_fn(
|
|||
logits=logits)
|
||||
|
||||
|
||||
class DNNLinearCombinedClassifier(estimator.Estimator):
|
||||
"""An estimator for TensorFlow Linear and DNN joined classification models.
|
||||
|
||||
Note: This estimator is also known as wide-n-deep.
|
||||
|
||||
Example:
|
||||
|
||||
```python
|
||||
numeric_feature = numeric_column(...)
|
||||
sparse_column_a = categorical_column_with_hash_bucket(...)
|
||||
sparse_column_b = categorical_column_with_hash_bucket(...)
|
||||
|
||||
sparse_feature_a_x_sparse_feature_b = crossed_column(...)
|
||||
sparse_feature_a_emb = embedding_column(sparse_id_column=sparse_feature_a,
|
||||
...)
|
||||
sparse_feature_b_emb = embedding_column(sparse_id_column=sparse_feature_b,
|
||||
...)
|
||||
|
||||
estimator = DNNLinearCombinedClassifier(
|
||||
# wide settings
|
||||
linear_feature_columns=[sparse_feature_a_x_sparse_feature_b],
|
||||
linear_optimizer=tf.train.FtrlOptimizer(...),
|
||||
# deep settings
|
||||
dnn_feature_columns=[
|
||||
sparse_feature_a_emb, sparse_feature_b_emb, numeric_feature],
|
||||
dnn_hidden_units=[1000, 500, 100],
|
||||
dnn_optimizer=tf.train.ProximalAdagradOptimizer(...))
|
||||
|
||||
# To apply L1 and L2 regularization, you can set optimizers as follows:
|
||||
tf.train.ProximalAdagradOptimizer(
|
||||
learning_rate=0.1,
|
||||
l1_regularization_strength=0.001,
|
||||
l2_regularization_strength=0.001)
|
||||
# It is same for FtrlOptimizer.
|
||||
|
||||
# Input builders
|
||||
def input_fn_train: # returns x, y
|
||||
pass
|
||||
estimator.train(input_fn=input_fn_train, steps=100)
|
||||
|
||||
def input_fn_eval: # returns x, y
|
||||
pass
|
||||
metrics = estimator.evaluate(input_fn=input_fn_eval, steps=10)
|
||||
def input_fn_predict: # returns x, None
|
||||
pass
|
||||
predictions = estimator.predict(input_fn=input_fn_predict)
|
||||
```
|
||||
|
||||
Input of `train` and `evaluate` should have following features,
|
||||
otherwise there will be a `KeyError`:
|
||||
|
||||
* for each `column` in `dnn_feature_columns` + `linear_feature_columns`:
|
||||
- if `column` is a `_CategoricalColumn`, a feature with `key=column.name`
|
||||
whose `value` is a `SparseTensor`.
|
||||
- if `column` is a `_WeightedCategoricalColumn`, two features: the first
|
||||
with `key` the id column name, the second with `key` the weight column
|
||||
name. Both features' `value` must be a `SparseTensor`.
|
||||
- if `column` is a `_DenseColumn`, a feature with `key=column.name`
|
||||
whose `value` is a `Tensor`.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self,
|
||||
model_dir=None,
|
||||
linear_feature_columns=None,
|
||||
linear_optimizer=None,
|
||||
dnn_feature_columns=None,
|
||||
dnn_optimizer=None,
|
||||
dnn_hidden_units=None,
|
||||
dnn_activation_fn=nn.relu,
|
||||
dnn_dropout=None,
|
||||
n_classes=2,
|
||||
input_layer_partitioner=None,
|
||||
config=None):
|
||||
"""Initializes a DNNLinearCombinedClassifier instance.
|
||||
|
||||
Args:
|
||||
model_dir: Directory to save model parameters, graph and etc. This can
|
||||
also be used to load checkpoints from the directory into a estimator
|
||||
to continue training a previously saved model.
|
||||
linear_feature_columns: An iterable containing all the feature columns
|
||||
used by linear part of the model. All items in the set must be
|
||||
instances of classes derived from `FeatureColumn`.
|
||||
linear_optimizer: An instance of `tf.Optimizer` used to apply gradients to
|
||||
the linear part of the model. If `None`, will use a FTRL optimizer.
|
||||
dnn_feature_columns: An iterable containing all the feature columns used
|
||||
by deep part of the model. All items in the set must be instances of
|
||||
classes derived from `FeatureColumn`.
|
||||
dnn_optimizer: An instance of `tf.Optimizer` used to apply gradients to
|
||||
the deep part of the model. If `None`, will use an Adagrad optimizer.
|
||||
dnn_hidden_units: List of hidden units per layer. All layers are fully
|
||||
connected.
|
||||
dnn_activation_fn: Activation function applied to each layer. If None,
|
||||
will use `tf.nn.relu`.
|
||||
dnn_dropout: When not None, the probability we will drop out
|
||||
a given coordinate.
|
||||
n_classes: Number of label classes. Defaults to 2, namely binary
|
||||
classification. Must be > 1.
|
||||
input_layer_partitioner: Partitioner for input layer. Defaults to
|
||||
`min_max_variable_partitioner` with `min_slice_size` 64 << 20.
|
||||
config: RunConfig object to configure the runtime settings.
|
||||
|
||||
Raises:
|
||||
ValueError: If both linear_feature_columns and dnn_features_columns are
|
||||
empty at the same time.
|
||||
"""
|
||||
linear_feature_columns = linear_feature_columns or []
|
||||
dnn_feature_columns = dnn_feature_columns or []
|
||||
self._feature_columns = linear_feature_columns + dnn_feature_columns
|
||||
if not self._feature_columns:
|
||||
raise ValueError('Either linear_feature_columns or dnn_feature_columns '
|
||||
'must be defined.')
|
||||
if n_classes == 2:
|
||||
head = head_lib._binary_logistic_head_with_sigmoid_cross_entropy_loss() # pylint: disable=protected-access
|
||||
else:
|
||||
head = head_lib._multi_class_head_with_softmax_cross_entropy_loss( # pylint: disable=protected-access
|
||||
n_classes)
|
||||
|
||||
def _model_fn(features, labels, mode, config):
|
||||
return _dnn_linear_combined_model_fn(
|
||||
features=features,
|
||||
labels=labels,
|
||||
mode=mode,
|
||||
head=head,
|
||||
linear_feature_columns=linear_feature_columns,
|
||||
linear_optimizer=linear_optimizer,
|
||||
dnn_feature_columns=dnn_feature_columns,
|
||||
dnn_optimizer=dnn_optimizer,
|
||||
dnn_hidden_units=dnn_hidden_units,
|
||||
dnn_activation_fn=dnn_activation_fn,
|
||||
dnn_dropout=dnn_dropout,
|
||||
input_layer_partitioner=input_layer_partitioner,
|
||||
config=config)
|
||||
|
||||
super(DNNLinearCombinedClassifier, self).__init__(
|
||||
model_fn=_model_fn, model_dir=model_dir, config=config)
|
||||
|
||||
|
||||
class DNNLinearCombinedRegressor(estimator.Estimator):
|
||||
"""An estimator for TensorFlow Linear and DNN joined models for regresssion.
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user