tf.ones_like

Creates a tensor of all ones that has the same shape as the input.

Used in the notebooks

Used in the guide Used in the tutorials

See also tf.ones.

Given a single tensor (tensor), this operation returns a tensor of the same type and shape as tensor with all elements set to 1. Optionally, you can use dtype to specify a new type for the returned tensor.

For example:

tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
tf.ones_like(tensor)
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
  array([[1, 1, 1],
         [1, 1, 1]], dtype=int32)>

Note that the layout of the input tensor is not preserved if the op is used inside tf.function. To obtain a tensor with the same layout as the input, chain the returned value to a dtensor.relayout_like.

input A Tensor.
dtype A type for the returned Tensor. Must be float16, float32, float64, int8, uint8, int16, uint16, int32, int64, complex64, complex128, bool or string.
name A name for the operation (optional).
layout Optional, tf.experimental.dtensor.Layout. If provided, the result is a DTensor with the provided layout.

A Tensor with all elements set to one.