I tried using Tensorboard, a visualization tool for machine learning

Introduction

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.

Installation

Since I am using Anaconda, install Tensorboard with the following command.

conda install tensorboard

coding

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.

Commentary

Simply put, the code above plots an array with random values and a sin function.

Import SummaryWriter

from torch.utils.tensorboard import SummaryWriter Import SummaryWriter, which is a module required for drawing graphs on Tensorboard.

Instance generation

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.

Assign value

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.

close

Let's close it last with writer.close ().

View the graph

Run tb.py

Let's run the above code. The graph is drawn.

python tb.py

View the graph

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.

スクリーンショット 2020-08-12 22.48.19.png

If you look at it in chrome, you can see that the graph plots neatly.

See the graph ahead of ssh

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?

Ssh to remote server

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

Run tb.py on remote server

Let's run the code that draws the graph on the sshed remote server.

@Remote server


python tb.py

Run Tensorboard

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)

View the graph

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.

スクリーンショット 2020-08-12 22.48.19.png

Summary

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.

Recommended Posts

I tried using Tensorboard, a visualization tool for machine learning
[Python] I made a classifier for irises [Machine learning]
Memo for building a machine learning environment using Python
PyTorch Learning Note 2 (I tried using a pre-trained model)
I tried to compress the image using machine learning
I tried to implement various methods for machine learning (prediction model) using scikit-learn.
I tried machine learning with liblinear
I tried hosting a TensorFlow deep learning model using TensorFlow Serving
[TF] I tried to visualize the learning result using Tensorboard
I tried reinforcement learning using PyBrain
I tried deep learning using Theano
I tried to compare the accuracy of machine learning models using kaggle as a theme.
I made a tool that makes it convenient to set parameters for machine learning models.
I tried playing a ○ ✕ game using TensorFlow
I tried drawing a line using turtle
[Kaggle] I tried ensemble learning using LightGBM
I tried the OSS visualization tool, superset
I tried using pipenv, so a memo
Creating a development environment for machine learning
[Visualization] I tried using Bokeh / plotly! 【memorandum】
[Updated Ver1.3.1] I made a data preprocessing library DataLiner for machine learning.
[ML-Aents] I tried machine learning using Unity and Python TensorFlow (v0.11β compatible)
I created a visualization site for GDP (Gross Domestic Product) using DASH!
I tried to classify guitar chords in real time using machine learning
I tried using eval (a, b) for Fibonacci, but it wasn't fast
I tried using scrapy for the first time
A story about simple machine learning using TensorFlow
vprof --I tried using the profiler for Python
I made a useful tool for Digital Ocean
I tried using Pythonect, a dataflow programming language.
I tried reading a CSV file using Python
I tried using firebase for Django's cache server
I tried running alembic, a Python migration tool
I tried using a database (sqlite3) with kivy
I tried to make a ○ ✕ game using TensorFlow
I installed Chainer, a framework for deep learning
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
I tried to predict the change in snowfall for 2 years by machine learning
I tried to process and transform the image and expand the data for machine learning
Creating a position estimation model for the Werewolf Intelligence Tournament using machine learning
A memorandum of method often used in machine learning using scikit-learn (for beginners)
I tried using argparse
I tried using anytree
I tried using aiomysql
I tried using Summpy
I tried using coturn
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried deep learning
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using face_recognition
I tried using Jupyter
I tried using PyCaret
I tried to understand the learning function of neural networks carefully without using a machine learning library (first half).
I tried using Heapq
I tried using doctest