0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

tf.print() を使う時に f-string 内だとテンソルの中身が表示できない

Last updated at Posted at 2020-05-24

概要

タイトルの通りです。
tf.print()f-string は使わないほうが良いでしょう。

実際に、以下のコードを動かして出力を比較しましょう。

import numpy as np
import tensorflow as tf

@tf.function
def add_a_b(a, b):
    c = tf.add(a, b)
    print("(1):", c)
    tf.print("(2):", f"{c}")
    tf.print("(3):", c)
    return c

a = tf.constant(np.asarray([[1, 2], [3, 4]]), dtype=tf.float32)
b = tf.constant(np.asarray([[4, 3], [2, 1]]), dtype=tf.float32)

c = add_a_b(a, b)

出力はこのようになります。
(2) が f-string ですが、tf.print() の恩恵を受けられていないことが分かります。

(1): Tensor("Add:0", shape=(2, 2), dtype=float32)
(2): Tensor("Add:0", shape=(2, 2), dtype=float32)
(3): [[5 5]
 [5 5]]
0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?