tf.pad
Stay organized with collections
Save and categorize content based on your preferences.
Pads a tensor.
tf.pad(
tensor, paddings, mode='CONSTANT', constant_values=0, name=None
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
This operation pads a tensor
according to the paddings
you specify.
paddings
is an integer tensor with shape [n, 2]
, where n is the rank of
tensor
. For each dimension D of input
, paddings[D, 0]
indicates how
many values to add before the contents of tensor
in that dimension, and
paddings[D, 1]
indicates how many values to add after the contents of
tensor
in that dimension. If mode
is "REFLECT" then both paddings[D, 0]
and paddings[D, 1]
must be no greater than tensor.dim_size(D) - 1
. If
mode
is "SYMMETRIC" then both paddings[D, 0]
and paddings[D, 1]
must be
no greater than tensor.dim_size(D)
.
The padded size of each dimension D of the output is:
paddings[D, 0] + tensor.dim_size(D) + paddings[D, 1]
For example:
t = tf.constant([[1, 2, 3], [4, 5, 6]])
paddings = tf.constant([[1, 1,], [2, 2]])
# 'constant_values' is 0.
# rank of 't' is 2.
tf.pad(t, paddings, "CONSTANT") # [[0, 0, 0, 0, 0, 0, 0],
# [0, 0, 1, 2, 3, 0, 0],
# [0, 0, 4, 5, 6, 0, 0],
# [0, 0, 0, 0, 0, 0, 0]]
tf.pad(t, paddings, "REFLECT") # [[6, 5, 4, 5, 6, 5, 4],
# [3, 2, 1, 2, 3, 2, 1],
# [6, 5, 4, 5, 6, 5, 4],
# [3, 2, 1, 2, 3, 2, 1]]
tf.pad(t, paddings, "SYMMETRIC") # [[2, 1, 1, 2, 3, 3, 2],
# [2, 1, 1, 2, 3, 3, 2],
# [5, 4, 4, 5, 6, 6, 5],
# [5, 4, 4, 5, 6, 6, 5]]
Args |
tensor
|
A Tensor .
|
paddings
|
A Tensor of type int32 .
|
mode
|
One of "CONSTANT", "REFLECT", or "SYMMETRIC" (case-insensitive)
|
constant_values
|
In "CONSTANT" mode, the scalar pad value to use. Must be
same type as tensor .
|
name
|
A name for the operation (optional).
|
Returns |
A Tensor . Has the same type as tensor .
|
Raises |
ValueError
|
When mode is not one of "CONSTANT", "REFLECT", or "SYMMETRIC".
|
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.pad\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#L3163-L3219) |\n\nPads a tensor. \n\n tf.pad(\n tensor, paddings, mode='CONSTANT', constant_values=0, name=None\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|--------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Advanced automatic differentiation](https://www.tensorflow.org/guide/advanced_autodiff) | - [Client-efficient large-model federated learning via \\`federated_select\\` and sparse aggregation](https://www.tensorflow.org/federated/tutorials/sparse_federated_learning) - [A Tour of TensorFlow Probability](https://www.tensorflow.org/probability/examples/A_Tour_of_TensorFlow_Probability) - [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) |\n\nThis operation pads a `tensor` according to the `paddings` you specify.\n`paddings` is an integer tensor with shape `[n, 2]`, where n is the rank of\n`tensor`. For each dimension D of `input`, `paddings[D, 0]` indicates how\nmany values to add before the contents of `tensor` in that dimension, and\n`paddings[D, 1]` indicates how many values to add after the contents of\n`tensor` in that dimension. If `mode` is \"REFLECT\" then both `paddings[D, 0]`\nand `paddings[D, 1]` must be no greater than `tensor.dim_size(D) - 1`. If\n`mode` is \"SYMMETRIC\" then both `paddings[D, 0]` and `paddings[D, 1]` must be\nno greater than `tensor.dim_size(D)`.\n\nThe padded size of each dimension D of the output is:\n\n`paddings[D, 0] + tensor.dim_size(D) + paddings[D, 1]`\n\n#### For example:\n\n t = tf.constant([[1, 2, 3], [4, 5, 6]])\n paddings = tf.constant([[1, 1,], [2, 2]])\n # 'constant_values' is 0.\n # rank of 't' is 2.\n tf.pad(t, paddings, \"CONSTANT\") # [[0, 0, 0, 0, 0, 0, 0],\n # [0, 0, 1, 2, 3, 0, 0],\n # [0, 0, 4, 5, 6, 0, 0],\n # [0, 0, 0, 0, 0, 0, 0]]\n\n tf.pad(t, paddings, \"REFLECT\") # [[6, 5, 4, 5, 6, 5, 4],\n # [3, 2, 1, 2, 3, 2, 1],\n # [6, 5, 4, 5, 6, 5, 4],\n # [3, 2, 1, 2, 3, 2, 1]]\n\n tf.pad(t, paddings, \"SYMMETRIC\") # [[2, 1, 1, 2, 3, 3, 2],\n # [2, 1, 1, 2, 3, 3, 2],\n # [5, 4, 4, 5, 6, 6, 5],\n # [5, 4, 4, 5, 6, 6, 5]]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------------|---------------------------------------------------------------------------------|\n| `tensor` | A `Tensor`. |\n| `paddings` | A `Tensor` of type `int32`. |\n| `mode` | One of \"CONSTANT\", \"REFLECT\", or \"SYMMETRIC\" (case-insensitive) |\n| `constant_values` | In \"CONSTANT\" mode, the scalar pad value to use. Must be same type as `tensor`. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor`. Has the same type as `tensor`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|----------------------------------------------------------------|\n| `ValueError` | When mode is not one of \"CONSTANT\", \"REFLECT\", or \"SYMMETRIC\". |\n\n\u003cbr /\u003e"]]