Creates a constant tensor. 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 len
(
value, dtype=None, shape=None, name="Const", verify_shape=False)
| 98 | |
| 99 | @tf_export(v1=["constant"]) |
| 100 | def constant_v1( |
| 101 | value, dtype=None, shape=None, name="Const", verify_shape=False): |
| 102 | """Creates a constant tensor. |
| 103 | |
| 104 | The resulting tensor is populated with values of type `dtype`, as |
| 105 | specified by arguments `value` and (optionally) `shape` (see examples |
| 106 | below). |
| 107 | |
| 108 | The argument `value` can be a constant value, or a list of values of type |
| 109 | `dtype`. If `value` is a list, then the length of the list must be less |
| 110 | than or equal to the number of elements implied by the `shape` argument (if |
| 111 | specified). In the case where the list length is less than the number of |
| 112 | elements specified by `shape`, the last element in the list will be used |
| 113 | to fill the remaining entries. |
| 114 | |
| 115 | The argument `shape` is optional. If present, it specifies the dimensions of |
| 116 | the resulting tensor. If not present, the shape of `value` is used. |
| 117 | |
| 118 | If the argument `dtype` is not specified, then the type is inferred from |
| 119 | the type of `value`. |
| 120 | |
| 121 | For example: |
| 122 | |
| 123 | ```python |
| 124 | # Constant 1-D Tensor populated with value list. |
| 125 | tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7] |
| 126 | |
| 127 | # Constant 2-D tensor populated with scalar value -1. |
| 128 | tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.] |
| 129 | [-1. -1. -1.]] |
| 130 | ``` |
| 131 | |
| 132 | `tf.constant` differs from `tf.fill` in a few ways: |
| 133 | |
| 134 | * `tf.constant` supports arbitrary constants, not just uniform scalar |
| 135 | Tensors like `tf.fill`. |
| 136 | * `tf.constant` creates a `Const` node in the computation graph with the |
| 137 | exact value at graph construction time. On the other hand, `tf.fill` |
| 138 | creates an Op in the graph that is expanded at runtime. |
| 139 | * Because `tf.constant` only embeds constant values in the graph, it does |
| 140 | not support dynamic shapes based on other runtime Tensors, whereas |
| 141 | `tf.fill` does. |
| 142 | |
| 143 | Args: |
| 144 | value: A constant value (or list) of output type `dtype`. |
| 145 | |
| 146 | dtype: The type of the elements of the resulting tensor. |
| 147 | |
| 148 | shape: Optional dimensions of resulting tensor. |
| 149 | |
| 150 | name: Optional name for the tensor. |
| 151 | |
| 152 | verify_shape: Boolean that enables verification of a shape of values. |
| 153 | |
| 154 | Returns: |
| 155 | A Constant Tensor. |
| 156 | |
| 157 | Raises: |
nothing calls this directly
no test coverage detected