介绍
打印信息,相当于python的print()
语法
tf.print(
*inputs,
**kwargs
)
参数
- *inputs:要打印的信息
- output_stream:输出流,日志记录级别或要打印到的文件。默认为sys.stderr,但sys.stdout,absl.logging.info,abssl.logging .warning和absl.looging,错误也受支持。要打印到文件,请传递以“ file://”开头的字符串,后跟文件路径,例如“ file:///tmp/foo.out”
- summarize:summarize每个张量中的第一个和最后一个元素按张量递归打印。如果为None,则为每个张量打印每个维度的前3个元素和后3个元素。如果设置为-1,它将打印每个张量的所有元素
- sep:用于分隔输入的字符串。默认为“”。同python的
print() - end:结束字符,附加在打印字符串的末尾。默认为换行符。同python的
print()
例子
一般在静态图模式下运行,可以打印tensor的值
@tf.function
def f():
tensor = tf.constant(10)
print(tensor)
tf.print(tensor, output_stream=sys.stderr)
f()
执行结果如下:
Tensor("Const:0", shape=(), dtype=int32)
10
函数由@tf.function修饰,以静态图模式运行,此时print()只能打印tensor的名字、形状、类型,不知道值
关于tensorflow2的eager模式和静态图见:
https://www.malaoshi.top/show_1EF4aMnxYdnu.html
关于@tf.function见:
https://www.malaoshi.top/show_1EF5HFCDp8fP.html