[PYTHON] When using tf.print (), the contents of the tensor cannot be displayed if it is inside f-string.

Overview

As the title says. It's best not to use f-string intf.print ().

In fact, let's run the following code and compare the output.

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)

The output looks like this. You can see that (2) is f-string but does not benefit fromtf.print ().

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

Recommended Posts

When using tf.print (), the contents of the tensor cannot be displayed if it is inside f-string.
Solution when the image cannot be displayed with tkinter [python]
If it is not easy to understand, it cannot be improved.
The story of using mysqlclient because PyMySQL cannot be used with Django 2.2
It seems that the version of pyflakes is not the latest when flake8 is installed
Measures to be taken when "Cannot open display" is displayed in X11 Forward
When increasing the available memory of node, it may be limited by vm.max_map_count
What to do if (base) is displayed at the beginning of the Mac terminal
[Solution] When inserting "0001" into the column of string in sqlite3, it is entered as "1".
[Python Data Frame] When the value is empty, fill it with the value of another column.
[Curse of dimensionality] If the number of sensors is changed to ∞, can anomaly be detected?
[Is it explosive !?] Setup for using the GPU version of Tensorflow on OS X
What to do if the progress bar is not displayed in tqdm of python