Help protect the Great Barrier Reef with TensorFlow on Kaggle

tf.constant_initializer

Initializer that generates tensors with constant values.

Inherits From: Initializer

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.

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.

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.

value = [0, 1, 2, 3, 4, 5, 6, 7]
init = tf.compat.v1.constant_initializer(value)
# Fitting shape
with tf.compat.v1.Session():
x = tf.compat.v1.get_variable('x', shape=[2, 4], initializer=init)
x.initializer.run()
print(x.eval())
[[0. 1. 2. 3.]
[4. 5. 6. 7.]]
# Larger shape
with tf.compat.v1.Session():
y = tf.compat.v1.get_variable('y', shape=[3, 4], initializer=init)
y.initializer.run()
print(y.eval())
[[0. 1. 2. 3.]
[4. 5. 6. 7.]
[7. 7. 7. 7.]]
# Smaller shape
with tf.compat.v1.Session():
z = tf.compat.v1.get_variable('z', shape=[2, 3], initializer=init)
Traceback (most recent call last):

ValueError: Too many elements provided. Needed at most 6, but received 8

Methods

from_config

View source

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

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

Returns
A JSON-serializable Python dict.

__call__

View source

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.

[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }]
[{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]