Estimators

Train and evaluate TensorFlow models.

class tf.contrib.learn.BaseEstimator

Abstract BaseEstimator class to train and evaluate TensorFlow models.

Concrete implementation of this class should provide the following functions:

  • _get_train_ops
  • _get_eval_ops
  • _get_predict_ops

Estimator implemented below is a good example of how to use this class.


tf.contrib.learn.BaseEstimator.__init__(model_dir=None, config=None) {:#BaseEstimator.init}

Initializes a BaseEstimator 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.
  • config: A RunConfig instance.

tf.contrib.learn.BaseEstimator.__repr__() {:#BaseEstimator.repr}


tf.contrib.learn.BaseEstimator.config


tf.contrib.learn.BaseEstimator.evaluate(x=None, y=None, input_fn=None, feed_fn=None, batch_size=None, steps=None, metrics=None, name=None)

See Evaluable.

Raises:
  • ValueError: If at least one of x or y is provided, and at least one of input_fn or feed_fn is provided. Or if metrics is not None or dict.

tf.contrib.learn.BaseEstimator.export(*args, **kwargs)

Exports inference graph into given dir. (deprecated arguments)

SOME ARGUMENTS ARE DEPRECATED. They will be removed after 2016-09-23. Instructions for updating: The signature of the input_fn accepted by export is changing to be consistent with what's used by tf.Learn Estimator's train/evaluate. input_fn and input_feature_key will become required args, and use_deprecated_input_fn will default to False and be removed altogether.

Args:
  export_dir: A string containing a directory to write the exported graph
    and checkpoints.
  input_fn: If `use_deprecated_input_fn` is true, then a function that given
    `Tensor` of `Example` strings, parses it into features that are then
    passed to the model. Otherwise, a function that takes no argument and
    returns a tuple of (features, targets), where features is a dict of
    string key to `Tensor` and targets is a `Tensor` that's currently not
    used (and so can be `None`).
  input_feature_key: Only used if `use_deprecated_input_fn` is false. String
    key into the features dict returned by `input_fn` that corresponds toa
    the raw `Example` strings `Tensor` that the exported model will take as
    input.
  use_deprecated_input_fn: Determines the signature format of `input_fn`.
  signature_fn: Function that returns a default signature and a named
    signature map, given `Tensor` of `Example` strings, `dict` of `Tensor`s
    for features and `Tensor` or `dict` of `Tensor`s for predictions.
  prediction_key: The key for a tensor in the `predictions` dict (output
    from the `model_fn`) to use as the `predictions` input to the
    `signature_fn`. Optional. If `None`, predictions will pass to
    `signature_fn` without filtering.
  default_batch_size: Default batch size of the `Example` placeholder.
  exports_to_keep: Number of exports to keep.

Returns:
  The string path to the exported directory. NB: this functionality was
  added ca. 2016/09/25; clients that depend on the return value may need
  to handle the case where this function returns None because subclasses
  are not returning a value.

tf.contrib.learn.BaseEstimator.fit(x=None, y=None, input_fn=None, steps=None, batch_size=None, monitors=None, max_steps=None)

See Trainable.

Raises:
  • ValueError: If x or y are not None while input_fn is not None.
  • ValueError: If both steps and max_steps are not None.

tf.contrib.learn.BaseEstimator.get_params(deep=True)

Get parameters for this estimator.

Args:
  • deep: boolean, optional

    If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params : mapping of string to any Parameter names mapped to their values.


tf.contrib.learn.BaseEstimator.get_variable_names()

Returns list of all variable names in this model.

Returns:

List of names.


tf.contrib.learn.BaseEstimator.get_variable_value(name)

Returns value of the variable given by name.

Args:
  • name: string, name of the tensor.
Returns:

Numpy array - value of the tensor.


tf.contrib.learn.BaseEstimator.model_dir


tf.contrib.learn.BaseEstimator.partial_fit(x=None, y=None, input_fn=None, steps=1, batch_size=None, monitors=None)

Incremental fit on a batch of samples.

This method is expected to be called several times consecutively on different or the same chunks of the dataset. This either can implement iterative training or out-of-core/online training.

This is especially useful when the whole dataset is too big to fit in memory at the same time. Or when model is taking long time to converge, and you want to split up training into subparts.

Args:
  • x: Matrix of shape [n_samples, n_features...]. Can be iterator that returns arrays of features. The training input samples for fitting the model. If set, input_fn must be None.
  • y: Vector or matrix [n_samples] or [n_samples, n_outputs]. Can be iterator that returns array of targets. The training target values (class labels in classification, real numbers in regression). If set, input_fn must be None.
  • input_fn: Input function. If set, x, y, and batch_size must be None.
  • steps: Number of steps for which to train model. If None, train forever.
  • batch_size: minibatch size to use on the input, defaults to first dimension of x. Must be None if input_fn is provided.
  • monitors: List of BaseMonitor subclass instances. Used for callbacks inside the training loop.
Returns:

self, for chaining.

Raises:
  • ValueError: If at least one of x and y is provided, and input_fn is provided.

tf.contrib.learn.BaseEstimator.predict(*args, **kwargs)

Returns predictions for given features. (deprecated arguments)

SOME ARGUMENTS ARE DEPRECATED. They will be removed after 2016-09-15. Instructions for updating: The default behavior of predict() is changing. The default value for as_iterable will change to True, and then the flag will be removed altogether. The behavior of this flag is described below.

Args:
  x: Matrix of shape [n_samples, n_features...]. Can be iterator that
     returns arrays of features. The training input samples for fitting the
     model. If set, `input_fn` must be `None`.
  input_fn: Input function. If set, `x` and 'batch_size' must be `None`.
  batch_size: Override default batch size. If set, 'input_fn' must be
    'None'.
  outputs: list of `str`, name of the output to predict.
    If `None`, returns all.
  as_iterable: If True, return an iterable which keeps yielding predictions
    for each example until inputs are exhausted. Note: The inputs must
    terminate if you want the iterable to terminate (e.g. be sure to pass
    num_epochs=1 if you are using something like read_batch_features).

Returns:
  A numpy array of predicted classes or regression values if the
  constructor's `model_fn` returns a `Tensor` for `predictions` or a `dict`
  of numpy arrays if `model_fn` returns a `dict`. Returns an iterable of
  predictions if as_iterable is True.

Raises:
  ValueError: If x and input_fn are both provided or both `None`.

tf.contrib.learn.BaseEstimator.set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The former have parameters of the form <component>__<parameter> so that it's possible to update each component of a nested object.

Args:
  • **params: Parameters.
Returns:

self

Raises:
  • ValueError: If params contain invalid names.

class tf.contrib.learn.Estimator

Estimator class is the basic TensorFlow model trainer/evaluator.


tf.contrib.learn.Estimator.__init__(model_fn=None, model_dir=None, config=None, params=None, feature_engineering_fn=None) {:#Estimator.init}

Constructs an Estimator instance.

Args:
  • model_fn: Model function, takes features and targets tensors or dicts of tensors and returns predictions and loss tensors. Supports next three signatures for the function:

    • (features, targets) -> (predictions, loss, train_op)
    • (features, targets, mode) -> (predictions, loss, train_op)
    • (features, targets, mode, params) -> (predictions, loss, train_op)

    Where

    • features are single Tensor or dict of Tensors (depending on data passed to fit),
    • targets are Tensor or dict of Tensors (for multi-head models). If mode is ModeKeys.INFER, targets=None will be passed. If the model_fn's signature does not accept mode, the model_fn must still be able to handle targets=None.
    • mode represents if this training, evaluation or prediction. See ModeKeys.
    • params is a dict of hyperparameters. Will receive what is passed to Estimator in params parameter. This allows to configure Estimators from hyper parameter tunning.
  • 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.

  • config: Configuration object.
  • params: dict of hyper parameters that will be passed into model_fn. Keys are names of parameters, values are basic python types.
  • feature_engineering_fn: Feature engineering function. Takes features and targets which are the output of input_fn and returns features and targets which will be fed into model_fn. Please check model_fn for a definition of features and targets.
Raises:
  • ValueError: parameters of model_fn don't match params.

tf.contrib.learn.Estimator.__repr__() {:#Estimator.repr}


tf.contrib.learn.Estimator.config


tf.contrib.learn.Estimator.evaluate(x=None, y=None, input_fn=None, feed_fn=None, batch_size=None, steps=None, metrics=None, name=None)

See Evaluable.

Raises:
  • ValueError: If at least one of x or y is provided, and at least one of input_fn or feed_fn is provided. Or if metrics is not None or dict.

tf.contrib.learn.Estimator.export(*args, **kwargs)

Exports inference graph into given dir. (deprecated arguments)

SOME ARGUMENTS ARE DEPRECATED. They will be removed after 2016-09-23. Instructions for updating: The signature of the input_fn accepted by export is changing to be consistent with what's used by tf.Learn Estimator's train/evaluate. input_fn and input_feature_key will become required args, and use_deprecated_input_fn will default to False and be removed altogether.

Args:
  export_dir: A string containing a directory to write the exported graph
    and checkpoints.
  input_fn: If `use_deprecated_input_fn` is true, then a function that given
    `Tensor` of `Example` strings, parses it into features that are then
    passed to the model. Otherwise, a function that takes no argument and
    returns a tuple of (features, targets), where features is a dict of
    string key to `Tensor` and targets is a `Tensor` that's currently not
    used (and so can be `None`).
  input_feature_key: Only used if `use_deprecated_input_fn` is false. String
    key into the features dict returned by `input_fn` that corresponds toa
    the raw `Example` strings `Tensor` that the exported model will take as
    input.
  use_deprecated_input_fn: Determines the signature format of `input_fn`.
  signature_fn: Function that returns a default signature and a named
    signature map, given `Tensor` of `Example` strings, `dict` of `Tensor`s
    for features and `Tensor` or `dict` of `Tensor`s for predictions.
  prediction_key: The key for a tensor in the `predictions` dict (output
    from the `model_fn`) to use as the `predictions` input to the
    `signature_fn`. Optional. If `None`, predictions will pass to
    `signature_fn` without filtering.
  default_batch_size: Default batch size of the `Example` placeholder.
  exports_to_keep: Number of exports to keep.

Returns:
  The string path to the exported directory. NB: this functionality was
  added ca. 2016/09/25; clients that depend on the return value may need
  to handle the case where this function returns None because subclasses
  are not returning a value.

tf.contrib.learn.Estimator.fit(x=None, y=None, input_fn=None, steps=None, batch_size=None, monitors=None, max_steps=None)

See Trainable.

Raises:
  • ValueError: If x or y are not None while input_fn is not None.
  • ValueError: If both steps and max_steps are not None.

tf.contrib.learn.Estimator.get_params(deep=True)

Get parameters for this estimator.

Args:
  • deep: boolean, optional

    If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params : mapping of string to any Parameter names mapped to their values.


tf.contrib.learn.Estimator.get_variable_names()

Returns list of all variable names in this model.

Returns:

List of names.


tf.contrib.learn.Estimator.get_variable_value(name)

Returns value of the variable given by name.

Args:
  • name: string, name of the tensor.
Returns:

Numpy array - value of the tensor.


tf.contrib.learn.Estimator.model_dir


tf.contrib.learn.Estimator.partial_fit(x=None, y=None, input_fn=None, steps=1, batch_size=None, monitors=None)

Incremental fit on a batch of samples.

This method is expected to be called several times consecutively on different or the same chunks of the dataset. This either can implement iterative training or out-of-core/online training.

This is especially useful when the whole dataset is too big to fit in memory at the same time. Or when model is taking long time to converge, and you want to split up training into subparts.

Args:
  • x: Matrix of shape [n_samples, n_features...]. Can be iterator that returns arrays of features. The training input samples for fitting the model. If set, input_fn must be None.
  • y: Vector or matrix [n_samples] or [n_samples, n_outputs]. Can be iterator that returns array of targets. The training target values (class labels in classification, real numbers in regression). If set, input_fn must be None.
  • input_fn: Input function. If set, x, y, and batch_size must be None.
  • steps: Number of steps for which to train model. If None, train forever.
  • batch_size: minibatch size to use on the input, defaults to first dimension of x. Must be None if input_fn is provided.
  • monitors: List of BaseMonitor subclass instances. Used for callbacks inside the training loop.
Returns:

self, for chaining.

Raises:
  • ValueError: If at least one of x and y is provided, and input_fn is provided.

tf.contrib.learn.Estimator.predict(*args, **kwargs)

Returns predictions for given features. (deprecated arguments)

SOME ARGUMENTS ARE DEPRECATED. They will be removed after 2016-09-15. Instructions for updating: The default behavior of predict() is changing. The default value for as_iterable will change to True, and then the flag will be removed altogether. The behavior of this flag is described below.

Args:
  x: Matrix of shape [n_samples, n_features...]. Can be iterator that
     returns arrays of features. The training input samples for fitting the
     model. If set, `input_fn` must be `None`.
  input_fn: Input function. If set, `x` and 'batch_size' must be `None`.
  batch_size: Override default batch size. If set, 'input_fn' must be
    'None'.
  outputs: list of `str`, name of the output to predict.
    If `None`, returns all.
  as_iterable: If True, return an iterable which keeps yielding predictions
    for each example until inputs are exhausted. Note: The inputs must
    terminate if you want the iterable to terminate (e.g. be sure to pass
    num_epochs=1 if you are using something like read_batch_features).

Returns:
  A numpy array of predicted classes or regression values if the
  constructor's `model_fn` returns a `Tensor` for `predictions` or a `dict`
  of numpy arrays if `model_fn` returns a `dict`. Returns an iterable of
  predictions if as_iterable is True.

Raises:
  ValueError: If x and input_fn are both provided or both `None`.

tf.contrib.learn.Estimator.set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The former have parameters of the form <component>__<parameter> so that it's possible to update each component of a nested object.

Args:
  • **params: Parameters.
Returns:

self

Raises:
  • ValueError: If params contain invalid names.

class tf.contrib.learn.ModeKeys

Standard names for model modes.

The following standard keys are defined:

  • TRAIN: training mode.
  • EVAL: evaluation mode.
  • INFER: inference mode.

class tf.contrib.learn.DNNClassifier

A classifier for TensorFlow DNN models.

Example:

education = sparse_column_with_hash_bucket(column_name="education",
                                           hash_bucket_size=1000)
occupation = sparse_column_with_hash_bucket(column_name="occupation",
                                            hash_bucket_size=1000)

education_emb = embedding_column(sparse_id_column=education, dimension=16,
                                 combiner="sum")
occupation_emb = embedding_column(sparse_id_column=occupation, dimension=16,
                                 combiner="sum")

estimator = DNNClassifier(
    feature_columns=[education_emb, occupation_emb],
    hidden_units=[1024, 512, 256])

# Or estimator using the ProximalAdagradOptimizer optimizer with
# regularization.
estimator = DNNClassifier(
    feature_columns=[education_emb, occupation_emb],
    hidden_units=[1024, 512, 256],
    optimizer=tf.train.ProximalAdagradOptimizer(
      learning_rate=0.1,
      l1_regularization_strength=0.001
    ))

# Input builders
def input_fn_train: # returns x, Y
  pass
estimator.fit(input_fn=input_fn_train)

def input_fn_eval: # returns x, Y
  pass
estimator.evaluate(input_fn=input_fn_eval)
estimator.predict(x=x)

Input of fit and evaluate should have following features, otherwise there will be a KeyError:

  • if weight_column_name is not None, a feature with key=weight_column_name whose value is a Tensor.
  • for each column in feature_columns:
  • if column is a SparseColumn, a feature with key=column.name whose value is a SparseTensor.
  • if column is a WeightedSparseColumn, 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 RealValuedColumn, a feature with key=column.name whose value is a Tensor.

tf.contrib.learn.DNNClassifier.__init__(hidden_units, feature_columns, model_dir=None, n_classes=2, weight_column_name=None, optimizer=None, activation_fn=relu, dropout=None, gradient_clip_norm=None, enable_centered_bias=None, config=None) {:#DNNClassifier.init}

Initializes a DNNClassifier instance.

Args:
  • hidden_units: List of hidden units per layer. All layers are fully connected. Ex. [64, 32] means first layer has 64 nodes and second one has 32.
  • feature_columns: An iterable containing all the feature columns used by the model. All items in the set should be instances of classes derived from FeatureColumn.
  • 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.
  • n_classes: number of target classes. Default is binary classification. It must be greater than 1.
  • weight_column_name: A string defining feature column name representing weights. It is used to down weight or boost examples during training. It will be multiplied by the loss of the example.
  • optimizer: An instance of tf.Optimizer used to train the model. If None, will use an Adagrad optimizer.
  • activation_fn: Activation function applied to each layer. If None, will use tf.nn.relu.
  • dropout: When not None, the probability we will drop out a given coordinate.
  • gradient_clip_norm: A float > 0. If provided, gradients are clipped to their global norm with this clipping ratio. See tf.clip_by_global_norm for more details.
  • enable_centered_bias: A bool. If True, estimator will learn a centered bias variable for each class. Rest of the model structure learns the residual after centered bias.
  • config: RunConfig object to configure the runtime settings.
Returns:

A DNNClassifier estimator.

Raises:
  • ValueError: If n_classes < 2.

tf.contrib.learn.DNNClassifier.bias_

DEPRECATED FUNCTION

THIS FUNCTION IS DEPRECATED. It will be removed after 2016-10-13. Instructions for updating: This method inspects the private state of the object, and should not be used


tf.contrib.learn.DNNClassifier.config


tf.contrib.learn.DNNClassifier.evaluate(x=None, y=None, input_fn=None, feed_fn=None, batch_size=None, steps=None, metrics=None, name=None)

See evaluable.Evaluable.


tf.contrib.learn.DNNClassifier.export(export_dir, input_fn=None, input_feature_key=None, use_deprecated_input_fn=True, signature_fn=None, default_batch_size=1, exports_to_keep=None)

See BaseEstimator.export.


tf.contrib.learn.DNNClassifier.fit(x=None, y=None, input_fn=None, steps=None, batch_size=None, monitors=None, max_steps=None)

See trainable.Trainable.


tf.contrib.learn.DNNClassifier.get_variable_names()

Returns list of all variable names in this model.

Returns:

List of names.


tf.contrib.learn.DNNClassifier.get_variable_value(name)

Returns value of the variable given by name.

Args:
  • name: string, name of the tensor.
Returns:

Tensor object.


tf.contrib.learn.DNNClassifier.model_dir


tf.contrib.learn.DNNClassifier.predict(*args, **kwargs)

Returns predicted classes for given features. (deprecated arguments)

SOME ARGUMENTS ARE DEPRECATED. They will be removed after 2016-09-15. Instructions for updating: The default behavior of predict() is changing. The default value for as_iterable will change to True, and then the flag will be removed altogether. The behavior of this flag is described below.

Args:
  x: features.
  input_fn: Input function. If set, x must be None.
  batch_size: Override default batch size.
  as_iterable: If True, return an iterable which keeps yielding predictions
    for each example until inputs are exhausted. Note: The inputs must
    terminate if you want the iterable to terminate (e.g. be sure to pass
    num_epochs=1 if you are using something like read_batch_features).

Returns:
  Numpy array of predicted classes (or an iterable of predicted classes if
  as_iterable is True).

tf.contrib.learn.DNNClassifier.predict_proba(*args, **kwargs)

Returns prediction probabilities for given features. (deprecated arguments)

SOME ARGUMENTS ARE DEPRECATED. They will be removed after 2016-09-15. Instructions for updating: The default behavior of predict() is changing. The default value for as_iterable will change to True, and then the flag will be removed altogether. The behavior of this flag is described below.

Args:
  x: features.
  input_fn: Input function. If set, x and y must be None.
  batch_size: Override default batch size.
  as_iterable: If True, return an iterable which keeps yielding predictions
    for each example until inputs are exhausted. Note: The inputs must
    terminate if you want the iterable to terminate (e.g. be sure to pass
    num_epochs=1 if you are using something like read_batch_features).

Returns:
  Numpy array of predicted probabilities (or an iterable of predicted
  probabilities if as_iterable is True).

tf.contrib.learn.DNNClassifier.weights_

DEPRECATED FUNCTION

THIS FUNCTION IS DEPRECATED. It will be removed after 2016-10-13. Instructions for updating: This method inspects the private state of the object, and should not be used


class tf.contrib.learn.DNNRegressor

A regressor for TensorFlow DNN models.

Example:

education = sparse_column_with_hash_bucket(column_name="education",
                                           hash_bucket_size=1000)
occupation = sparse_column_with_hash_bucket(column_name="occupation",
                                            hash_bucket_size=1000)

education_emb = embedding_column(sparse_id_column=education, dimension=16,
                                 combiner="sum")
occupation_emb = embedding_column(sparse_id_column=occupation, dimension=16,
                                 combiner="sum")

estimator = DNNRegressor(
    feature_columns=[education_emb, occupation_emb],
    hidden_units=[1024, 512, 256])

# Or estimator using the ProximalAdagradOptimizer optimizer with
# regularization.
estimator = DNNRegressor(
    feature_columns=[education_emb, occupation_emb],
    hidden_units=[1024, 512, 256],
    optimizer=tf.train.ProximalAdagradOptimizer(
      learning_rate=0.1,
      l1_regularization_strength=0.001
    ))

# Input builders
def input_fn_train: # returns x, Y
  pass
estimator.fit(input_fn=input_fn_train)

def input_fn_eval: # returns x, Y
  pass
estimator.evaluate(input_fn=input_fn_eval)
estimator.predict(x=x)

Input of fit and evaluate should have following features, otherwise there will be a KeyError:

  • if weight_column_name is not None, a feature with key=weight_column_name whose value is a Tensor.
  • for each column in feature_columns:
  • if column is a SparseColumn, a feature with key=column.name whose value is a SparseTensor.
  • if column is a WeightedSparseColumn, 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 RealValuedColumn, a feature with key=column.name whose value is a Tensor.

tf.contrib.learn.DNNRegressor.__init__(hidden_units, feature_columns, model_dir=None, weight_column_name=None, optimizer=None, activation_fn=relu, dropout=None, gradient_clip_norm=None, enable_centered_bias=None, config=None) {:#DNNRegressor.init}

Initializes a DNNRegressor instance.

Args:
  • hidden_units: List of hidden units per layer. All layers are fully connected. Ex. [64, 32] means first layer has 64 nodes and second one has 32.
  • feature_columns: An iterable containing all the feature columns used by the model. All items in the set should be instances of classes derived from FeatureColumn.
  • 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.
  • weight_column_name: A string defining feature column name representing weights. It is used to down weight or boost examples during training. It will be multiplied by the loss of the example.
  • optimizer: An instance of tf.Optimizer used to train the model. If None, will use an Adagrad optimizer.
  • activation_fn: Activation function applied to each layer. If None, will use tf.nn.relu.
  • dropout: When not None, the probability we will drop out a given coordinate.
  • gradient_clip_norm: A float > 0. If provided, gradients are clipped to their global norm with this clipping ratio. See tf.clip_by_global_norm for more details.
  • enable_centered_bias: A bool. If True, estimator will learn a centered bias variable for each class. Rest of the model structure learns the residual after centered bias.
  • config: RunConfig object to configure the runtime settings.
Returns:

A DNNRegressor estimator.


tf.contrib.learn.DNNRegressor.__repr__() {:#DNNRegressor.repr}


tf.contrib.learn.DNNRegressor.bias_


tf.contrib.learn.DNNRegressor.config


tf.contrib.learn.DNNRegressor.dnn_bias_

Returns bias of deep neural network part.


tf.contrib.learn.DNNRegressor.dnn_weights_

Returns weights of deep neural network part.


tf.contrib.learn.DNNRegressor.evaluate(x=None, y=None, input_fn=None, feed_fn=None, batch_size=None, steps=None, metrics=None, name=None)

See Evaluable.

Raises:
  • ValueError: If at least one of x or y is provided, and at least one of input_fn or feed_fn is provided. Or if metrics is not None or dict.

tf.contrib.learn.DNNRegressor.export(*args, **kwargs)

Exports inference graph into given dir. (deprecated arguments)

SOME ARGUMENTS ARE DEPRECATED. They will be removed after 2016-09-23. Instructions for updating: The signature of the input_fn accepted by export is changing to be consistent with what's used by tf.Learn Estimator's train/evaluate. input_fn and input_feature_key will become required args, and use_deprecated_input_fn will default to False and be removed altogether.

Args:
  export_dir: A string containing a directory to write the exported graph
    and checkpoints.
  input_fn: If `use_deprecated_input_fn` is true, then a function that given
    `Tensor` of `Example` strings, parses it into features that are then
    passed to the model. Otherwise, a function that takes no argument and
    returns a tuple of (features, targets), where features is a dict of
    string key to `Tensor` and targets is a `Tensor` that's currently not
    used (and so can be `None`).
  input_feature_key: Only used if `use_deprecated_input_fn` is false. String
    key into the features dict returned by `input_fn` that corresponds toa
    the raw `Example` strings `Tensor` that the exported model will take as
    input.
  use_deprecated_input_fn: Determines the signature format of `input_fn`.
  signature_fn: Function that returns a default signature and a named
    signature map, given `Tensor` of `Example` strings, `dict` of `Tensor`s
    for features and `Tensor` or `dict` of `Tensor`s for predictions.
  prediction_key: The key for a tensor in the `predictions` dict (output
    from the `model_fn`) to use as the `predictions` input to the
    `signature_fn`. Optional. If `None`, predictions will pass to
    `signature_fn` without filtering.
  default_batch_size: Default batch size of the `Example` placeholder.
  exports_to_keep: Number of exports to keep.

Returns:
  The string path to the exported directory. NB: this functionality was
  added ca. 2016/09/25; clients that depend on the return value may need
  to handle the case where this function returns None because subclasses
  are not returning a value.

tf.contrib.learn.DNNRegressor.fit(x=None, y=None, input_fn=None, steps=None, batch_size=None, monitors=None, max_steps=None)

See Trainable.

Raises:
  • ValueError: If x or y are not None while input_fn is not None.
  • ValueError: If both steps and max_steps are not None.

tf.contrib.learn.DNNRegressor.get_params(deep=True)

Get parameters for this estimator.

Args:
  • deep: boolean, optional

    If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params : mapping of string to any Parameter names mapped to their values.


tf.contrib.learn.DNNRegressor.get_variable_names()

Returns list of all variable names in this model.

Returns:

List of names.


tf.contrib.learn.DNNRegressor.get_variable_value(name)

Returns value of the variable given by name.

Args:
  • name: string, name of the tensor.
Returns:

Numpy array - value of the tensor.


tf.contrib.learn.DNNRegressor.linear_bias_

Returns bias of the linear part.


tf.contrib.learn.DNNRegressor.linear_weights_

Returns weights per feature of the linear part.


tf.contrib.learn.DNNRegressor.model_dir


tf.contrib.learn.DNNRegressor.partial_fit(x=None, y=None, input_fn=None, steps=1, batch_size=None, monitors=None)

Incremental fit on a batch of samples.

This method is expected to be called several times consecutively on different or the same chunks of the dataset. This either can implement iterative training or out-of-core/online training.

This is especially useful when the whole dataset is too big to fit in memory at the same time. Or when model is taking long time to converge, and you want to split up training into subparts.

Args:
  • x: Matrix of shape [n_samples, n_features...]. Can be iterator that returns arrays of features. The training input samples for fitting the model. If set, input_fn must be None.
  • y: Vector or matrix [n_samples] or [n_samples, n_outputs]. Can be iterator that returns array of targets. The training target values (class labels in classification, real numbers in regression). If set, input_fn must be None.
  • input_fn: Input function. If set, x, y, and batch_size must be None.
  • steps: Number of steps for which to train model. If None, train forever.
  • batch_size: minibatch size to use on the input, defaults to first dimension of x. Must be None if input_fn is provided.
  • monitors: List of BaseMonitor subclass instances. Used for callbacks inside the training loop.
Returns:

self, for chaining.

Raises:
  • ValueError: If at least one of x and y is provided, and input_fn is provided.

tf.contrib.learn.DNNRegressor.predict(*args, **kwargs)

Returns predictions for given features. (deprecated arguments)

SOME ARGUMENTS ARE DEPRECATED. They will be removed after 2016-09-15. Instructions for updating: The default behavior of predict() is changing. The default value for as_iterable will change to True, and then the flag will be removed altogether. The behavior of this flag is described below.

Args:
  x: Matrix of shape [n_samples, n_features...]. Can be iterator that
     returns arrays of features. The training input samples for fitting the
     model. If set, `input_fn` must be `None`.
  input_fn: Input function. If set, `x` and 'batch_size' must be `None`.
  batch_size: Override default batch size. If set, 'input_fn' must be
    'None'.
  outputs: list of `str`, name of the output to predict.
    If `None`, returns all.
  as_iterable: If True, return an iterable which keeps yielding predictions
    for each example until inputs are exhausted. Note: The inputs must
    terminate if you want the iterable to terminate (e.g. be sure to pass
    num_epochs=1 if you are using something like read_batch_features).

Returns:
  A numpy array of predicted classes or regression values if the
  constructor's `model_fn` returns a `Tensor` for `predictions` or a `dict`
  of numpy arrays if `model_fn` returns a `dict`. Returns an iterable of
  predictions if as_iterable is True.

Raises:
  ValueError: If x and input_fn are both provided or both `None`.

tf.contrib.learn.DNNRegressor.set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The former have parameters of the form <component>__<parameter> so that it's possible to update each component of a nested object.

Args:
  • **params: Parameters.
Returns:

self

Raises:
  • ValueError: If params contain invalid names.

tf.contrib.learn.DNNRegressor.weights_


class tf.contrib.learn.TensorFlowEstimator

Base class for all TensorFlow estimators.


tf.contrib.learn.TensorFlowEstimator.__init__(model_fn, n_classes, batch_size=32, steps=200, optimizer='Adagrad', learning_rate=0.1, clip_gradients=5.0, class_weight=None, continue_training=False, config=None, verbose=1) {:#TensorFlowEstimator.init}

Initializes a TensorFlowEstimator instance.

Args:
  • model_fn: Model function, that takes input x, y tensors and outputs prediction and loss tensors.
  • n_classes: Number of classes in the target.
  • batch_size: Mini batch size.
  • steps: Number of steps to run over data.
  • optimizer: Optimizer name (or class), for example "SGD", "Adam", "Adagrad".
  • learning_rate: If this is constant float value, no decay function is used. Instead, a customized decay function can be passed that accepts global_step as parameter and returns a Tensor. e.g. exponential decay function:

    python def exp_decay(global_step): return tf.train.exponential_decay( learning_rate=0.1, global_step, decay_steps=2, decay_rate=0.001)

  • clip_gradients: Clip norm of the gradients to this value to stop gradient explosion.

  • class_weight: None or list of n_classes floats. Weight associated with classes for loss computation. If not given, all classes are supposed to have weight one.
  • continue_training: when continue_training is True, once initialized model will be continuely trained on every call of fit.
  • config: RunConfig object that controls the configurations of the session, e.g. num_cores, gpu_memory_fraction, etc.
  • verbose: Controls the verbosity, possible values:

    • 0: the algorithm and debug information is muted.
    • 1: trainer prints the progress.
    • 2: log device placement is printed.

tf.contrib.learn.TensorFlowEstimator.__repr__() {:#TensorFlowEstimator.repr}


tf.contrib.learn.TensorFlowEstimator.config


tf.contrib.learn.TensorFlowEstimator.evaluate(x=None, y=None, input_fn=None, feed_fn=None, batch_size=None, steps=None, metrics=None, name=None)

Evaluates given model with provided evaluation data.

See superclass Estimator for more details.

Args:
  • x: features.
  • y: targets.
  • input_fn: Input function.
  • feed_fn: Function creating a feed dict every time it is called.
  • batch_size: minibatch size to use on the input.
  • steps: Number of steps for which to evaluate model.
  • metrics: Dict of metric ops to run. If None, the default metrics are used.
  • name: Name of the evaluation.
Returns:

Returns dict with evaluation results.


tf.contrib.learn.TensorFlowEstimator.export(*args, **kwargs)

Exports inference graph into given dir. (deprecated arguments)

SOME ARGUMENTS ARE DEPRECATED. They will be removed after 2016-09-23. Instructions for updating: The signature of the input_fn accepted by export is changing to be consistent with what's used by tf.Learn Estimator's train/evaluate. input_fn and input_feature_key will become required args, and use_deprecated_input_fn will default to False and be removed altogether.

Args:
  export_dir: A string containing a directory to write the exported graph
    and checkpoints.
  input_fn: If `use_deprecated_input_fn` is true, then a function that given
    `Tensor` of `Example` strings, parses it into features that are then
    passed to the model. Otherwise, a function that takes no argument and
    returns a tuple of (features, targets), where features is a dict of
    string key to `Tensor` and targets is a `Tensor` that's currently not
    used (and so can be `None`).
  input_feature_key: Only used if `use_deprecated_input_fn` is false. String
    key into the features dict returned by `input_fn` that corresponds toa
    the raw `Example` strings `Tensor` that the exported model will take as
    input.
  use_deprecated_input_fn: Determines the signature format of `input_fn`.
  signature_fn: Function that returns a default signature and a named
    signature map, given `Tensor` of `Example` strings, `dict` of `Tensor`s
    for features and `Tensor` or `dict` of `Tensor`s for predictions.
  prediction_key: The key for a tensor in the `predictions` dict (output
    from the `model_fn`) to use as the `predictions` input to the
    `signature_fn`. Optional. If `None`, predictions will pass to
    `signature_fn` without filtering.
  default_batch_size: Default batch size of the `Example` placeholder.
  exports_to_keep: Number of exports to keep.

Returns:
  The string path to the exported directory. NB: this functionality was
  added ca. 2016/09/25; clients that depend on the return value may need
  to handle the case where this function returns None because subclasses
  are not returning a value.

tf.contrib.learn.TensorFlowEstimator.fit(x, y, steps=None, monitors=None, logdir=None)

Neural network model from provided model_fn and training data.

Args:
  • x: matrix or tensor of shape [n_samples, n_features...]. Can be iterator that returns arrays of features. The training input samples for fitting the model.

  • y: vector or matrix [n_samples] or [n_samples, n_outputs]. Can be iterator that returns array of targets. The training target values (class labels in classification, real numbers in regression).

  • steps: int, number of steps to train. If None or 0, train for self.steps.

  • monitors: List of BaseMonitor objects to print training progress and invoke early stopping.
  • logdir: the directory to save the log file that can be used for optional visualization.
Returns:

Returns self.


tf.contrib.learn.TensorFlowEstimator.get_params(deep=True)

Get parameters for this estimator.

Args:
  • deep: boolean, optional

    If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params : mapping of string to any Parameter names mapped to their values.


tf.contrib.learn.TensorFlowEstimator.get_tensor(name)

Returns tensor by name.

Args:
  • name: string, name of the tensor.
Returns:

Tensor.


tf.contrib.learn.TensorFlowEstimator.get_variable_names()

Returns list of all variable names in this model.

Returns:

List of names.


tf.contrib.learn.TensorFlowEstimator.get_variable_value(name)

Returns value of the variable given by name.

Args:
  • name: string, name of the tensor.
Returns:

Numpy array - value of the tensor.


tf.contrib.learn.TensorFlowEstimator.model_dir


tf.contrib.learn.TensorFlowEstimator.partial_fit(x, y)

Incremental fit on a batch of samples.

This method is expected to be called several times consecutively on different or the same chunks of the dataset. This either can implement iterative training or out-of-core/online training. This is especially useful when the whole dataset is too big to fit in memory at the same time. Or when model is taking long time to converge, and you want to split up training into subparts.

Args:
  • x: matrix or tensor of shape [n_samples, n_features...]. Can be iterator that returns arrays of features. The training input samples for fitting the model.

  • y: vector or matrix [n_samples] or [n_samples, n_outputs]. Can be iterator that returns array of targets. The training target values (class label in classification, real numbers in regression).

Returns:

Returns self.


tf.contrib.learn.TensorFlowEstimator.predict(x, axis=1, batch_size=None)

Predict class or regression for x.

For a classification model, the predicted class for each sample in x is returned. For a regression model, the predicted value based on x is returned.

Args:
  • x: array-like matrix, [n_samples, n_features...] or iterator.
  • axis: Which axis to argmax for classification. By default axis 1 (next after batch) is used. Use 2 for sequence predictions.
  • batch_size: If test set is too big, use batch size to split it into mini batches. By default the batch_size member variable is used.
Returns:
  • y: array of shape [n_samples]. The predicted classes or predicted value.

tf.contrib.learn.TensorFlowEstimator.predict_proba(x, batch_size=None)

Predict class probability of the input samples x.

Args:
  • x: array-like matrix, [n_samples, n_features...] or iterator.
  • batch_size: If test set is too big, use batch size to split it into mini batches. By default the batch_size member variable is used.
Returns:
  • y: array of shape [n_samples, n_classes]. The predicted probabilities for each class.

tf.contrib.learn.TensorFlowEstimator.restore(cls, path, config=None)

Restores model from give path.

Args:
  • path: Path to the checkpoints and other model information.
  • config: RunConfig object that controls the configurations of the session, e.g. num_cores, gpu_memory_fraction, etc. This is allowed to be reconfigured.
Returns:

Estimator, object of the subclass of TensorFlowEstimator.

Raises:
  • ValueError: if path does not contain a model definition.

tf.contrib.learn.TensorFlowEstimator.save(path)

Saves checkpoints and graph to given path.

Args:
  • path: Folder to save model to.

tf.contrib.learn.TensorFlowEstimator.set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The former have parameters of the form <component>__<parameter> so that it's possible to update each component of a nested object.

Args:
  • **params: Parameters.
Returns:

self

Raises:
  • ValueError: If params contain invalid names.

class tf.contrib.learn.LinearClassifier

Linear classifier model.

Train a linear model to classify instances into one of multiple possible classes. When number of possible classes is 2, this is binary classification.

Example:

education = sparse_column_with_hash_bucket(column_name="education",
                                           hash_bucket_size=1000)
occupation = sparse_column_with_hash_bucket(column_name="occupation",
                                            hash_bucket_size=1000)

education_x_occupation = crossed_column(columns=[education, occupation],
                                        hash_bucket_size=10000)

# Estimator using the default optimizer.
estimator = LinearClassifier(
    feature_columns=[occupation, education_x_occupation])

# Or estimator using the FTRL optimizer with regularization.
estimator = LinearClassifier(
    feature_columns=[occupation, education_x_occupation],
    optimizer=tf.train.FtrlOptimizer(
      learning_rate=0.1,
      l1_regularization_strength=0.001
    ))

# Or estimator using the SDCAOptimizer.
estimator = LinearClassifier(
   feature_columns=[occupation, education_x_occupation],
   optimizer=tf.contrib.linear_optimizer.SDCAOptimizer(
     example_id_column='example_id',
     num_loss_partitions=...,
     symmetric_l2_regularization=2.0
   ))

# Input builders
def input_fn_train: # returns x, y
  ...
def input_fn_eval: # returns x, y
  ...
estimator.fit(input_fn=input_fn_train)
estimator.evaluate(input_fn=input_fn_eval)
estimator.predict(x=x)

Input of fit and evaluate should have following features, otherwise there will be a KeyError:

  • if weight_column_name is not None, a feature with key=weight_column_name whose value is a Tensor.
  • for each column in feature_columns:
  • if column is a SparseColumn, a feature with key=column.name whose value is a SparseTensor.
  • if column is a WeightedSparseColumn, 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 RealValuedColumn, a feature with key=column.name whose value is a Tensor.

tf.contrib.learn.LinearClassifier.__init__(feature_columns, model_dir=None, n_classes=2, weight_column_name=None, optimizer=None, gradient_clip_norm=None, enable_centered_bias=None, _joint_weight=False, config=None) {:#LinearClassifier.init}

Construct a LinearClassifier estimator object.

Args:
  • feature_columns: An iterable containing all the feature columns used by the model. All items in the set should be instances of classes derived from FeatureColumn.
  • 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.
  • n_classes: number of target classes. Default is binary classification.
  • weight_column_name: A string defining feature column name representing weights. It is used to down weight or boost examples during training. It will be multiplied by the loss of the example.
  • optimizer: The optimizer used to train the model. If specified, it should be either an instance of tf.Optimizer or the SDCAOptimizer. If None, the Ftrl optimizer will be used.
  • gradient_clip_norm: A float > 0. If provided, gradients are clipped to their global norm with this clipping ratio. See tf.clip_by_global_norm for more details.
  • enable_centered_bias: A bool. If True, estimator will learn a centered bias variable for each class. Rest of the model structure learns the residual after centered bias. _joint_weight: If True, the weights for all columns will be stored in a single (possibly partitioned) variable. It's more efficient, but it's incompatible with SDCAOptimizer, and requires all feature columns are sparse and use the 'sum' combiner.

  • config: RunConfig object to configure the runtime settings.

Returns:

A LinearClassifier estimator.

Raises:
  • ValueError: if n_classes < 2.

tf.contrib.learn.LinearClassifier.bias_


tf.contrib.learn.LinearClassifier.config


tf.contrib.learn.LinearClassifier.evaluate(x=None, y=None, input_fn=None, feed_fn=None, batch_size=None, steps=None, metrics=None, name=None)

See evaluable.Evaluable.


tf.contrib.learn.LinearClassifier.export(export_dir, input_fn=None, input_feature_key=None, use_deprecated_input_fn=True, signature_fn=None, default_batch_size=1, exports_to_keep=None)

See BaseEstimator.export.


tf.contrib.learn.LinearClassifier.fit(x=None, y=None, input_fn=None, steps=None, batch_size=None, monitors=None, max_steps=None)

See trainable.Trainable.


tf.contrib.learn.LinearClassifier.get_estimator()


tf.contrib.learn.LinearClassifier.get_variable_names()


tf.contrib.learn.LinearClassifier.get_variable_value(name)


tf.contrib.learn.LinearClassifier.model_dir


tf.contrib.learn.LinearClassifier.predict(x=None, input_fn=None, batch_size=None, as_iterable=False)

Runs inference to determine the predicted class.


tf.contrib.learn.LinearClassifier.predict_proba(x=None, input_fn=None, batch_size=None, outputs=None, as_iterable=False)

Runs inference to determine the class probability predictions.


tf.contrib.learn.LinearClassifier.weights_


class tf.contrib.learn.LinearRegressor

Linear regressor model.

Train a linear regression model to predict target variable value given observation of feature values.

Example:

education = sparse_column_with_hash_bucket(column_name="education",
                                           hash_bucket_size=1000)
occupation = sparse_column_with_hash_bucket(column_name="occupation",
                                            hash_bucket_size=1000)

education_x_occupation = crossed_column(columns=[education, occupation],
                                        hash_bucket_size=10000)

estimator = LinearRegressor(
    feature_columns=[occupation, education_x_occupation])

# Input builders
def input_fn_train: # returns x, y
  ...
def input_fn_eval: # returns x, y
  ...
estimator.fit(input_fn=input_fn_train)
estimator.evaluate(input_fn=input_fn_eval)
estimator.predict(x=x)

Input of fit and evaluate should have following features, otherwise there will be a KeyError:

  • if weight_column_name is not None: key=weight_column_name, value=a Tensor
  • for column in feature_columns:
  • if isinstance(column, SparseColumn): key=column.name, value=a SparseTensor
  • if isinstance(column, WeightedSparseColumn): {key=id column name, value=a SparseTensor, key=weight column name, value=a SparseTensor}
  • if isinstance(column, RealValuedColumn): key=column.name, value=a Tensor

tf.contrib.learn.LinearRegressor.__init__(feature_columns, model_dir=None, weight_column_name=None, optimizer=None, gradient_clip_norm=None, enable_centered_bias=None, target_dimension=1, _joint_weights=False, config=None) {:#LinearRegressor.init}

Construct a LinearRegressor estimator object.

Args:
  • feature_columns: An iterable containing all the feature columns used by the model. All items in the set should be instances of classes derived from FeatureColumn.
  • model_dir: Directory to save model parameters, graph, etc. This can also be used to load checkpoints from the directory into a estimator to continue training a previously saved model.
  • weight_column_name: A string defining feature column name representing weights. It is used to down weight or boost examples during training. It will be multiplied by the loss of the example.
  • optimizer: An instance of tf.Optimizer used to train the model. If None, will use an Ftrl optimizer.
  • gradient_clip_norm: A float > 0. If provided, gradients are clipped to their global norm with this clipping ratio. See tf.clip_by_global_norm for more details.
  • enable_centered_bias: A bool. If True, estimator will learn a centered bias variable for each class. Rest of the model structure learns the residual after centered bias.
  • target_dimension: dimension of the target for multilabels. _joint_weights: If True use a single (possibly partitioned) variable to store the weights. It's faster, but requires all feature columns are sparse and have the 'sum' combiner. Incompatible with SDCAOptimizer.

  • config: RunConfig object to configure the runtime settings.

Returns:

A LinearRegressor estimator.


tf.contrib.learn.LinearRegressor.__repr__() {:#LinearRegressor.repr}


tf.contrib.learn.LinearRegressor.bias_


tf.contrib.learn.LinearRegressor.config


tf.contrib.learn.LinearRegressor.dnn_bias_

Returns bias of deep neural network part.


tf.contrib.learn.LinearRegressor.dnn_weights_

Returns weights of deep neural network part.


tf.contrib.learn.LinearRegressor.evaluate(x=None, y=None, input_fn=None, feed_fn=None, batch_size=None, steps=None, metrics=None, name=None)

See Evaluable.

Raises:
  • ValueError: If at least one of x or y is provided, and at least one of input_fn or feed_fn is provided. Or if metrics is not None or dict.

tf.contrib.learn.LinearRegressor.export(*args, **kwargs)

Exports inference graph into given dir. (deprecated arguments)

SOME ARGUMENTS ARE DEPRECATED. They will be removed after 2016-09-23. Instructions for updating: The signature of the input_fn accepted by export is changing to be consistent with what's used by tf.Learn Estimator's train/evaluate. input_fn and input_feature_key will become required args, and use_deprecated_input_fn will default to False and be removed altogether.

Args:
  export_dir: A string containing a directory to write the exported graph
    and checkpoints.
  input_fn: If `use_deprecated_input_fn` is true, then a function that given
    `Tensor` of `Example` strings, parses it into features that are then
    passed to the model. Otherwise, a function that takes no argument and
    returns a tuple of (features, targets), where features is a dict of
    string key to `Tensor` and targets is a `Tensor` that's currently not
    used (and so can be `None`).
  input_feature_key: Only used if `use_deprecated_input_fn` is false. String
    key into the features dict returned by `input_fn` that corresponds toa
    the raw `Example` strings `Tensor` that the exported model will take as
    input.
  use_deprecated_input_fn: Determines the signature format of `input_fn`.
  signature_fn: Function that returns a default signature and a named
    signature map, given `Tensor` of `Example` strings, `dict` of `Tensor`s
    for features and `Tensor` or `dict` of `Tensor`s for predictions.
  prediction_key: The key for a tensor in the `predictions` dict (output
    from the `model_fn`) to use as the `predictions` input to the
    `signature_fn`. Optional. If `None`, predictions will pass to
    `signature_fn` without filtering.
  default_batch_size: Default batch size of the `Example` placeholder.
  exports_to_keep: Number of exports to keep.

Returns:
  The string path to the exported directory. NB: this functionality was
  added ca. 2016/09/25; clients that depend on the return value may need
  to handle the case where this function returns None because subclasses
  are not returning a value.

tf.contrib.learn.LinearRegressor.fit(x=None, y=None, input_fn=None, steps=None, batch_size=None, monitors=None, max_steps=None)

See Trainable.

Raises:
  • ValueError: If x or y are not None while input_fn is not None.
  • ValueError: If both steps and max_steps are not None.

tf.contrib.learn.LinearRegressor.get_params(deep=True)

Get parameters for this estimator.

Args:
  • deep: boolean, optional

    If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params : mapping of string to any Parameter names mapped to their values.


tf.contrib.learn.LinearRegressor.get_variable_names()

Returns list of all variable names in this model.

Returns:

List of names.


tf.contrib.learn.LinearRegressor.get_variable_value(name)

Returns value of the variable given by name.

Args:
  • name: string, name of the tensor.
Returns:

Numpy array - value of the tensor.


tf.contrib.learn.LinearRegressor.linear_bias_

Returns bias of the linear part.


tf.contrib.learn.LinearRegressor.linear_weights_

Returns weights per feature of the linear part.


tf.contrib.learn.LinearRegressor.model_dir


tf.contrib.learn.LinearRegressor.partial_fit(x=None, y=None, input_fn=None, steps=1, batch_size=None, monitors=None)

Incremental fit on a batch of samples.

This method is expected to be called several times consecutively on different or the same chunks of the dataset. This either can implement iterative training or out-of-core/online training.

This is especially useful when the whole dataset is too big to fit in memory at the same time. Or when model is taking long time to converge, and you want to split up training into subparts.

Args:
  • x: Matrix of shape [n_samples, n_features...]. Can be iterator that returns arrays of features. The training input samples for fitting the model. If set, input_fn must be None.
  • y: Vector or matrix [n_samples] or [n_samples, n_outputs]. Can be iterator that returns array of targets. The training target values (class labels in classification, real numbers in regression). If set, input_fn must be None.
  • input_fn: Input function. If set, x, y, and batch_size must be None.
  • steps: Number of steps for which to train model. If None, train forever.
  • batch_size: minibatch size to use on the input, defaults to first dimension of x. Must be None if input_fn is provided.
  • monitors: List of BaseMonitor subclass instances. Used for callbacks inside the training loop.
Returns:

self, for chaining.

Raises:
  • ValueError: If at least one of x and y is provided, and input_fn is provided.

tf.contrib.learn.LinearRegressor.predict(*args, **kwargs)

Returns predictions for given features. (deprecated arguments)

SOME ARGUMENTS ARE DEPRECATED. They will be removed after 2016-09-15. Instructions for updating: The default behavior of predict() is changing. The default value for as_iterable will change to True, and then the flag will be removed altogether. The behavior of this flag is described below.

Args:
  x: Matrix of shape [n_samples, n_features...]. Can be iterator that
     returns arrays of features. The training input samples for fitting the
     model. If set, `input_fn` must be `None`.
  input_fn: Input function. If set, `x` and 'batch_size' must be `None`.
  batch_size: Override default batch size. If set, 'input_fn' must be
    'None'.
  outputs: list of `str`, name of the output to predict.
    If `None`, returns all.
  as_iterable: If True, return an iterable which keeps yielding predictions
    for each example until inputs are exhausted. Note: The inputs must
    terminate if you want the iterable to terminate (e.g. be sure to pass
    num_epochs=1 if you are using something like read_batch_features).

Returns:
  A numpy array of predicted classes or regression values if the
  constructor's `model_fn` returns a `Tensor` for `predictions` or a `dict`
  of numpy arrays if `model_fn` returns a `dict`. Returns an iterable of
  predictions if as_iterable is True.

Raises:
  ValueError: If x and input_fn are both provided or both `None`.

tf.contrib.learn.LinearRegressor.set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The former have parameters of the form <component>__<parameter> so that it's possible to update each component of a nested object.

Args:
  • **params: Parameters.
Returns:

self

Raises:
  • ValueError: If params contain invalid names.

tf.contrib.learn.LinearRegressor.weights_


class tf.contrib.learn.TensorFlowRNNClassifier

TensorFlow RNN Classifier model.


tf.contrib.learn.TensorFlowRNNClassifier.__init__(rnn_size, n_classes, cell_type='gru', num_layers=1, input_op_fn=null_input_op_fn, initial_state=None, bidirectional=False, sequence_length=None, attn_length=None, attn_size=None, attn_vec_size=None, batch_size=32, steps=50, optimizer='Adagrad', learning_rate=0.1, class_weight=None, clip_gradients=5.0, continue_training=False, config=None, verbose=1) {:#TensorFlowRNNClassifier.init}

Initializes a TensorFlowRNNClassifier instance.

Args:
  • rnn_size: The size for rnn cell, e.g. size of your word embeddings.
  • cell_type: The type of rnn cell, including rnn, gru, and lstm.
  • num_layers: The number of layers of the rnn model.
  • input_op_fn: Function that will transform the input tensor, such as creating word embeddings, byte list, etc. This takes an argument x for input and returns transformed x.
  • bidirectional: boolean, Whether this is a bidirectional rnn.
  • sequence_length: If sequence_length is provided, dynamic calculation is performed. This saves computational time when unrolling past max sequence length.
  • initial_state: An initial state for the RNN. This must be a tensor of appropriate type and shape [batch_size x cell.state_size].
  • attn_length: integer, the size of attention vector attached to rnn cells.
  • attn_size: integer, the size of an attention window attached to rnn cells.
  • attn_vec_size: integer, the number of convolutional features calculated on attention state and the size of the hidden layer built from base cell state.
  • n_classes: Number of classes in the target.
  • batch_size: Mini batch size.
  • steps: Number of steps to run over data.
  • optimizer: Optimizer name (or class), for example "SGD", "Adam", "Adagrad".
  • learning_rate: If this is constant float value, no decay function is used. Instead, a customized decay function can be passed that accepts global_step as parameter and returns a Tensor. e.g. exponential decay function:

    python def exp_decay(global_step): return tf.train.exponential_decay( learning_rate=0.1, global_step, decay_steps=2, decay_rate=0.001)

  • class_weight: None or list of n_classes floats. Weight associated with classes for loss computation. If not given, all classes are supposed to have weight one.

  • continue_training: when continue_training is True, once initialized model will be continuely trained on every call of fit.
  • config: RunConfig object that controls the configurations of the session, e.g. num_cores, gpu_memory_fraction, etc.

tf.contrib.learn.TensorFlowRNNClassifier.__repr__() {:#TensorFlowRNNClassifier.repr}


tf.contrib.learn.TensorFlowRNNClassifier.bias_

Returns bias of the rnn layer.


tf.contrib.learn.TensorFlowRNNClassifier.config


tf.contrib.learn.TensorFlowRNNClassifier.evaluate(x=None, y=None, input_fn=None, feed_fn=None, batch_size=None, steps=None, metrics=None, name=None)

Evaluates given model with provided evaluation data.

See superclass Estimator for more details.

Args:
  • x: features.
  • y: targets.
  • input_fn: Input function.
  • feed_fn: Function creating a feed dict every time it is called.
  • batch_size: minibatch size to use on the input.
  • steps: Number of steps for which to evaluate model.
  • metrics: Dict of metric ops to run. If None, the default metrics are used.
  • name: Name of the evaluation.
Returns:

Returns dict with evaluation results.


tf.contrib.learn.TensorFlowRNNClassifier.export(*args, **kwargs)

Exports inference graph into given dir. (deprecated arguments)

SOME ARGUMENTS ARE DEPRECATED. They will be removed after 2016-09-23. Instructions for updating: The signature of the input_fn accepted by export is changing to be consistent with what's used by tf.Learn Estimator's train/evaluate. input_fn and input_feature_key will become required args, and use_deprecated_input_fn will default to False and be removed altogether.

Args:
  export_dir: A string containing a directory to write the exported graph
    and checkpoints.
  input_fn: If `use_deprecated_input_fn` is true, then a function that given
    `Tensor` of `Example` strings, parses it into features that are then
    passed to the model. Otherwise, a function that takes no argument and
    returns a tuple of (features, targets), where features is a dict of
    string key to `Tensor` and targets is a `Tensor` that's currently not
    used (and so can be `None`).
  input_feature_key: Only used if `use_deprecated_input_fn` is false. String
    key into the features dict returned by `input_fn` that corresponds toa
    the raw `Example` strings `Tensor` that the exported model will take as
    input.
  use_deprecated_input_fn: Determines the signature format of `input_fn`.
  signature_fn: Function that returns a default signature and a named
    signature map, given `Tensor` of `Example` strings, `dict` of `Tensor`s
    for features and `Tensor` or `dict` of `Tensor`s for predictions.
  prediction_key: The key for a tensor in the `predictions` dict (output
    from the `model_fn`) to use as the `predictions` input to the
    `signature_fn`. Optional. If `None`, predictions will pass to
    `signature_fn` without filtering.
  default_batch_size: Default batch size of the `Example` placeholder.
  exports_to_keep: Number of exports to keep.

Returns:
  The string path to the exported directory. NB: this functionality was
  added ca. 2016/09/25; clients that depend on the return value may need
  to handle the case where this function returns None because subclasses
  are not returning a value.

tf.contrib.learn.TensorFlowRNNClassifier.fit(x, y, steps=None, monitors=None, logdir=None)

Neural network model from provided model_fn and training data.

Args:
  • x: matrix or tensor of shape [n_samples, n_features...]. Can be iterator that returns arrays of features. The training input samples for fitting the model.

  • y: vector or matrix [n_samples] or [n_samples, n_outputs]. Can be iterator that returns array of targets. The training target values (class labels in classification, real numbers in regression).

  • steps: int, number of steps to train. If None or 0, train for self.steps.

  • monitors: List of BaseMonitor objects to print training progress and invoke early stopping.
  • logdir: the directory to save the log file that can be used for optional visualization.
Returns:

Returns self.


tf.contrib.learn.TensorFlowRNNClassifier.get_params(deep=True)

Get parameters for this estimator.

Args:
  • deep: boolean, optional

    If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params : mapping of string to any Parameter names mapped to their values.


tf.contrib.learn.TensorFlowRNNClassifier.get_tensor(name)

Returns tensor by name.

Args:
  • name: string, name of the tensor.
Returns:

Tensor.


tf.contrib.learn.TensorFlowRNNClassifier.get_variable_names()

Returns list of all variable names in this model.

Returns:

List of names.


tf.contrib.learn.TensorFlowRNNClassifier.get_variable_value(name)

Returns value of the variable given by name.

Args:
  • name: string, name of the tensor.
Returns:

Numpy array - value of the tensor.


tf.contrib.learn.TensorFlowRNNClassifier.model_dir


tf.contrib.learn.TensorFlowRNNClassifier.partial_fit(x, y)

Incremental fit on a batch of samples.

This method is expected to be called several times consecutively on different or the same chunks of the dataset. This either can implement iterative training or out-of-core/online training. This is especially useful when the whole dataset is too big to fit in memory at the same time. Or when model is taking long time to converge, and you want to split up training into subparts.

Args:
  • x: matrix or tensor of shape [n_samples, n_features...]. Can be iterator that returns arrays of features. The training input samples for fitting the model.

  • y: vector or matrix [n_samples] or [n_samples, n_outputs]. Can be iterator that returns array of targets. The training target values (class label in classification, real numbers in regression).

Returns:

Returns self.


tf.contrib.learn.TensorFlowRNNClassifier.predict(x, axis=1, batch_size=None)

Predict class or regression for x.

For a classification model, the predicted class for each sample in x is returned. For a regression model, the predicted value based on x is returned.

Args:
  • x: array-like matrix, [n_samples, n_features...] or iterator.
  • axis: Which axis to argmax for classification. By default axis 1 (next after batch) is used. Use 2 for sequence predictions.
  • batch_size: If test set is too big, use batch size to split it into mini batches. By default the batch_size member variable is used.
Returns:
  • y: array of shape [n_samples]. The predicted classes or predicted value.

tf.contrib.learn.TensorFlowRNNClassifier.predict_proba(x, batch_size=None)

Predict class probability of the input samples x.

Args:
  • x: array-like matrix, [n_samples, n_features...] or iterator.
  • batch_size: If test set is too big, use batch size to split it into mini batches. By default the batch_size member variable is used.
Returns:
  • y: array of shape [n_samples, n_classes]. The predicted probabilities for each class.

tf.contrib.learn.TensorFlowRNNClassifier.restore(cls, path, config=None)

Restores model from give path.

Args:
  • path: Path to the checkpoints and other model information.
  • config: RunConfig object that controls the configurations of the session, e.g. num_cores, gpu_memory_fraction, etc. This is allowed to be reconfigured.
Returns:

Estimator, object of the subclass of TensorFlowEstimator.

Raises:
  • ValueError: if path does not contain a model definition.

tf.contrib.learn.TensorFlowRNNClassifier.save(path)

Saves checkpoints and graph to given path.

Args:
  • path: Folder to save model to.

tf.contrib.learn.TensorFlowRNNClassifier.set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The former have parameters of the form <component>__<parameter> so that it's possible to update each component of a nested object.

Args:
  • **params: Parameters.
Returns:

self

Raises:
  • ValueError: If params contain invalid names.

tf.contrib.learn.TensorFlowRNNClassifier.weights_

Returns weights of the rnn layer.


class tf.contrib.learn.TensorFlowRNNRegressor

TensorFlow RNN Regressor model.


tf.contrib.learn.TensorFlowRNNRegressor.__init__(rnn_size, cell_type='gru', num_layers=1, input_op_fn=null_input_op_fn, initial_state=None, bidirectional=False, sequence_length=None, attn_length=None, attn_size=None, attn_vec_size=None, n_classes=0, batch_size=32, steps=50, optimizer='Adagrad', learning_rate=0.1, clip_gradients=5.0, continue_training=False, config=None, verbose=1) {:#TensorFlowRNNRegressor.init}

Initializes a TensorFlowRNNRegressor instance.

Args:
  • rnn_size: The size for rnn cell, e.g. size of your word embeddings.
  • cell_type: The type of rnn cell, including rnn, gru, and lstm.
  • num_layers: The number of layers of the rnn model.
  • input_op_fn: Function that will transform the input tensor, such as creating word embeddings, byte list, etc. This takes an argument x for input and returns transformed x.
  • bidirectional: boolean, Whether this is a bidirectional rnn.
  • sequence_length: If sequence_length is provided, dynamic calculation is performed. This saves computational time when unrolling past max sequence length.
  • attn_length: integer, the size of attention vector attached to rnn cells.
  • attn_size: integer, the size of an attention window attached to rnn cells.
  • attn_vec_size: integer, the number of convolutional features calculated on attention state and the size of the hidden layer built from base cell state.
  • initial_state: An initial state for the RNN. This must be a tensor of appropriate type and shape [batch_size x cell.state_size].
  • batch_size: Mini batch size.
  • steps: Number of steps to run over data.
  • optimizer: Optimizer name (or class), for example "SGD", "Adam", "Adagrad".
  • learning_rate: If this is constant float value, no decay function is used. Instead, a customized decay function can be passed that accepts global_step as parameter and returns a Tensor. e.g. exponential decay function:

    python def exp_decay(global_step): return tf.train.exponential_decay( learning_rate=0.1, global_step, decay_steps=2, decay_rate=0.001)

  • continue_training: when continue_training is True, once initialized model will be continuely trained on every call of fit.

  • config: RunConfig object that controls the configurations of the session, e.g. num_cores, gpu_memory_fraction, etc.
  • verbose: Controls the verbosity, possible values:

    • 0: the algorithm and debug information is muted.
    • 1: trainer prints the progress.
    • 2: log device placement is printed.

tf.contrib.learn.TensorFlowRNNRegressor.__repr__() {:#TensorFlowRNNRegressor.repr}


tf.contrib.learn.TensorFlowRNNRegressor.bias_

Returns bias of the rnn layer.


tf.contrib.learn.TensorFlowRNNRegressor.config


tf.contrib.learn.TensorFlowRNNRegressor.evaluate(x=None, y=None, input_fn=None, feed_fn=None, batch_size=None, steps=None, metrics=None, name=None)

Evaluates given model with provided evaluation data.

See superclass Estimator for more details.

Args:
  • x: features.
  • y: targets.
  • input_fn: Input function.
  • feed_fn: Function creating a feed dict every time it is called.
  • batch_size: minibatch size to use on the input.
  • steps: Number of steps for which to evaluate model.
  • metrics: Dict of metric ops to run. If None, the default metrics are used.
  • name: Name of the evaluation.
Returns:

Returns dict with evaluation results.


tf.contrib.learn.TensorFlowRNNRegressor.export(*args, **kwargs)

Exports inference graph into given dir. (deprecated arguments)

SOME ARGUMENTS ARE DEPRECATED. They will be removed after 2016-09-23. Instructions for updating: The signature of the input_fn accepted by export is changing to be consistent with what's used by tf.Learn Estimator's train/evaluate. input_fn and input_feature_key will become required args, and use_deprecated_input_fn will default to False and be removed altogether.

Args:
  export_dir: A string containing a directory to write the exported graph
    and checkpoints.
  input_fn: If `use_deprecated_input_fn` is true, then a function that given
    `Tensor` of `Example` strings, parses it into features that are then
    passed to the model. Otherwise, a function that takes no argument and
    returns a tuple of (features, targets), where features is a dict of
    string key to `Tensor` and targets is a `Tensor` that's currently not
    used (and so can be `None`).
  input_feature_key: Only used if `use_deprecated_input_fn` is false. String
    key into the features dict returned by `input_fn` that corresponds toa
    the raw `Example` strings `Tensor` that the exported model will take as
    input.
  use_deprecated_input_fn: Determines the signature format of `input_fn`.
  signature_fn: Function that returns a default signature and a named
    signature map, given `Tensor` of `Example` strings, `dict` of `Tensor`s
    for features and `Tensor` or `dict` of `Tensor`s for predictions.
  prediction_key: The key for a tensor in the `predictions` dict (output
    from the `model_fn`) to use as the `predictions` input to the
    `signature_fn`. Optional. If `None`, predictions will pass to
    `signature_fn` without filtering.
  default_batch_size: Default batch size of the `Example` placeholder.
  exports_to_keep: Number of exports to keep.

Returns:
  The string path to the exported directory. NB: this functionality was
  added ca. 2016/09/25; clients that depend on the return value may need
  to handle the case where this function returns None because subclasses
  are not returning a value.

tf.contrib.learn.TensorFlowRNNRegressor.fit(x, y, steps=None, monitors=None, logdir=None)

Neural network model from provided model_fn and training data.

Args:
  • x: matrix or tensor of shape [n_samples, n_features...]. Can be iterator that returns arrays of features. The training input samples for fitting the model.

  • y: vector or matrix [n_samples] or [n_samples, n_outputs]. Can be iterator that returns array of targets. The training target values (class labels in classification, real numbers in regression).

  • steps: int, number of steps to train. If None or 0, train for self.steps.

  • monitors: List of BaseMonitor objects to print training progress and invoke early stopping.
  • logdir: the directory to save the log file that can be used for optional visualization.
Returns:

Returns self.


tf.contrib.learn.TensorFlowRNNRegressor.get_params(deep=True)

Get parameters for this estimator.

Args:
  • deep: boolean, optional

    If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params : mapping of string to any Parameter names mapped to their values.


tf.contrib.learn.TensorFlowRNNRegressor.get_tensor(name)

Returns tensor by name.

Args:
  • name: string, name of the tensor.
Returns:

Tensor.


tf.contrib.learn.TensorFlowRNNRegressor.get_variable_names()

Returns list of all variable names in this model.

Returns:

List of names.


tf.contrib.learn.TensorFlowRNNRegressor.get_variable_value(name)

Returns value of the variable given by name.

Args:
  • name: string, name of the tensor.
Returns:

Numpy array - value of the tensor.


tf.contrib.learn.TensorFlowRNNRegressor.model_dir


tf.contrib.learn.TensorFlowRNNRegressor.partial_fit(x, y)

Incremental fit on a batch of samples.

This method is expected to be called several times consecutively on different or the same chunks of the dataset. This either can implement iterative training or out-of-core/online training. This is especially useful when the whole dataset is too big to fit in memory at the same time. Or when model is taking long time to converge, and you want to split up training into subparts.

Args:
  • x: matrix or tensor of shape [n_samples, n_features...]. Can be iterator that returns arrays of features. The training input samples for fitting the model.

  • y: vector or matrix [n_samples] or [n_samples, n_outputs]. Can be iterator that returns array of targets. The training target values (class label in classification, real numbers in regression).

Returns:

Returns self.


tf.contrib.learn.TensorFlowRNNRegressor.predict(x, axis=1, batch_size=None)

Predict class or regression for x.

For a classification model, the predicted class for each sample in x is returned. For a regression model, the predicted value based on x is returned.

Args:
  • x: array-like matrix, [n_samples, n_features...] or iterator.
  • axis: Which axis to argmax for classification. By default axis 1 (next after batch) is used. Use 2 for sequence predictions.
  • batch_size: If test set is too big, use batch size to split it into mini batches. By default the batch_size member variable is used.
Returns:
  • y: array of shape [n_samples]. The predicted classes or predicted value.

tf.contrib.learn.TensorFlowRNNRegressor.predict_proba(x, batch_size=None)

Predict class probability of the input samples x.

Args:
  • x: array-like matrix, [n_samples, n_features...] or iterator.
  • batch_size: If test set is too big, use batch size to split it into mini batches. By default the batch_size member variable is used.
Returns:
  • y: array of shape [n_samples, n_classes]. The predicted probabilities for each class.

tf.contrib.learn.TensorFlowRNNRegressor.restore(cls, path, config=None)

Restores model from give path.

Args:
  • path: Path to the checkpoints and other model information.
  • config: RunConfig object that controls the configurations of the session, e.g. num_cores, gpu_memory_fraction, etc. This is allowed to be reconfigured.
Returns:

Estimator, object of the subclass of TensorFlowEstimator.

Raises:
  • ValueError: if path does not contain a model definition.

tf.contrib.learn.TensorFlowRNNRegressor.save(path)

Saves checkpoints and graph to given path.

Args:
  • path: Folder to save model to.

tf.contrib.learn.TensorFlowRNNRegressor.set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The former have parameters of the form <component>__<parameter> so that it's possible to update each component of a nested object.

Args:
  • **params: Parameters.
Returns:

self

Raises:
  • ValueError: If params contain invalid names.

tf.contrib.learn.TensorFlowRNNRegressor.weights_

Returns weights of the rnn layer.