创建一个常数张量。
用法
tf.compat.v1.constant(
value, dtype=None, shape=None, name='Const', verify_shape=False
)参数
-
value输出类型为dtype的常量值(或列表)。 -
dtype结果张量的元素类型。 -
shape结果张量的可选尺寸。 -
name张量的可选名称。 -
verify_shape用于验证值形状的布尔值。
返回
- 一个常数张量。
抛出
-
TypeError如果形状指定不正确或不受支持。
生成的张量由 dtype 类型的值填充,由参数 value 和(可选)shape 指定(参见下面的示例)。
参数 value 可以是常量值,也可以是类型为 dtype 的值的列表。如果 value 是一个列表,则列表的长度必须小于或等于 shape 参数(如果指定)隐含的元素数。在列表长度小于 shape 指定的元素数量的情况下,列表中的最后一个元素将用于填充剩余的条目。
参数shape 是可选的。如果存在,它指定结果张量的维度。如果不存在,则使用 value 的形状。
如果未指定参数 dtype ,则从 value 的类型推断类型。
例如:
# 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 在以下几个方面与 tf.fill 不同:
tf.constant支持任意常量,而不仅仅是像tf.fill这样的统一标量张量。tf.constant在计算图中创建一个Const节点,该节点在图构建时具有精确值。另一方面,tf.fill在运行时扩展的图中创建一个 Op。- 因为
tf.constant仅在图中嵌入常量值,它不支持基于其他运行时张量的动态形状,而tf.fill支持。
相关用法
- Python tf.compat.v1.confusion_matrix用法及代码示例
- Python tf.compat.v1.convert_to_tensor用法及代码示例
- Python tf.compat.v1.cond用法及代码示例
- Python tf.compat.v1.count_nonzero用法及代码示例
- Python tf.compat.v1.case用法及代码示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代码示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代码示例
- Python tf.compat.v1.data.TFRecordDataset.interleave用法及代码示例
- Python tf.compat.v1.distributions.Bernoulli.cross_entropy用法及代码示例
- Python tf.compat.v1.Variable.eval用法及代码示例
- Python tf.compat.v1.train.FtrlOptimizer.compute_gradients用法及代码示例
- Python tf.compat.v1.layers.conv3d用法及代码示例
- Python tf.compat.v1.strings.length用法及代码示例
- Python tf.compat.v1.data.Dataset.snapshot用法及代码示例
- Python tf.compat.v1.data.experimental.SqlDataset.reduce用法及代码示例
- Python tf.compat.v1.feature_column.categorical_column_with_vocabulary_file用法及代码示例
- Python tf.compat.v1.data.TextLineDataset.from_tensors用法及代码示例
- Python tf.compat.v1.variable_scope用法及代码示例
- Python tf.compat.v1.data.experimental.SqlDataset.as_numpy_iterator用法及代码示例
- Python tf.compat.v1.distributions.Bernoulli.covariance用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.compat.v1.constant。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
