tf2.0.0正式版中如何打印值?

How to print the value in the official version of tf2.0.0?

我发现在tf2.0.0正式版中无法显示tensor的值。我应该怎么办?麻木的?评估?

print(tf.random.uniform((3, 3)))

print(tf.keras.layers.LayerNormalization()(tf.random.uniform((3, 3))))

结果:

Tensor("random_uniform:0", shape=(3, 3), dtype=float32)

Tensor("layer_normalization/batchnorm/add_1:0", shape=(3, 3), dtype=float32)

你确定你的TF版本吗?这是我对您的代码的结果:

import tensorflow as tf

def main():
    print("Version: ", tf.version.VERSION)
    print(tf.random.uniform((3, 3)))
    print(tf.keras.layers.LayerNormalization()(tf.random.uniform((3, 3))))

if __name__ == '__main__':
    main()
Version:  2.0.0
tf.Tensor(
[[0.4394927  0.44767535 0.02136886]
 [0.7118287  0.65160227 0.47469318]
 [0.7066748  0.130373   0.09051967]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[ 0.8090544  -1.4032681   0.5942137 ]
 [-1.3625047   0.38342142  0.9790828 ]
 [-1.2024965   0.00880218  1.1936939 ]], shape=(3, 3), dtype=float32)

您还可以选择使用 tf.print 而不是 print,它只显示值(不是形状,也不是数据类型),这与调用 print(tensor.numpy()).