# tf.contrib.training.HParams

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L310-L739) |

Class to hold a set of hyperparameters as name-value pairs.

    tf.contrib.training.HParams(
        hparam_def=None, model_structure=None, **kwargs
    )

A `HParams` object holds hyperparameters used to build and train a model,
such as the number of hidden units in a neural net layer or the learning rate
to use when training.

You first create a `HParams` object by specifying the names and values of the
hyperparameters.

To make them easily accessible the parameter names are added as direct
attributes of the class. A typical usage is as follows:

    # Create a HParams object specifying names and values of the model
    # hyperparameters:
    hparams = HParams(learning_rate=0.1, num_hidden_units=100)

    # The hyperparameter are available as attributes of the HParams object:
    hparams.learning_rate ==> 0.1
    hparams.num_hidden_units ==> 100

Hyperparameters have type, which is inferred from the type of their value
passed at construction type. The currently supported types are: integer,
float, boolean, string, and list of integer, float, boolean, or string.

You can override hyperparameter values by calling the
[`parse()`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/contrib/training/HParams#HParams.parse) method, passing a string of comma separated
`name=value` pairs. This is intended to make it possible to override
any hyperparameter values from a single command-line flag to which
the user passes 'hyper-param=value' pairs. It avoids having to define
one flag for each hyperparameter.

The syntax expected for each value depends on the type of the parameter.
See `parse()` for a description of the syntax.

#### Example:

    # Define a command line flag to pass name=value pairs.
    # For example using argparse:
    import argparse
    parser = argparse.ArgumentParser(description='Train my model.')
    parser.add_argument('--hparams', type=str,
                        help='Comma separated list of "name=value" pairs.')
    args = parser.parse_args()
    ...
    def my_program():
      # Create a HParams object specifying the names and values of the
      # model hyperparameters:
      hparams = tf.contrib.training.HParams(
          learning_rate=0.1,
          num_hidden_units=100,
          activations=['relu', 'tanh'])

      # Override hyperparameters values by parsing the command line
      hparams.parse(args.hparams)

      # If the user passed `--hparams=learning_rate=0.3` on the command line
      # then 'hparams' has the following attributes:
      hparams.learning_rate ==> 0.3
      hparams.num_hidden_units ==> 100
      hparams.activations ==> ['relu', 'tanh']

      # If the hyperparameters are in json format use parse_json:
      hparams.parse_json('{"learning_rate": 0.3, "activations": "relu"}')

<br />

<br />

<br />

| ## Args ||
|---|---|
| `hparam_def` | Serialized hyperparameters, encoded as a hparam_pb2.HParamDef protocol buffer. If provided, this object is initialized by deserializing hparam_def. Otherwise \*\*kwargs is used. |
| `model_structure` | An instance of ModelStructure, defining the feature crosses to be used in the Trial. |
| `**kwargs` | Key-value pairs where the key is the hyperparameter name and the value is the value for the parameter. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | If both `hparam_def` and initialization values are provided, or if one of the arguments is invalid. |

<br />

## Methods

### `add_hparam`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L473-L496)

    add_hparam(
        name, value
    )

Adds {name, value} pair to hyperparameters.

<br />

<br />

<br />

| Args ||
|---|---|
| `name` | Name of the hyperparameter. |
| `value` | Value of the hyperparameter. Can be one of the following types: int, float, string, int list, float list, or string list. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | if one of the arguments is invalid. |

<br />

### `del_hparam`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L525-L535)

    del_hparam(
        name
    )

Removes the hyperparameter with key 'name'.

Does nothing if it isn't present.

<br />

<br />

<br />

| Args ||
|---|---|
| `name` | Name of the hyperparameter. |

<br />

### `from_proto`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L737-L739)

    @staticmethod
    from_proto(
        hparam_def, import_scope=None
    )

### `get`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L635-L660)

    get(
        key, default=None
    )

Returns the value of `key` if it exists, else `default`.

### `get_model_structure`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L586-L587)

    get_model_structure()

### `override_from_dict`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L561-L576)

    override_from_dict(
        values_dict
    )

Override existing hyperparameter values, parsing new values from a dictionary.

<br />

<br />

<br />

| Args ||
|---|---|
| `values_dict` | Dictionary of name:value pairs. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The `HParams` instance. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `KeyError` | If a hyperparameter in `values_dict` doesn't exist. |
| `ValueError` | If `values_dict` cannot be parsed. |

<br />

### `parse`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L537-L559)

    parse(
        values
    )

Override existing hyperparameter values, parsing new values from a string.

See parse_values for more detail on the allowed format for values.

<br />

<br />

<br />

| Args ||
|---|---|
| `values` | String. Comma separated list of `name=value` pairs where 'value' must follow the syntax described above. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The `HParams` instance. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If `values` cannot be parsed or a hyperparameter in `values` doesn't exist. |

<br />

### `parse_json`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L610-L624)

    parse_json(
        values_json
    )

Override existing hyperparameter values, parsing new values from a json object.

<br />

<br />

<br />

| Args ||
|---|---|
| `values_json` | String containing a json object of name:value pairs. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The `HParams` instance. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `KeyError` | If a hyperparameter in `values_json` doesn't exist. |
| `ValueError` | If `values_json` cannot be parsed. |

<br />

### `set_from_map`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L578-L581)

    set_from_map(
        values_map
    )

DEPRECATED. Use override_from_dict. (deprecated)

> [!WARNING]
> **Warning:** THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use `override_from_dict`.

### `set_hparam`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L498-L523)

    set_hparam(
        name, value
    )

Set the value of an existing hyperparameter.

This function verifies that the type of the value matches the type of the
existing hyperparameter.

<br />

<br />

<br />

| Args ||
|---|---|
| `name` | Name of the hyperparameter. |
| `value` | New value of the hyperparameter. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `KeyError` | If the hyperparameter doesn't exist. |
| `ValueError` | If there is a type mismatch. |

<br />

### `set_model_structure`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L583-L584)

    set_model_structure(
        model_structure
    )

### `to_json`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L589-L608)

    to_json(
        indent=None, separators=None, sort_keys=False
    )

Serializes the hyperparameters into JSON.

<br />

<br />

<br />

| Args ||
|---|---|
| `indent` | If a non-negative integer, JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0, or negative, will only insert newlines. `None` (the default) selects the most compact representation. |
| `separators` | Optional `(item_separator, key_separator)` tuple. Default is `(', ', ': ')`. |
| `sort_keys` | If `True`, the output dictionaries will be sorted by key. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A JSON string. ||

<br />

### `to_proto`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L708-L735)

    to_proto(
        export_scope=None
    )

Converts a `HParams` object to a `HParamDef` protocol buffer.

<br />

<br />

<br />

| Args ||
|---|---|
| `export_scope` | Optional `string`. Name scope to remove. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A `HParamDef` protocol buffer. ||

<br />

### `values`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L626-L633)

    values()

Return the hyperparameter values as a Python dictionary.

<br />

<br />

<br />

| Returns ||
|---|---|
| A dictionary with hyperparameter names as keys. The values are the hyperparameter values. ||

<br />

### `__contains__`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/training/python/training/hparam.py#L662-L663)

    __contains__(
        key
    )