深入浅出TensorFlow2函数——tf.constant

这篇具有很好参考价值的文章主要介绍了深入浅出TensorFlow2函数——tf.constant。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

分类目录:《深入浅出TensorFlow2函数》总目录
相关文章:
· 深入浅出TensorFlow2函数——tf.constant
· 深入浅出TensorFlow2函数——tf.Tensor
· 深入浅出Pytorch函数——torch.tensor
· 深入浅出Pytorch函数——torch.as_tensor
· 深入浅出PaddlePaddle函数——paddle.to_tensor


语法
tf.constant(
    value, dtype=None, shape=None, name='Const'
)
参数
  • value:输出张量的常数值。
  • dtype:输出张量元素的类型。
  • shape:[可选] 张量的形状。
  • name:[可选] 张量的名称。
返回值

一个常数张量。文章来源地址https://www.toymoban.com/news/detail-514354.html

实例
# 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]])>
函数实现
@tf_export("constant", v1=[])
def constant(value, dtype=None, shape=None, name="Const"):
  """Creates a constant tensor from a tensor-like object.
  Note: All eager `tf.Tensor` values are immutable (in contrast to
  `tf.Variable`). There is nothing especially _constant_ about the value
  returned from `tf.constant`. This function is not fundamentally different from
  `tf.convert_to_tensor`. The name `tf.constant` comes from the `value` being
  embedded in a `Const` node in the `tf.Graph`. `tf.constant` is useful
  for asserting that the value can be embedded that way.
  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 create tensors on the current device. Inputs which are
  already tensors maintain their placements unchanged.
  Related Ops:
  * `tf.convert_to_tensor` is similar but:
    * It has no `shape` argument.
    * Symbolic tensors are allowed to pass through.
    >>> with tf.compat.v1.Graph().as_default():
    ...   i = tf.compat.v1.placeholder(shape=[None, None], dtype=tf.float32)
    ...   t = tf.convert_to_tensor(i)
  * `tf.fill`: differs in a few ways:
    *   `tf.constant` supports arbitrary constants, not just uniform scalar
        Tensors like `tf.fill`.
    *   `tf.fill` creates an Op in the graph that is expanded at runtime, so it
        can efficiently represent large tensors.
    *   Since `tf.fill` does not embed the value, it can produce dynamically
        sized outputs.
  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.
  Raises:
    TypeError: if shape is incorrectly specified or unsupported.
    ValueError: if called on a symbolic tensor.
  """
  return _constant_impl(value, dtype, shape, name, verify_shape=False,
                        allow_broadcast=True)

到了这里,关于深入浅出TensorFlow2函数——tf.constant的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 深入浅出TensorFlow2函数——tf.Tensor

    分类目录:《深入浅出TensorFlow2函数》总目录 相关文章: · 深入浅出TensorFlow2函数——tf.Tensor · 深入浅出Pytorch函数——torch.Tensor · 深入浅出PaddlePaddle函数——paddle.Tensor 一个 tf.Tensor 表示一个多维数组。在编写TensorFlow程序时,被操作和传递的主要对象就是 tf.Tensor 。 tf.Tens

    2024年02月17日
    浏览(17)
  • 深入浅出TensorFlow2函数——tf.math.exp

    分类目录:《深入浅出TensorFlow2函数》总目录 相关文章: · 深入浅出TensorFlow2函数——tf.exp · 深入浅出TensorFlow2函数——tf.math.exp · 深入浅出Pytorch函数——torch.exp · 深入浅出PaddlePaddle函数——paddle.exp 按元素计算 x x x 的指数 y = e x y=e^x y = e x 。 语法 参数 x :[ tf.Tensor ] 必须

    2024年02月12日
    浏览(8)
  • 深入浅出TensorFlow2函数——tf.random.poisson

    分类目录:《深入浅出TensorFlow2函数》总目录 绘制 shape 个来自每个给定泊松分布的样本。 语法 参数 shape :输出张量的形状,为一个一维整数张量或Python数组。 lam :样本提供描述泊松分布的参数。 dtype :输出的浮点类型: float16 、 bfloat16 、 float32 、 float64 ,默认为 float3

    2024年02月11日
    浏览(27)
  • 深入浅出TensorFlow2函数——tf.random.uniform

    分类目录:《深入浅出TensorFlow2函数》总目录 绘制 shape 个来自每个给定均匀分布的样本。 语法 参数 shape :输出张量的形状,为一个一维整数张量或Python数组。 minval :要生成的随机值范围的下限(含),默认值为 0 。 minval :要生成的随机值范围的上限(不含),默认值为 1 。

    2024年02月11日
    浏览(7)
  • 深入浅出TensorFlow2函数——tf.random.normal

    分类目录:《深入浅出TensorFlow2函数》总目录 语法 参数 shape :输出张量的形状,为一个一维整数张量或Python数组。 mean 正态分布的平均值。类型为张量或 dtype ,可与 stddev 一起广播。 stddev :正态分布的标准偏差。类型为张量或 dtype ,可与 mean 一起广播。 dtype :输出的浮点

    2024年02月12日
    浏览(10)
  • 深入浅出TensorFlow2函数——tf.reduce_sum

    分类目录:《深入浅出TensorFlow2函数》总目录 相关文章: · 深入浅出TensorFlow2函数——tf.reduce_sum · 深入浅出TensorFlow2函数——tf.math.reduce_sum · 深入浅出Pytorch函数——torch.sum · 深入浅出PaddlePaddle函数——paddle.sum 计算张量各维度上元素的总和。 语法 参数 input_tensor :[ Tensor

    2024年02月10日
    浏览(14)
  • 深入浅出TensorFlow2函数——tf.math.reduce_sum

    分类目录:《深入浅出TensorFlow2函数》总目录 相关文章: · 深入浅出TensorFlow2函数——tf.reduce_sum · 深入浅出TensorFlow2函数——tf.math.reduce_sum · 深入浅出Pytorch函数——torch.sum · 深入浅出PaddlePaddle函数——paddle.sum 计算张量各维度上元素的总和。 语法 参数 input_tensor :[ Tensor

    2024年02月12日
    浏览(8)
  • 深入浅出TensorFlow2函数——tf.data.Dataset.from_tensor_slices

    分类目录:《深入浅出TensorFlow2函数》总目录 返回一个数据集,其元素是给定张量的切片。给定的张量沿着它们的第一维度进行切片。此操作保留输入张量的结构,删除每个张量的第一个维度并将其用作数据集维度。所有输入张量在其第一维度上必须具有相同的大小。 语法

    2024年02月12日
    浏览(12)
  • 深入浅出Pytorch函数——torch.nn.init.constant_

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.nn.init.calculate_gain · 深入浅出Pytorch函数——torch.nn.init.uniform_ · 深入浅出Pytorch函数——torch.nn.init.normal_ · 深入浅出Pytorch函数——torch.nn.init.constant_ · 深入浅出Pytorch函数——torch.nn.init.ones_ ·

    2024年02月12日
    浏览(42)
  • 深入浅出C语言—【函数】上

    深入浅出C语言—【函数】上

       目录 1.函数的概念 2.C语言函数的分类 2.1 库函数 2.1.1 strcpy库函数举例学习方式 2.1.2 库函数扩展知识 2.2 自定义函数 2.2.1求两个整数中的较大值 3. 函数的参数 3.1 实际参数(实参) 3.2 形式参数(形参) 4. 函数的调用 4.1 传值调用 4.2 传址调用 老铁们,网址自取,记得一键

    2024年02月07日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包