tf.fill
Stay organized with collections
Save and categorize content based on your preferences.
Creates a tensor filled with a scalar value.
tf.fill(
dims, value, name=None, layout=None
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
See also tf.ones
, tf.zeros
, tf.one_hot
, tf.eye
.
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.
Args |
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 .
|
layout
|
Optional, tf.experimental.dtensor.Layout . If provided, the result
is a DTensor with the
provided layout.
|
Returns |
A tf.Tensor with shape dims and the same dtype as value .
|
Raises |
InvalidArgumentError
|
dims contains negative entries.
|
NotFoundError
|
dims contains non-integer entries.
|
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.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.fill\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/array_ops.py#L204-L250) |\n\nCreates a tensor filled with a scalar value.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.fill`](https://www.tensorflow.org/api_docs/python/tf/fill)\n\n\u003cbr /\u003e\n\n tf.fill(\n dims, value, name=None, layout=None\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Ragged tensors](https://www.tensorflow.org/guide/ragged_tensor) - [tf.data: Build TensorFlow input pipelines](https://www.tensorflow.org/guide/data) - [Subword tokenizers](https://www.tensorflow.org/text/guide/subwords_tokenizer) - [Unicode strings](https://www.tensorflow.org/text/guide/unicode) | - [Scalable model compression](https://www.tensorflow.org/tutorials/optimization/compression) - [Multiple changepoint detection and Bayesian model selection](https://www.tensorflow.org/probability/examples/Multiple_changepoint_detection_and_Bayesian_model_selection) - [Graph-based Neural Structured Learning in TFX](https://www.tensorflow.org/tfx/tutorials/tfx/neural_structured_learning) - [Graph regularization for sentiment classification using synthesized graphs](https://www.tensorflow.org/neural_structured_learning/tutorials/graph_keras_lstm_imdb) - [Bayesian Gaussian Mixture Model and Hamiltonian MCMC](https://www.tensorflow.org/probability/examples/Bayesian_Gaussian_Mixture_Model) |\n\nSee also [`tf.ones`](../tf/ones), [`tf.zeros`](../tf/zeros), [`tf.one_hot`](../tf/one_hot), [`tf.eye`](../tf/eye).\n\nThis operation creates a tensor of shape `dims` and fills it with `value`.\n\n#### For example:\n\n tf.fill([2, 3], 9)\n \u003ctf.Tensor: shape=(2, 3), dtype=int32, numpy=\n array([[9, 9, 9],\n [9, 9, 9]], dtype=int32)\u003e\n\n[`tf.fill`](../tf/fill) evaluates at graph runtime and supports dynamic shapes based on\nother runtime `tf.Tensors`, unlike [`tf.constant(value, shape=dims)`](../tf/constant), which\nembeds the value as a `Const` node.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `dims` | A 1-D sequence of non-negative numbers. Represents the shape of the output [`tf.Tensor`](../tf/Tensor). Entries should be of type: `int32`, `int64`. |\n| `value` | A value to fill the returned [`tf.Tensor`](../tf/Tensor). |\n| `name` | Optional string. The name of the output [`tf.Tensor`](../tf/Tensor). |\n| `layout` | Optional, [`tf.experimental.dtensor.Layout`](../tf/experimental/dtensor/Layout). If provided, the result is a [DTensor](https://www.tensorflow.org/guide/dtensor_overview) with the provided layout. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A [`tf.Tensor`](../tf/Tensor) with shape `dims` and the same dtype as `value`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|------------------------|--------------------------------------|\n| `InvalidArgumentError` | `dims` contains negative entries. |\n| `NotFoundError` | `dims` contains non-integer entries. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nnumpy compatibility\n-------------------\n\n\u003cbr /\u003e\n\nSimilar to `np.full`. In `numpy`, more parameters are supported. Passing a\nnumber argument as the shape (`np.full(5, value)`) is valid in `numpy` for\nspecifying a 1-D shaped result, while TensorFlow does not support this syntax.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e"]]