Initializer that generates tensors with constant values.

Used in the notebooks

Used in the tutorials

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.


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.))
<tf.Variable ... shape=(3,) ... numpy=array([2., 2., 2.], dtype=float32)>