为什么同一个操作在TensorFlow中有不同的返回类型?

2024-04-18 18:50:31 发布

您现在位置:Python中文网/ 问答频道 /正文

在TensorFlow中,我有两个常量节点:

>> node1 = tf.constant(3.0, tf.float32)
>> node2 = tf.constant(4.0)

当我在Jupyter Notebook中运行此图时,以下两个会话运行的返回类型不同:

^{pr2}$

对于此操作,没有输出单元格:

^{3}$

但当我以:

>> print(sess.run(node1)), print(sess.run(node2)) #prints 3.0 and on the next line, it prints 4.0, and it ALSO prints a tuple (None, None) in the output cell.

有人能解释一下这些退货的类型吗? Jupyter


Tags: andtherunnone类型tftensorflowit
2条回答

你正在创建一个元组

打印功能和其他功能一样。它不返回值,所以它返回None,通常不显示。在

但是,逗号运算符创建一个元组,jupyter显示返回值。在

写4,4会得到(4,4)。在

什么都不写,都不会导致你所看到的。在

如果您只想执行两个操作,请将它们分别放在各自的行中—逗号不适用于此。在

第二行有两个用逗号分隔的print语句。print函数返回None,Python REPL(read eval print loop)将忽略单个None。但是,逗号创建了一个元组,即使它只包含None值,它也不会被忽略。在

如果要在一行上运行多个语句,请使用;而不是,

print("foo"); print("bar")

相关问题 更多 >