Path: tensorflow/tensorflow/python/ops/array_ops.py
@tf_export("pad", v1=[])
@dispatch.add_dispatch_support
def pad_v2(tensor, paddings, mode="CONSTANT", constant_values=0, name=None):
return pad(tensor, paddings, mode, name, constant_values)
@tf_export(v1=["pad"])
@dispatch.add_dispatch_support
def pad(tensor, paddings, mode="CONSTANT", name=None, constant_values=0):
mode = mode.upper()
if mode == "CONSTANT":
# TODO(rjryan): Once the forward compatibility period (3 weeks) have passed
# remove the "Pad" fallback here.
if (not tensor_util.is_tf_type(constant_values) and
np.ndim(constant_values) == 0 and
constant_values == np.zeros_like(constant_values)):
result = gen_array_ops.pad(tensor, paddings, name=name)
else:
result = gen_array_ops.pad_v2(
tensor, paddings, constant_values, name=name)
elif mode == "REFLECT":
result = gen_array_ops.mirror_pad(
tensor, paddings, mode="REFLECT", name=name)
elif mode == "SYMMETRIC":
result = gen_array_ops.mirror_pad(
tensor, paddings, mode="SYMMETRIC", name=name)
else:
raise ValueError("Value of argument `mode` expected to be "
"""one of "CONSTANT", "REFLECT", or "SYMMETRIC". """
f"Received `mode` = {mode}")
# Restore shape information where possible.
if not context.executing_eagerly():
paddings_constant = _get_paddings_constant(paddings)
input_shape = (
tensor_shape.TensorShape(tensor.shape)
if isinstance(tensor, ops.Tensor) else result.op.inputs[0].shape)
if (input_shape.ndims is not None and
not result.shape.is_fully_defined() and paddings_constant is not None):
new_shape = []
for padding, dim in zip(paddings_constant, input_shape.as_list()):
if padding is None or dim is None or any((x is None for x in padding)):
new_shape.append(None)
else:
new_shape.append(sum(padding) + dim)
result.set_shape(new_shape)
return result
array_ops:
Path: tensorflow/tensorflow/python/ops/array_ops.py