Using TensorBoard with a Google Cloud Platform Instance

20 Feb 2020 - Monte Fischer

It took me a little while to figure out how to set up TensorBoard on the Google Cloud Platform instance I use for my master’s research. This is a quick write-up of what I figured out to do; perhaps it will help someone else.

TensorBoard is a useful web application that visualizes Tensorflow networks. The official resources for TensorBoard tell you all about how to code your network so that it produces log files that TensorBoard can understand and visualize. Google Cloud Platform (GCP) offers a way to rent out powerful machines from Google, and offers $300 of free credits for first-time users. Assuming you’ve gotten GCP set up, have installed the gcloud command line tool, and have some log files that can be used by TensorBoard on your GCP instance, here’s what you do next.

Start up your GCP instance, connect to it, and launch TensorBoard

$ gcloud compute instances start [INSTANCE_NAME]
$ gcloud compute ssh [INSTANCE_NAME]
user@MY_INSTANCE:~$ tensorboard --logdir LOG_DIRECTORY 

Assuming that port 6006 is open, this will start hosting TensorBoard from LOG_DIRECTORY at port 6006 of your GCP instance. Problem is, you’d like to be able to see what TensorBoard is saying from your local machine. The solution is classic port forwarding. You can set up an ssh connection to your GCP instance such that one of your local ports gets “forwarded” to a port on the remote GCP instance. That way, whenever you go to look at that local port on your machine, you can see through the ssh tunnel to what’s being hosted on your GCP instance’s port 6006. As a command, you do this in a terminal on your local host:

$ gcloud compute ssh [INSTANCE_NAME] -- -NfL 6006:localhost:6006

Now open up a web browser and connect to localhost:6006. You will be taken, through the ssh port forward, to port 6006 on your GCP instance! You should see the TensorBoard dashboard appear. That’s it! A simple practical lesson in port forwarding.

P.S. If, like me, you are mucking around with TensorBoard and get annoyed at the multiple processes you have spawned clogging up your ports, use $ netstat -anp | grep 6006 to find the PID of the offending process and stop it with $ kill -9 [PID]