View source on GitHub |
Initializer that generates tensors with constant values.
Inherits From: Initializer
tf.constant_initializer(
value=0
)
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.
|
Raises | |
---|---|
TypeError
|
If the input value is not one of the expected types.
|
Methods
from_config
@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
get_config()
Returns the configuration of the initializer as a JSON-serializable dict.
Returns | |
---|---|
A JSON-serializable Python dict. |
__call__
__call__(
shape, dtype=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 the dtype 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. |