tf.constant_initializer
Stay organized with collections
Save and categorize content based on your preferences.
Initializer that generates tensors with constant values.
tf.constant_initializer(
value=0, support_partition=False
)
Used in the notebooks
Initializers allow you to pre-specify an initialization strategy, encoded in
the Initializer object, without knowing the shape and dtype of the variable
being initialized.
tf.constant_initializer
returns an object which when called returns a tensor
populated with the value
specified in the constructor. This value
must be
convertible to the requested dtype
.
The argument value
can be a scalar constant value, or a list of
values. Scalars broadcast to whichever shape is requested from the
initializer.
If value
is a list, then the length of the list must be equal to the number
of elements implied by the desired shape of the tensor. If the total number of
elements in value
is not equal to the number of elements required by the
tensor shape, the initializer will raise a TypeError
.
Examples:
def make_variables(k, initializer):
return (tf.Variable(initializer(shape=[k], dtype=tf.float32)),
tf.Variable(initializer(shape=[k, k], dtype=tf.float32)))
v1, v2 = make_variables(3, tf.constant_initializer(2.))
v1
<tf.Variable ... shape=(3,) ... numpy=array([2., 2., 2.], dtype=float32)>
v2
<tf.Variable ... shape=(3, 3) ... numpy=
array([[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.]], dtype=float32)>
make_variables(4, tf.random_uniform_initializer(minval=-1., maxval=1.))
(<tf.Variable...shape=(4,) dtype=float32...>, <tf.Variable...shape=(4, 4) ...
value = [0, 1, 2, 3, 4, 5, 6, 7]
init = tf.constant_initializer(value)
# Fitting shape
tf.Variable(init(shape=[2, 4], dtype=tf.float32))
<tf.Variable ...
array([[0., 1., 2., 3.],
[4., 5., 6., 7.]], dtype=float32)>
# Larger shape
tf.Variable(init(shape=[3, 4], dtype=tf.float32))
Traceback (most recent call last):
TypeError: ...value has 8 elements, shape is (3, 4) with 12 elements...
# Smaller shape
tf.Variable(init(shape=[2, 3], dtype=tf.float32))
Traceback (most recent call last):
TypeError: ...value has 8 elements, shape is (2, 3) with 6 elements...
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.
|
support_partition
|
If true, the initizer supports passing partition
offset and partition shape arguments to variable creators. This is
particularly useful when initializing sharded variables where each
variable shard is initialized to a slice of constant initializer.
|
Raises |
TypeError
|
If the input value is not one of the expected types.
|
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, **kwargs
)
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 the dtype of the
tensor created will be the type of the inital value.
|
**kwargs
|
Additional keyword arguments.
|
Raises |
TypeError
|
If the initializer cannot create a tensor of the requested
dtype.
|
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. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.constant_initializer\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/init_ops_v2.py#L204-L299) |\n\nInitializer that generates tensors with constant values. \n\n tf.constant_initializer(\n value=0, support_partition=False\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|---------------------------------------------------------------------------------------------------------------------------|\n| - [TFP Probabilistic Layers: Regression](https://www.tensorflow.org/probability/examples/Probabilistic_Layers_Regression) |\n\nInitializers allow you to pre-specify an initialization strategy, encoded in\nthe Initializer object, without knowing the shape and dtype of the variable\nbeing initialized.\n\n[`tf.constant_initializer`](../tf/constant_initializer) returns an object which when called returns a tensor\npopulated with the `value` specified in the constructor. This `value` must be\nconvertible to the requested `dtype`.\n\nThe argument `value` can be a scalar constant value, or a list of\nvalues. Scalars broadcast to whichever shape is requested from the\ninitializer.\n\nIf `value` is a list, then the length of the list must be equal to the number\nof elements implied by the desired shape of the tensor. If the total number of\nelements in `value` is not equal to the number of elements required by the\ntensor shape, the initializer will raise a `TypeError`.\n\n#### Examples:\n\n def make_variables(k, initializer):\n return (tf.Variable(initializer(shape=[k], dtype=tf.float32)),\n tf.Variable(initializer(shape=[k, k], dtype=tf.float32)))\n v1, v2 = make_variables(3, tf.constant_initializer(2.))\n v1\n \u003ctf.Variable ... shape=(3,) ... numpy=array([2., 2., 2.], dtype=float32)\u003e\n v2\n \u003ctf.Variable ... shape=(3, 3) ... numpy=\n array([[2., 2., 2.],\n [2., 2., 2.],\n [2., 2., 2.]], dtype=float32)\u003e\n make_variables(4, tf.random_uniform_initializer(minval=-1., maxval=1.))\n (\u003ctf.Variable...shape=(4,) dtype=float32...\u003e, \u003ctf.Variable...shape=(4, 4) ...\n\n value = [0, 1, 2, 3, 4, 5, 6, 7]\n init = tf.constant_initializer(value)\n # Fitting shape\n tf.Variable(init(shape=[2, 4], dtype=tf.float32))\n \u003ctf.Variable ...\n array([[0., 1., 2., 3.],\n [4., 5., 6., 7.]], dtype=float32)\u003e\n # Larger shape\n tf.Variable(init(shape=[3, 4], dtype=tf.float32))\n Traceback (most recent call last):\n\n TypeError: ...value has 8 elements, shape is (3, 4) with 12 elements...\n # Smaller shape\n tf.Variable(init(shape=[2, 3], dtype=tf.float32))\n Traceback (most recent call last):\n\n TypeError: ...value has 8 elements, shape is (2, 3) with 6 elements...\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| `support_partition` | If true, the initizer supports passing partition offset and partition shape arguments to variable creators. This is particularly useful when initializing sharded variables where each variable shard is initialized to a slice of constant initializer. |\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\nMethods\n-------\n\n### `from_config`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/init_ops_v2.py#L76-L96) \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/v2.16.1/tensorflow/python/ops/init_ops_v2.py#L298-L299) \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/v2.16.1/tensorflow/python/ops/init_ops_v2.py#L280-L296) \n\n __call__(\n shape, dtype=None, **kwargs\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 the dtype of the tensor created will be the type of the inital value. |\n| `**kwargs` | Additional keyword arguments. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|-------------|-------------------------------------------------------------------|\n| `TypeError` | If the initializer cannot create a tensor of the requested dtype. |\n\n\u003cbr /\u003e"]]