tf.compat.v1.constant
Stay organized with collections
Save and categorize content based on your preferences.
Creates a constant tensor.
tf.compat.v1.constant(
value, dtype=None, shape=None, name='Const', verify_shape=False
)
The resulting tensor is populated with values of type dtype
, as
specified by arguments value
and (optionally) shape
(see examples
below).
The argument value
can be a constant value, or a list of values of type
dtype
. If value
is a list, then the length of the list must be less
than or equal to the number of elements implied by the shape
argument (if
specified). In the case where the list length is less than the number of
elements specified by shape
, the last element in the list will be used
to fill the remaining entries.
The argument shape
is optional. If present, it specifies the dimensions of
the resulting tensor. If not present, the shape of value
is used.
If the argument dtype
is not specified, then the type is inferred from
the type of value
.
For example:
# Constant 1-D Tensor populated with value list.
tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]
# Constant 2-D tensor populated with scalar value -1.
tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.]
[-1. -1. -1.]]
tf.constant
differs from tf.fill
in a few ways:
tf.constant
supports arbitrary constants, not just uniform scalar
Tensors like tf.fill
.
tf.constant
creates a Const
node in the computation graph with the
exact value at graph construction time. On the other hand, tf.fill
creates an Op in the graph that is expanded at runtime.
- Because
tf.constant
only embeds constant values in the graph, it does
not support dynamic shapes based on other runtime Tensors, whereas
tf.fill
does.
Args |
value
|
A constant value (or list) of output type dtype .
|
dtype
|
The type of the elements of the resulting tensor.
|
shape
|
Optional dimensions of resulting tensor.
|
name
|
Optional name for the tensor.
|
verify_shape
|
Boolean that enables verification of a shape of values.
|
Returns |
A Constant Tensor.
|
Raises |
TypeError
|
if shape is incorrectly specified or unsupported.
|
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 2023-10-06 UTC.
[null,null,["Last updated 2023-10-06 UTC."],[],[],null,["# tf.compat.v1.constant\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.14.0/tensorflow/python/framework/constant_op.py#L105-L167) |\n\nCreates a constant tensor. \n\n tf.compat.v1.constant(\n value, dtype=None, shape=None, name='Const', verify_shape=False\n )\n\nThe resulting tensor is populated with values of type `dtype`, as\nspecified by arguments `value` and (optionally) `shape` (see examples\nbelow).\n\nThe argument `value` can be a constant value, or a list of values of type\n`dtype`. If `value` is a list, then the length of the list must be less\nthan or equal to the number of elements implied by the `shape` argument (if\nspecified). In the case where the list length is less than the number of\nelements specified by `shape`, the last element in the list will be used\nto fill the remaining entries.\n\nThe argument `shape` is optional. If present, it specifies the dimensions of\nthe resulting tensor. If not present, the shape of `value` is used.\n\nIf the argument `dtype` is not specified, then the type is inferred from\nthe type of `value`.\n\n#### For example:\n\n # Constant 1-D Tensor populated with value list.\n tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) =\u003e [1 2 3 4 5 6 7]\n\n # Constant 2-D tensor populated with scalar value -1.\n tensor = tf.constant(-1.0, shape=[2, 3]) =\u003e [[-1. -1. -1.]\n [-1. -1. -1.]]\n\n[`tf.constant`](../../../tf/constant) differs from [`tf.fill`](../../../tf/fill) in a few ways:\n\n- [`tf.constant`](../../../tf/constant) supports arbitrary constants, not just uniform scalar Tensors like [`tf.fill`](../../../tf/fill).\n- [`tf.constant`](../../../tf/constant) creates a `Const` node in the computation graph with the exact value at graph construction time. On the other hand, [`tf.fill`](../../../tf/fill) creates an Op in the graph that is expanded at runtime.\n- Because [`tf.constant`](../../../tf/constant) only embeds constant values in the graph, it does not support dynamic shapes based on other runtime Tensors, whereas [`tf.fill`](../../../tf/fill) does.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------------|---------------------------------------------------------|\n| `value` | A constant value (or list) of output type `dtype`. |\n| `dtype` | The type of the elements of the resulting tensor. |\n| `shape` | Optional dimensions of resulting tensor. |\n| `name` | Optional name for the tensor. |\n| `verify_shape` | Boolean that enables verification of a shape of values. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A Constant Tensor. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-------------|---------------------------------------------------|\n| `TypeError` | if shape is incorrectly specified or unsupported. |\n\n\u003cbr /\u003e"]]