Class constant_initializer
Initializer that generates tensors with constant values. InitializerInherits From:
Aliases:
- Class
tf.compat.v2.constant_initializer
- Class
tf.compat.v2.initializers.Constant
- Class
tf.compat.v2.initializers.constant
- Class
tf.compat.v2.keras.initializers.Constant
- Class
tf.compat.v2.keras.initializers.constant
- Class
tf.initializers.Constant
- Class
tf.initializers.constant
- Class
tf.keras.initializers.Constant
- Class
tf.keras.initializers.constant
Used in the guide:
K
er
as
o
ve
rv
ie
w`` The resulting tensor is populated withvalue
s of typedtype
, as specified by argumentsvalue
following the desiredshape
of the new tensor (see examples below). The argumentvalue
can be a constantvalue
, or a list ofvalue
s of typedtype
. Ifvalue
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 invalue
is less than the number of elements required by the tensor shape, the last element invalue
will be used to fill the remaining entries. If the total number of elements invalue
is greater than the number of elements required by the tensor shape, the initializer will raise aValueError
.
Args:
value
: A Python scalar, list or tuple ofvalue
s, or a N-dimensional numpy array. All elements of the initialized variable will be set to the correspondingvalue
in thevalue
argument.
Raises:
TypeError
: If the inputvalue
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
<code></code>
<code class="no-select nocode"> >>> value = [0, 1, 2, 3, 4, 5, 6, 7]</code>
<code class="no-select nocode"> >>> # value = np.array(value)</code>
<code class="no-select nocode"> >>> # value = value.reshape([2, 4])</code>
<code class="no-select nocode"> >>> init = tf.compat.v1.constant_initializer(value)</code>
<code class="no-select nocode"></code>
<code class="no-select nocode">> </code>
<code class="no-select nocode"> >>> print('fitting shape:')</code>
<code class="no-select nocode"> >>> with tf.compat.v1.Session():</code>
<code class="no-select nocode"> >>> x = tf.compat.v1.get_variable('x', shape=[2, 4], initializer=init)</code>
<code class="no-select nocode"> >>> x.initializer.run()</code>
<code class="no-select nocode"> >>> print(x.eval())</code>
<code class="no-select nocode"></code>
<code class="no-select nocode">> </code>
<code class="no-select nocode">> fitting shape:</code>
<code class="no-select nocode">> [[ 0. 1. 2. 3.]</code>
<code class="no-select nocode">> [ 4. 5. 6. 7.]]</code>
<code class="no-select nocode">> </code>
<code class="no-select nocode"> >>> print('larger shape:')</code>
<code class="no-select nocode"> >>> with tf.compat.v1.Session():</code>
<code class="no-select nocode"> >>> x = tf.compat.v1.get_variable('x', shape=[3, 4], initializer=init)</code>
<code class="no-select nocode"> >>> x.initializer.run()</code>
<code class="no-select nocode"> >>> print(x.eval())</code>
<code class="no-select nocode"></code>
<code class="no-select nocode">> </code>
<code class="no-select nocode">> larger shape:</code>
<code class="no-select nocode">> [[ 0. 1. 2. 3.]</code>
<code class="no-select nocode">> [ 4. 5. 6. 7.]</code>
<code class="no-select nocode">> [ 7. 7. 7. 7.]]</code>
<code class="no-select nocode">> </code>
<code class="no-select nocode"> >>> print('smaller shape:')</code>
<code class="no-select nocode"> >>> with tf.compat.v1.Session():</code>
<code class="no-select nocode"> >>> x = tf.compat.v1.get_variable('x', shape=[2, 3], initializer=init)</code>
<code class="no-select nocode"></code>
<code class="no-select nocode">> </code>
<code class="no-select nocode">> ValueError: Too many elements provided. Needed at most 6, but received 8</code>
<code class="no-select nocode"></code>
<code class="no-select nocode"><h2 id="__init__"><code>__init__</code></h2></code>
<code class="no-select nocode"></code>
<code class="no-select nocode"><a target="_blank" href="https://github.com/tensorflow/tensorflow/blob/r2.0/tensorflow/python/ops/init_ops_v2.py#L190-L195">View source</a></code>
<code class="no-select nocode"></code>
<code class="no-select nocode"></code> python
<strong>init</strong>(value=0)
Initialize self. See help(type(self)) for accurate signature.
Methods
call
__call__(
shape,
dtype=None
)
Returns a tensor object initialized as specified by the initializer.
Args:
shape
: Shape of the tensor.dtype
: Optionaldtype
of the tensor. If not provided thedtype
of the tensor created will be the type of the inital value.
Raises:
TypeError
: If the initializer cannot create a tensor of the requested dtype.
from_config
from_config(
cls,
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 ofget_config
.
Returns:
An Initializer instance.
get_config
get_config()
Returns the configuration of the initializer as a JSON-serializable dict.
Returns:
A JSON-serializable Python dict.