Tensorflow:tf.constant(2)和2有什么区别

时间:2018-06-22 03:07:17

标签: tensorflow

我刚刚开始自己​​学习Tensorflow。我有一个问题,以下代码有什么区别?

x = tf.add(3,5)

a = tf.constant(3)
b = tf.constant(5)
x = tf.add(a,b)

print(sess.run(x))都返回8。那么使用tf.constant有什么区别?

1 个答案:

答案 0 :(得分:2)

没什么区别。

每次张量流操作需要Tensor输入但收到其他输入时,它都会尝试用tf.convert_to_tensor进行转换,如果成功,则正常进行该输出。

在常量为2的情况下,还可以包含np.arrayliststuple s或几乎任何容器或(格式良好的)容器层次结构,tf.convert_to_tensor将创建Const操作,就像您通过调用tf.constant手动创建的操作一样。

tf.constant仍然很有用,因为它除其他功能外,还允许您在图形中命名常量并控制其数据类型(分别使用namedtype参数)。

相关问题