Skip to content

Running Tensorboard on a Remote Server

Manoj Kolpe edited this page Sep 8, 2022 · 1 revision

Running Tensorboard in the Cluster and Accessing the Tensorboard on a Remote Server

Install Tensorboard and Pytorch[1]

$ conda install pytorch torchvision -c pytorch
$ conda install -c conda-forge tensorboard

or

$ pip install torch torchvision
$ pip install tensorboard

Using Tensorboard in PyTorch [1]

Example code:

import torch
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter()

x = torch.arange(-5, 5, 0.1).view(-1, 1)
y = -5 * x + 0.1 * torch.randn(x.size())

model = torch.nn.Linear(1, 1)
criterion = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr = 0.1)

def train_model(iter):
    for epoch in range(iter):
        y1 = model(x)
        loss = criterion(y1, y)
        writer.add_scalar("Loss/train", loss, epoch)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

train_model(10)
writer.flush()

There are two versions of tensorboard

  • Tensorboard - To view in the local system
  • Tensorboard dev - Push the logs to the Tensorboard server and viewable with a link

To run the Tensorboard in the local system, run the following code.

$ tensorboard --logdir=runs

To push the logs to the Tensorboard cloud, run the following command and view the generated link.

$ tensorboard dev upload --logdir runs

View the Tensorboard logs running in the cloud in a remote/local server.

Requirements

  • screen [2]
  • tensorboard [1]
  • ssh

Screen

GNU Screen is a terminal multiplexer, a software application that can multiplex several virtual consoles, allowing users to access multiple login sessions inside a single terminal window, or detach and reattach sessions from a terminal. [3]

To create a virtual console, run the following command.

$ screen

To list the active screen.

$ screen -ls

Screenshot from 2022-09-08 18-23-28

Renaming the session while inside the session/Renaming without attaching

$ screen -X sessionname foobars
$ screen -ls
There is a screen on:
    8890.foobars    (22/12/11 18:39:22) (Detached)
1 Socket in /var/run/screen/S-user.

Screenshot from 2022-09-08 18-24-09

Renaming the session while outside the session

$ screen -ls
There is a screen on:
    8890.foo        (02/23/2015 18:39:22)   (Detached)
    5136.barfoos    (02/23/2015 18:39:22)   (Detached)
1 Socket in /var/run/screen/S-user.

$ screen -S 8890.foo -X sessionname foobars
$ screen -ls
There is a screen on:
    8890.foobars    (02/23/2015 18:39:22)   (Detached)
    5136.barfoos    (02/23/2015 18:39:22)   (Detached)
1 Socket in /var/run/screen/S-user.

Command to detach/exit the session without killing it

ctrl+a and d

Screenshot from 2022-09-08 18-24-52

We have two detached sessions, and if you want to enter them.

$ screen -r tensorboard2

Screenshot from 2022-09-08 18-25-33

To detach ctrl+a and d

Enter the tensorboard2 session and run the below command to start the tensorboard

Here we are setting the port number for the Tensorboard as 6356; make sure to select a unique port number.

$ tensorboard --logdir runs/ --port 6356

Screenshot from 2022-09-08 18-26-13

Exit the session by ctrl+a and d

In your local system, run the below command to connect to the cluster with a specific port. Port number 6356 of the cluster is connected to the 16006 port of your local system.

$ ssh -L 16006:127.0.0.1:6356 [email protected]

Enter the password while prompted Screenshot from 2022-09-08 18-26-46

Run the server with the address: http://127.0.0.1:16006/

Screenshot from 2022-09-08 18-27-39

screen shortcuts

References

[1] https://pytorch.org/tutorials/recipes/recipes/tensorboard_with_pytorch.html [2] https://linuxize.com/post/how-to-use-linux-screen/ [3] https://en.wikipedia.org/wiki/GNU_Screen [4] https://superuser.com/questions/370510/rename-screen-session [5] https://stackoverflow.com/questions/37987839/how-can-i-run-tensorboard-on-a-remote-server

Clone this wiki locally