tf.fill

TensorFlow 1 version View source on GitHub

Creates a tensor filled with a scalar value.

This operation creates a tensor of shape dims and fills it with value.

For example:

tf.fill([2, 3], 9)
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[9, 9, 9],
       [9, 9, 9]], dtype=int32)>

tf.fill evaluates at graph runtime and supports dynamic shapes based on other runtime tf.Tensors, unlike tf.constant(value, shape=dims), which embeds the value as a Const node.

dims A 1-D sequence of non-negative numbers. Represents the shape of the output tf.Tensor. Entries should be of type: int32, int64.
value A value to fill the returned tf.Tensor.
name Optional string. The name of the output tf.Tensor.

A tf.Tensor with shape dims and the same dtype as value.

InvalidArgumentError dims contains negative entries.
NotFoundError dims contains non-integer entries.

Numpy Compatibility

Similar to np.full. In numpy, more parameters are supported. Passing a number argument as the shape (np.full(5, value)) is valid in numpy for specifying a 1-D shaped result, while TensorFlow does not support this syntax.