# tf.constant_initializer

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/ops/init_ops_v2.py#L123-L215) |

Initializer that generates tensors with constant values.

Inherits From: [`Initializer`](https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/keras/initializers/Initializer)

#### View aliases


**Main aliases**

[`tf.initializers.Constant`](https://www.tensorflow.org/api_docs/python/tf/keras/initializers/Constant), [`tf.initializers.constant`](https://www.tensorflow.org/api_docs/python/tf/keras/initializers/Constant), [`tf.keras.initializers.Constant`](https://www.tensorflow.org/api_docs/python/tf/keras/initializers/Constant), [`tf.keras.initializers.constant`](https://www.tensorflow.org/api_docs/python/tf/keras/initializers/Constant)

<br />

    tf.constant_initializer(
        value=0
    )

The resulting tensor is populated with values of type `dtype`, as
specified by arguments `value` following the desired `shape` of the
new tensor (see examples below).

The argument `value` can be a constant value, or a list of values of type
`dtype`. If `value` is a list, then the length of the list must be less
than or equal to the number of elements implied by the desired shape of the
tensor. In the case where the total number of elements in `value` is less
than the number of elements required by the tensor shape, the last element
in `value` will be used to fill the remaining entries. If the total number of
elements in `value` is greater than the number of elements required by the
tensor shape, the initializer will raise a `ValueError`.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `value` | A Python scalar, list or tuple of values, or a N-dimensional numpy array. All elements of the initialized variable will be set to the corresponding value in the `value` argument. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `TypeError` | If the input `value` is not one of the expected types. |

<br />

#### Examples:

The following example can be rewritten using a numpy.ndarray instead
of the `value` list, even reshaped, as shown in the two commented lines
below the `value` list initialization.

      import numpy as np
      import tensorflow as tf
      
        
> > > value = \[0, 1, 2, 3, 4, 5, 6, 7\]
> > >
> > > # value = np.array(value)
> > >
> > > # value = value.reshape(\[2, 4\])
> > >
> > > init = tf.compat.v1.constant_initializer(value)
>
>     <pre class="devsite-click-to-copy prettyprint lang-py">
>       <code class="devsite-terminal" data-terminal-prefix="&gt;&gt;&gt;">print(&#x27;fitting shape:&#x27;)</code>
>       <code class="devsite-terminal" data-terminal-prefix="&gt;&gt;&gt;">with tf.compat.v1.Session():</code>
>       <code class="devsite-terminal" data-terminal-prefix="&gt;&gt;&gt;">  x = tf.compat.v1.get_variable(&#x27;x&#x27;, shape=[2, 4], initializer=init)</code>
>       <code class="devsite-terminal" data-terminal-prefix="&gt;&gt;&gt;">  x.initializer.run()</code>
>       <code class="devsite-terminal" data-terminal-prefix="&gt;&gt;&gt;">  print(x.eval())</code>
>       <code class="no-select nocode">  </code>
>     </pre>
>
>
>       fitting shape:
>       [[ 0.  1.  2.  3.]
>        [ 4.  5.  6.  7.]]
>
>     <pre class="devsite-click-to-copy prettyprint lang-py">
>       <code class="devsite-terminal" data-terminal-prefix="&gt;&gt;&gt;">print(&#x27;larger shape:&#x27;)</code>
>       <code class="devsite-terminal" data-terminal-prefix="&gt;&gt;&gt;">with tf.compat.v1.Session():</code>
>       <code class="devsite-terminal" data-terminal-prefix="&gt;&gt;&gt;">  x = tf.compat.v1.get_variable(&#x27;x&#x27;, shape=[3, 4], initializer=init)</code>
>       <code class="devsite-terminal" data-terminal-prefix="&gt;&gt;&gt;">  x.initializer.run()</code>
>       <code class="devsite-terminal" data-terminal-prefix="&gt;&gt;&gt;">  print(x.eval())</code>
>       <code class="no-select nocode">  </code>
>     </pre>
>
>
>       larger shape:
>       [[ 0.  1.  2.  3.]
>        [ 4.  5.  6.  7.]
>        [ 7.  7.  7.  7.]]
>
>     <pre class="devsite-click-to-copy prettyprint lang-py">
>       <code class="devsite-terminal" data-terminal-prefix="&gt;&gt;&gt;">print(&#x27;smaller shape:&#x27;)</code>
>       <code class="devsite-terminal" data-terminal-prefix="&gt;&gt;&gt;">with tf.compat.v1.Session():</code>
>       <code class="devsite-terminal" data-terminal-prefix="&gt;&gt;&gt;">  x = tf.compat.v1.get_variable(&#x27;x&#x27;, shape=[2, 3], initializer=init)</code>
>       <code class="no-select nocode">  </code>
>     </pre>
>
>
>       ValueError: Too many elements provided. Needed at most 6, but received 8

## Methods

### `from_config`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/ops/init_ops_v2.py#L69-L89)

    @classmethod
    from_config(
        config
    )

Instantiates an initializer from a configuration dictionary.

#### Example:

    initializer = RandomUniform(-1, 1)
    config = initializer.get_config()
    initializer = RandomUniform.from_config(config)

<br />

<br />

<br />

| Args ||
|---|---|
| `config` | A Python dictionary. It will typically be the output of `get_config`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An Initializer instance. ||

<br />

### `get_config`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/ops/init_ops_v2.py#L214-L215)

    get_config()

Returns the configuration of the initializer as a JSON-serializable dict.

<br />

<br />

<br />

| Returns ||
|---|---|
| A JSON-serializable Python dict. ||

<br />

### `__call__`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/ops/init_ops_v2.py#L197-L212)

    __call__(
        shape, dtype=None
    )

Returns a tensor object initialized as specified by the initializer.

<br />

<br />

<br />

| Args ||
|---|---|
| `shape` | Shape of the tensor. |
| `dtype` | Optional dtype of the tensor. If not provided the dtype of the tensor created will be the type of the inital value. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `TypeError` | If the initializer cannot create a tensor of the requested dtype. |

<br />