constant(value,dtype=None,shape=None,name='Const')
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 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:
```python
# 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.]]
```
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.
Returns:
A Constant Tensor.
上面是官方文档解释:
创建一个常数张量;由此产生的张量的类型是dtype指定的类型;value可以是常量,也可以是list等,如果是list,list的长度必须小于或等于shape的维度;如果list的长度小于shape(如果指定了此参数)的元素数,则list中的最后一项用来填充shape剩余的条目。
shape是可选的,如果存在,则指定张量结果,如果不存在,则用value的值。如果没有指定dtype,则tensorflow自己推断value的值。
例如:tf.constant([1,10],shape=[1,2,3])输出结果是[[[1,10,10],[10,10,10]]],第一个维度有一项,第二个维度有两项,第三个维度有3项;结果张量里开头第一项是value(如果value是list)的第一项填充,剩余的都是10(也就是list[1,10]里面的最后一项填充进去的。)
如果没有指定shape,则输出的结果张量就是value。
水滴石穿,一点一点积累