[Novice Xiaobai] How to use tensorboard in tensorflow

Visualize scalars (charts) with tensorboard.


1. Initialize the log file

First, you need to introduce the following code, which is located in main.py.
Among them, logdir is the directory where log files are stored.

	import tensorflow as tf
    logdir = './tensorboard/test1/'
    summary_writer = tf.summary.create_file_writer(logdir)

The project structure is as follows:
insert image description here

2. Construct the diagram

Then you need to add the following code to the training place.
Here, I am recording the loss. step represents the step size, which is the abscissa in the tensorboard line graph, and is generally represented by the current epoch. The ordinate here is the loss value.

        with summary_writer.as_default():
            tf.summary.scalar('d_loss', d_loss, step=epoch)
            tf.summary.scalar('g_loss', g_loss, step=epoch)

3. Run the command and successfully open tensorboard

At this time, you need to click the "terminal" button at the bottom of the screen
insert image description here
, click the small inverted triangle, and then click "command prompt".
insert image description here
Go to the folder where the log files are located. (I am entering the tensorboard folder here)
insert image description here
Now it is the last step, you need to run the following command.

tensorboard --logdir=test1 --port=6007

logdir indicates which log folder is, and my log folder here is test1.
port specifies the port number on which the program is running. The port number I am using is 6007.
After running successfully, the following content appears. Click the link to jump to tensorboard!
insert image description here
Tensorboard visualizes the training process and can see the current training status in real time.
insert image description here

PS: I get an error: No dashboards are active for the current data set

Of course, when I used it for the first time, I did not successfully jump to the above interface, but jumped to the following interface.
insert image description here
Reason 1 : The program may not be running at this time, and there is no data in the log file, so naturally the graph cannot be displayed.
Solution: Start the program and start running until there is data in the log file, then refresh the interface and try again.

Reason 2 : The path of the log file is wrong, and tensorboard cannot find the file.

logdir = './tensorboard/test1/'

Solution: Correct the path to the log file.

Reason 3 : The originally set port 6007 is being occupied by other processes.

tensorboard --logdir=test1 --port=6007

Solution: Change the port to another number (6006, 6008, 6009, etc.).

Guess you like

Origin blog.csdn.net/rellvera/article/details/129778636