In this article, I will show you how to use TensorBoard in an amazon sagemaker notebook instance.

In one of my recent projects, I needed to visualize training and validation loss using TensorBoard in a sagemaker notebook instance. However, after searching online, I realized that there is rare blog covering this topic. So I wrote this step-by-step tutorial and hopefully it can help you.

Let’s dive into this topic using a simple regression example.

1. Create sample data

Firstly, I’m going to create a sample data set using NumPy and simply will create a variable called x, which is a sample of random data from 0 to 2π. Then we will compute y, which is the sign of x, where x is a random data. So x is our input values and y will be our truth for our dataset.

import numpy as np
	import matplotlib.pyplot as plt

	np.random.seed(42)
	x = 2 * np.pi * np.random.random((100,1))
	y = np.sin(x)

	plt.plot(x, y,'o')
	plt.xlabel('x')
	plt.ylabel('y')

	x_all = np.arange(0,2*np.pi, 2*np.pi/100).reshape(-1,1)
	plt.plot(x_all, np.sin(x_all), 'r')

We can go ahead and plot that out. the blue dots represent our sample dataset. And the red line illustrate what the sign of x would be.

Image for post

2. Build neural network

Then let’s build a simple fully connected neural network. This is a very simple neural network. It has two hidden layers. We’re going to use 100 nodes in the first hidden layer and 50 nodes in the second hidden layer. And use the ReLu activation for each of these layers.

import torch.nn as 
	import torch.nn.functional as F

	class MyNet(nn.Module):

	    def __init__(self):
	        super(MyNet, self).__init__()
	        self.fc1 = nn.Linear(1,100)
	        self.fc2 = nn.Linear(100,50)
	        self.fc3 = nn.Linear(50,1)

	    def forward(self, x):
	        x = F.relu(self.fc1(x))
	        x = F.relu(self.fc2(x))
	        return self.fc3(x)

	model = MyNet()
	print(model)

If we print the model, we can see a print out of what the model looks like.

Image for post

This doesn’t show us a lot of information visually, but it gives us the information of how many nodes are in each layer and how the layers are connected.

#tensorboard #aws-sagemaker #jupyterlab #pytorch #sagemaker

How to use TensorBoard in an Amazon SageMaker Notebook Instance
6.20 GEEK