【问题标题】:Explicit vs implicit type definition in TensorFlowTensorFlow 中的显式与隐式类型定义
【发布时间】:2019-05-09 19:37:31
【问题描述】:

我刚刚开始学习 TensorFlow。引用自documentation

让我们构建一个简单的计算图。最基本的操作是常数。构建操作的 Python 函数将张量值作为输入。结果操作不接受任何输入。运行时,它输出传递给构造函数的值。我们可以创建两个浮点常量 a 和 b,如下所示:

a = tf.constant(3.0, dtype=tf.float32)
b = tf.constant(4.0) # also tf.float32 implicitly
total = a + b
print(a)
print(b)
print(total)

第二个常量被隐式类型为 float32。这是基于第一个常量的显式类型吗?这是否意味着第一个 dtype 是必需的? tf.constant documentation 暗示它没有:

如果未指定参数 dtype,则从 value 的类型推断类型。

但是没有必要显式键入上面的 3.0 常量。

我只是想对此进行澄清,因为就像我说的那样,我才刚刚开始。

【问题讨论】:

    标签: python tensorflow types


    【解决方案1】:

    但是没有必要显式键入 3.0 常量 以上。

    完全正确。

    a = tf.constant(3.0, dtype=tf.float32)
    

    相当于:

    a = tf.constant(3.0)
    

    文档只是演示了不同的重载。如果我们想要不同的数值精度(甚至只是为了提高可读性),我们可能会选择显式提供类型,但如果您想要 TF 推断的默认数据类型,那么完全没有必要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 2013-08-02
      • 2011-07-01
      • 1970-01-01
      相关资源
      最近更新 更多