तिथि को रक्षित करें! Google I / O 18-20 मई को

# tf.constant

Creates a constant tensor from a tensor-like object.

### Used in the notebooks

If the argument `dtype` is not specified, then the type is inferred from the type of `value`.

````# Constant 1-D Tensor from a python list.`
`tf.constant([1, 2, 3, 4, 5, 6])`
`<tf.Tensor: shape=(6,), dtype=int32,`
`    numpy=array([1, 2, 3, 4, 5, 6], dtype=int32)>`
`# Or a numpy array`
`a = np.array([[1, 2, 3], [4, 5, 6]])`
`tf.constant(a)`
`<tf.Tensor: shape=(2, 3), dtype=int64, numpy=`
`  array([[1, 2, 3],`
`         [4, 5, 6]])>`
```

If `dtype` is specified the resulting tensor values are cast to the requested `dtype`.

````tf.constant([1, 2, 3, 4, 5, 6], dtype=tf.float64)`
`<tf.Tensor: shape=(6,), dtype=float64,`
`    numpy=array([1., 2., 3., 4., 5., 6.])>`
```

If `shape` is set, the `value` is reshaped to match. Scalars are expanded to fill the `shape`:

````tf.constant(0, shape=(2, 3))`
`  <tf.Tensor: shape=(2, 3), dtype=int32, numpy=`
`  array([[0, 0, 0],`
`         [0, 0, 0]], dtype=int32)>`
`tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])`
`<tf.Tensor: shape=(2, 3), dtype=int32, numpy=`
`  array([[1, 2, 3],`
`         [4, 5, 6]], dtype=int32)>`
```

`tf.constant` has no effect if an eager Tensor is passed as the `value`, it even transmits gradients:

````v = tf.Variable([0.0])`
`with tf.GradientTape() as g:`
`    loss = tf.constant(v + v)`
`g.gradient(loss, v).numpy()`
`array([2.], dtype=float32)`
```

But, since `tf.constant` embeds the value in the `tf.Graph` this fails for symbolic tensors:

````with tf.compat.v1.Graph().as_default():`
`  i = tf.compat.v1.placeholder(shape=[None, None], dtype=tf.float32)`
`  t = tf.constant(i)`
`Traceback (most recent call last):`

`TypeError: ...`
```

`tf.constant` will always create CPU (host) tensors. In order to create tensors on other devices, use `tf.identity`. (If the `value` is an eager Tensor, however, the tensor will be returned unmodified as mentioned above.)

```  `with tf.compat.v1.Graph().as_default():`
`  i = tf.compat.v1.placeholder(shape=[None, None], dtype=tf.float32)`
`  t = tf.convert_to_tensor(i)`
`  `
```

`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.

A Constant Tensor.

`TypeError` if shape is incorrectly specified or unsupported.
`ValueError` if called on a symbolic tensor.

[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }]
[{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]