tf.initializers.constant
Stay organized with collections
Save and categorize content based on your preferences.
Initializer that generates tensors with constant values.
Inherits From: Initializer
tf.initializers.constant(
value=0, dtype=tf.dtypes.float32, verify_shape=False
)
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
.
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.
|
dtype
|
Default data type, used if no dtype argument is provided when
calling the initializer.
|
verify_shape
|
Boolean that enables verification of the shape of value . If
True , the initializer will throw an error if the shape of value is not
compatible with the shape of the initialized tensor.
|
Raises |
TypeError
|
If the input value is not one of the expected types.
|
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=">>>">print('fitting shape:')</code>
<code class="devsite-terminal" data-terminal-prefix=">>>">with tf.compat.v1.Session():</code>
<code class="devsite-terminal" data-terminal-prefix=">>>"> x = tf.compat.v1.get_variable('x', shape=[2, 4], initializer=init)</code>
<code class="devsite-terminal" data-terminal-prefix=">>>"> x.initializer.run()</code>
<code class="devsite-terminal" data-terminal-prefix=">>>"> 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=">>>">print('larger shape:')</code>
<code class="devsite-terminal" data-terminal-prefix=">>>">with tf.compat.v1.Session():</code>
<code class="devsite-terminal" data-terminal-prefix=">>>"> x = tf.compat.v1.get_variable('x', shape=[3, 4], initializer=init)</code>
<code class="devsite-terminal" data-terminal-prefix=">>>"> x.initializer.run()</code>
<code class="devsite-terminal" data-terminal-prefix=">>>"> 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=">>>">print('smaller shape:')</code>
<code class="devsite-terminal" data-terminal-prefix=">>>">with tf.compat.v1.Session():</code>
<code class="devsite-terminal" data-terminal-prefix=">>>"> x = tf.compat.v1.get_variable('x', 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
<pre class="devsite-click-to-copy prettyprint lang-py">
<code class="devsite-terminal" data-terminal-prefix=">>>">print('shape verification:')</code>
<code class="devsite-terminal" data-terminal-prefix=">>>">init_verify = tf.compat.v1.constant_initializer(value,</code>
<code class="no-select nocode"> verify_shape=True)</code>
<code class="devsite-terminal" data-terminal-prefix=">>>">with tf.compat.v1.Session():</code>
<code class="devsite-terminal" data-terminal-prefix=">>>"> x = tf.compat.v1.get_variable('x', shape=[3, 4],</code>
<code class="no-select nocode"> initializer=init_verify)</code>
<code class="no-select nocode"> </code>
</pre>
TypeError: Expected Tensor's shape: (3, 4), got (8,).
Methods
from_config
View source
@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)
Args |
config
|
A Python dictionary. It will typically be the output of
get_config .
|
Returns |
An Initializer instance.
|
get_config
View source
get_config()
Returns the configuration of the initializer as a JSON-serializable dict.
Returns |
A JSON-serializable Python dict.
|
__call__
View source
__call__(
shape, dtype=None, partition_info=None, verify_shape=None
)
Returns a tensor object initialized as specified by the initializer.
Args |
shape
|
Shape of the tensor.
|
dtype
|
Optional dtype of the tensor. If not provided use the initializer
dtype.
|
partition_info
|
Optional information about the possible partitioning of a
tensor.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.initializers.constant\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/ops/init_ops.py#L142-L251) |\n\nInitializer that generates tensors with constant values.\n\nInherits From: [`Initializer`](../../tf/keras/initializers/Initializer)\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.constant_initializer`](/api_docs/python/tf/constant_initializer), [`tf.keras.initializers.Constant`](/api_docs/python/tf/keras/initializers/Constant), [`tf.keras.initializers.constant`](/api_docs/python/tf/keras/initializers/Constant)\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.constant_initializer`](/api_docs/python/tf/compat/v1/keras/initializers/Constant), [`tf.compat.v1.initializers.constant`](/api_docs/python/tf/compat/v1/keras/initializers/Constant), [`tf.compat.v1.keras.initializers.Constant`](/api_docs/python/tf/compat/v1/keras/initializers/Constant), [`tf.compat.v1.keras.initializers.constant`](/api_docs/python/tf/compat/v1/keras/initializers/Constant)\n\n\u003cbr /\u003e\n\n tf.initializers.constant(\n value=0, dtype=tf.dtypes.float32, verify_shape=False\n )\n\nThe resulting tensor is populated with values of type `dtype`, as\nspecified by arguments `value` following the desired `shape` of the\nnew tensor (see examples below).\n\nThe argument `value` can be a constant value, or a list of values of type\n`dtype`. If `value` is a list, then the length of the list must be less\nthan or equal to the number of elements implied by the desired shape of the\ntensor. In the case where the total number of elements in `value` is less\nthan the number of elements required by the tensor shape, the last element\nin `value` will be used to fill the remaining entries. If the total number of\nelements in `value` is greater than the number of elements required by the\ntensor shape, the initializer will raise a `ValueError`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `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. |\n| `dtype` | Default data type, used if no `dtype` argument is provided when calling the initializer. |\n| `verify_shape` | Boolean that enables verification of the shape of `value`. If `True`, the initializer will throw an error if the shape of `value` is not compatible with the shape of the initialized tensor. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-------------|--------------------------------------------------------|\n| `TypeError` | If the input `value` is not one of the expected types. |\n\n\u003cbr /\u003e\n\n#### Examples:\n\nThe following example can be rewritten using a numpy.ndarray instead\nof the `value` list, even reshaped, as shown in the two commented lines\nbelow the `value` list initialization. \n\n import numpy as np\n import tensorflow as tf\n \n \n\u003e \u003e \u003e value = \\[0, 1, 2, 3, 4, 5, 6, 7\\]\n\u003e \u003e \u003e\n\u003e \u003e \u003e value = np.array(value)\n\u003e \u003e \u003e =======================\n\u003e \u003e \u003e\n\u003e \u003e \u003e value = value.reshape(\\[2, 4\\])\n\u003e \u003e \u003e ===============================\n\u003e \u003e \u003e\n\u003e \u003e \u003e init = tf.compat.v1.constant_initializer(value)\n\u003e\n\u003e \u003cpre class=\"devsite-click-to-copy prettyprint lang-py\"\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003eprint('fitting shape:')\u003c/code\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003ewith tf.compat.v1.Session():\u003c/code\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003e x = tf.compat.v1.get_variable('x', shape=[2, 4], initializer=init)\u003c/code\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003e x.initializer.run()\u003c/code\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003e print(x.eval())\u003c/code\u003e\n\u003e \u003ccode class=\"no-select nocode\"\u003e \u003c/code\u003e\n\u003e \u003c/pre\u003e\n\u003e\n\u003e\n\u003e fitting shape:\n\u003e [[ 0. 1. 2. 3.]\n\u003e [ 4. 5. 6. 7.]]\n\u003e\n\u003e \u003cpre class=\"devsite-click-to-copy prettyprint lang-py\"\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003eprint('larger shape:')\u003c/code\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003ewith tf.compat.v1.Session():\u003c/code\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003e x = tf.compat.v1.get_variable('x', shape=[3, 4], initializer=init)\u003c/code\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003e x.initializer.run()\u003c/code\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003e print(x.eval())\u003c/code\u003e\n\u003e \u003ccode class=\"no-select nocode\"\u003e \u003c/code\u003e\n\u003e \u003c/pre\u003e\n\u003e\n\u003e\n\u003e larger shape:\n\u003e [[ 0. 1. 2. 3.]\n\u003e [ 4. 5. 6. 7.]\n\u003e [ 7. 7. 7. 7.]]\n\u003e\n\u003e \u003cpre class=\"devsite-click-to-copy prettyprint lang-py\"\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003eprint('smaller shape:')\u003c/code\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003ewith tf.compat.v1.Session():\u003c/code\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003e x = tf.compat.v1.get_variable('x', shape=[2, 3], initializer=init)\u003c/code\u003e\n\u003e \u003ccode class=\"no-select nocode\"\u003e \u003c/code\u003e\n\u003e \u003c/pre\u003e\n\u003e\n\u003e\n\u003e ValueError: Too many elements provided. Needed at most 6, but received 8\n\u003e\n\u003e \u003cpre class=\"devsite-click-to-copy prettyprint lang-py\"\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003eprint('shape verification:')\u003c/code\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003einit_verify = tf.compat.v1.constant_initializer(value,\u003c/code\u003e\n\u003e \u003ccode class=\"no-select nocode\"\u003e verify_shape=True)\u003c/code\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003ewith tf.compat.v1.Session():\u003c/code\u003e\n\u003e \u003ccode class=\"devsite-terminal\" data-terminal-prefix=\">>>\"\u003e x = tf.compat.v1.get_variable('x', shape=[3, 4],\u003c/code\u003e\n\u003e \u003ccode class=\"no-select nocode\"\u003e initializer=init_verify)\u003c/code\u003e\n\u003e \u003ccode class=\"no-select nocode\"\u003e \u003c/code\u003e\n\u003e \u003c/pre\u003e\n\u003e\n\u003e\n\u003e TypeError: Expected Tensor's shape: (3, 4), got (8,).\n\nMethods\n-------\n\n### `from_config`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/ops/init_ops.py#L78-L97) \n\n @classmethod\n from_config(\n config\n )\n\nInstantiates an initializer from a configuration dictionary.\n\n#### Example:\n\n initializer = RandomUniform(-1, 1)\n config = initializer.get_config()\n initializer = RandomUniform.from_config(config)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|----------|-----------------------------------------------------------------------|\n| `config` | A Python dictionary. It will typically be the output of `get_config`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| An Initializer instance. ||\n\n\u003cbr /\u003e\n\n### `get_config`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/ops/init_ops.py#L246-L251) \n\n get_config()\n\nReturns the configuration of the initializer as a JSON-serializable dict.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| A JSON-serializable Python dict. ||\n\n\u003cbr /\u003e\n\n### `__call__`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/ops/init_ops.py#L238-L244) \n\n __call__(\n shape, dtype=None, partition_info=None, verify_shape=None\n )\n\nReturns a tensor object initialized as specified by the initializer.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|------------------|--------------------------------------------------------------------------|\n| `shape` | Shape of the tensor. |\n| `dtype` | Optional dtype of the tensor. If not provided use the initializer dtype. |\n| `partition_info` | Optional information about the possible partitioning of a tensor. |\n\n\u003cbr /\u003e"]]