TensorFlow -如何在科学符号中抑制打印?



如何抑制TensorFlow科学符号打印?我使用TensorFlow 2.6.
示例:

import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "1"
import tensorflow as tf
x = tf.constant([0.0001, 0.0002, 0.0003], dtype=tf.float32)
print(x)

示例输出:

tf.Tensor([1.e-04 2.e-04 3.e-04], shape=(3,), dtype=float32)

宁愿:

tf.Tensor([0.0001, 0.0002, 0.0003], shape=(3,), dtype=float32)

我意识到我可以添加np.set_printoptions(suppress=True)行,然后在打印时转换为numpy:

import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "1"
import tensorflow as tf
import numpy as np
np.set_printoptions(suppress=True)
x = tf.constant([0.0001, 0.0002, 0.0003], dtype=tf.float32)
print(x.numpy())

但如果可能的话,我更喜欢在TensorFlow中直接抑制科学符号的选项。

您可以使用tf.print(),它将指定的输入打印到所需的输出流或日志级别。

import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "1"
import tensorflow as tf
x = tf.constant([0.0001, 0.0002, 0.0003], dtype=tf.float32)
tf.print(x)

输出:

[0.0001 0.0002 0.0003]

最新更新