I used Tensorboard for the first time to draw a graph and was impressed by its convenience, so I will share it. The Deep Learning framework used PyTorch.
Since I am using Anaconda, install Tensorboard with the following command.
conda install tensorboard
tb.py
import numpy as np
from torch.utils.tensorboard import SummaryWriter#Import SummaryWriter to draw graph
np.random.seed(1000)
x = np.random.randn(1000)
writer = SummaryWriter(log_dir="./logs")#Instance generation Specify the directory to save
for i in range(1000):
writer.add_scalar("x", x[i], i)#Write value
writer.add_scalar("sin", np.sin(i), i)
writer.close()#close
Please note that if the file name is *** tensorboard.py ***, it will be covered with module and an ImportError will occur.
Simply put, the code above plots an array with random values and a sin function.
from torch.utils.tensorboard import SummaryWriter
Import SummaryWriter, which is a module required for drawing graphs on Tensorboard.
writer = SummaryWriter(log_dir="./logs")
This will create a logs directory in the current directory and the files for Tensorboard will be saved in those logs.
Enter the value of the array with writer.add_scalar ("x", x [i], i)
.
It is writer.add_scalar (tags, scalar_value, global_step)
, specify the name of the graph with tags, substitute the value to be saved with scalar_value, and specify the interval of the horizontal axis of the graph with global_step.
Let's close it last with writer.close ()
.
Let's run the above code. The graph is drawn.
python tb.py
Let's execute the following command. Let's specify the directory saved by --logdir =" "
. This time it is
./logs`.
tensorboard --logdir="./logs"
Then, the following statement will be output to the terminal.
TensorBoard 2.2.1 at http://localhost:8000/ (Press CTRL+C to quit)
The local server will be launched, so type http: // localhost: 8000 /
in your browser.
If you look at it in chrome, you can see that the graph plots neatly.
Deep Learning code requires a lot of calculation and takes a huge amount of time on a local PC (PC at hand), so The default is to ssh to the GPU of the server in the lab and run the code on the server. Then, how do you try the graph drawn on the remote server on the local PC in such a case?
When sshing, use the -L option to connect the client (local PC) localhost: 9000 to the remote server username @ server IP address: 8000.
@Local PC
ssh username@IP address of the server-L 9000:localhost:8000
Let's run the code that draws the graph on the sshed remote server.
@Remote server
python tb.py
Let's execute the command to see the graph on the sshed remote server. Since the port connected to the local PC when sshing is 8000, let's specify 8000 with the --port option and execute it.
@Remote server
tensorboard --logdir="./logs" --port 8000
The following statement is output.
@Remote server
TensorBoard 2.2.1 at http://localhost:8000/ (Press CTRL+C to quit)
When I entered http: // localhost: 8000 /
in the browser earlier, I could see the graph, but this time I can't.
This time, I connected port 8000 of the remote server and port 9000 of the local PC, so
If you enter http: // localhost: 9000 /
in the browser of your local PC, you can see the same graph as before.
I drew a graph using Tensorboard in PyTorch. I also introduced how to view the graph of the code that was sent on the remote server of the ssh destination on the local PC. I would also like to use this Tensorboard and ssh -L for deep learning.