tensorflow1.11.0 tf.constant()函数(创建一个常数张量)
tf.constant
tf.constant(value,dtype=None,shape=None,name='Const',verify_shape=False )Defined in tensorflow/python/framework/constant_op.py.
See the guide: Constants, Sequences, and Random Values > Constant Value Tensors
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).
生成的張量將填充dtype類型的值,該值由參數value和(可選)shape指定(請參見下面的示例)。
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.
參數值可以是常量值,也可以是dtype類型的值列表。
如果value是一個列表,則列表的長度必須小于或等于shape參數所隱含的元素數(如果指定)。
如果列表長度小于shape指定的元素數,則列表中的最后一個元素將用于填充其余條目。
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.
如果未指定參數dtype,則從值的類型推斷類型。
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與tf.fill在以下方面有所不同:
-
tf.constant supports arbitrary constants, not just uniform scalar Tensors like tf.fill.
tf.constant支持任意常量,而不僅僅是像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.
tf.constant在計算圖中使用圖構建時的確切值創建一個Const節點。 另一方面,tf.fill在圖形中創建一個Op,并在運行時對其進行擴展。 -
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.
因為tf.constant僅將常數值嵌入到圖形中,所以它不支持基于其他運行時Tensor的動態形狀,而tf.fill則支持。
Args:
-
value: A constant value (or list) of output type dtype.
輸出類型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.
TypeError:如果形狀指定不正確或不受支持。
20200212
tf.constant()返回的結果是一個Tensor對象
import tensorflow as tf x = tf.constant([[1., 1.], [2., 2.]]) print(type(x))結果:<class ‘tensorflow.python.framework.ops.Tensor’>
總結
以上是生活随笔為你收集整理的tensorflow1.11.0 tf.constant()函数(创建一个常数张量)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python tensorflow tf
- 下一篇: tensorflow 什么是tensor