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")
| 163 | |
| 164 | @tf_export("constant", v1=[]) |
| 165 | def constant(value, dtype=None, shape=None, name="Const"): |
| 166 | """Creates a constant tensor. |
| 167 | |
| 168 | The resulting tensor is populated with values of type `dtype`, as |
| 169 | specified by arguments `value` and (optionally) `shape` (see examples |
| 170 | below). |
| 171 | |
| 172 | The argument `value` can be a constant value, or a list of values of type |
| 173 | `dtype`. If `value` is a list, then the length of the list must be less |
| 174 | than or equal to the number of elements implied by the `shape` argument (if |
| 175 | specified). In the case where the list length is less than the number of |
| 176 | elements specified by `shape`, the last element in the list will be used |
| 177 | to fill the remaining entries. |
| 178 | |
| 179 | The argument `shape` is optional. If present, it specifies the dimensions of |
| 180 | the resulting tensor. If not present, the shape of `value` is used. |
| 181 | |
| 182 | If the argument `dtype` is not specified, then the type is inferred from |
| 183 | the type of `value`. |
| 184 | |
| 185 | For example: |
| 186 | |
| 187 | ```python |
| 188 | # Constant 1-D Tensor populated with value list. |
| 189 | tensor = tf.constant([1, 2, 3, 4, 5, 6]) => [1 2 3 4 5 6] |
| 190 | |
| 191 | # Constant 1-D Tensor populated with value list. |
| 192 | tensor = tf.constant([1, 2, 3, 4, 5, 6], shape=(2,3)) |
| 193 | => [[1 2 3], [4 5 6]] |
| 194 | |
| 195 | # Constant 2-D tensor populated with scalar value -1. |
| 196 | tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.] |
| 197 | [-1. -1. -1.]] |
| 198 | ``` |
| 199 | |
| 200 | `tf.constant` differs from `tf.fill` in a few ways: |
| 201 | |
| 202 | * `tf.constant` supports arbitrary constants, not just uniform scalar |
| 203 | Tensors like `tf.fill`. |
| 204 | * `tf.constant` creates a `Const` node in the computation graph with the |
| 205 | exact value at graph construction time. On the other hand, `tf.fill` |
| 206 | creates an Op in the graph that is expanded at runtime. |
| 207 | * Because `tf.constant` only embeds constant values in the graph, it does |
| 208 | not support dynamic shapes based on other runtime Tensors, whereas |
| 209 | `tf.fill` does. |
| 210 | |
| 211 | Args: |
| 212 | value: A constant value (or list) of output type `dtype`. |
| 213 | |
| 214 | dtype: The type of the elements of the resulting tensor. |
| 215 | |
| 216 | shape: Optional dimensions of resulting tensor. |
| 217 | |
| 218 | name: Optional name for the tensor. |
| 219 | |
| 220 | Returns: |
| 221 | A Constant Tensor. |
| 222 |