# Load the TensorBoard notebook extension
TensorFlow
Tensor Flow graphs
Reusing TensorBoard on port 6006 (pid 431396), started 0:35:57 ago. (Use '!kill 431396' to kill it.)
from tensorboard import notebook
list() # View open TensorBoard instances notebook.
No known TensorBoard instances running.
=6006, height=1000) notebook.display(port
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
############## TENSORBOARD ########################
import sys
from torch.utils.tensorboard import SummaryWriter
# default `log_dir` is "runs" - we'll be more specific here
= SummaryWriter()
writer ###################################################
# Device configuration
= torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device
# Hyper-parameters
= 784 # 28x28
input_size = 500
hidden_size = 10
num_classes = 1
num_epochs = 64
batch_size = 0.001
learning_rate
# MNIST dataset
= torchvision.datasets.MNIST(root='./Data',
train_dataset =True,
train=transforms.ToTensor(),
transform=True)
download
= torchvision.datasets.MNIST(root='./Data',
test_dataset =False,
train=transforms.ToTensor())
transform
# Data loader
= torch.utils.data.DataLoader(dataset=train_dataset,
train_loader =batch_size,
batch_size=True)
shuffle
= torch.utils.data.DataLoader(dataset=test_dataset,
test_loader =batch_size,
batch_size=False)
shuffle
= iter(test_loader)
examples = next(examples)
example_data, example_targets
for i in range(6):
2,3,i+1)
plt.subplot(0], cmap='gray')
plt.imshow(example_data[i]['off')
plt.axis( plt.show()
############## TENSORBOARD ########################
= torchvision.utils.make_grid(example_data)
img_grid
img_grid
'mnist_images', img_grid)
writer.add_image(
writer.flush()#sys.exit()
###################################################
# Fully connected neural network with one hidden layer
class NeuralNet(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(NeuralNet, self).__init__()
self.input_size = input_size
self.l1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.l2 = nn.Linear(hidden_size, num_classes)
def forward(self, x):
= self.l1(x)
out = self.relu(out)
out = self.l2(out)
out # no activation and no softmax at the end
return out
import timm
# Load ResNet model without the final classification layer
= timm.create_model('resnet18', pretrained=True, num_classes=10)
model # Modify the first convolution layer to accept single-channel images
= nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
model.conv1
= model.to(device)
model
# model = NeuralNet(input_size, hidden_size, num_classes).to(device)
# Loss and optimizer
= nn.CrossEntropyLoss()
criterion = torch.optim.Adam(model.parameters(), lr=learning_rate) optimizer
############## TENSORBOARD ########################
# writer.add_graph(model, example_data.reshape(-1, 28*28).to(device))
writer.add_graph(model, example_data.to(device))
writer.flush()#sys.exit()
###################################################
# Train the model
= 0.0
running_loss = 0
running_correct = len(train_loader)
n_total_steps for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# origin shape: [100, 1, 28, 28]
# resized: [100, 784]
# images = images.reshape(-1, 28*28).to(device)
= images.to(device)
images = labels.to(device)
labels
# Forward pass
= model(images)
outputs = criterion(outputs, labels)
loss
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
+= loss.item()
running_loss
= torch.max(outputs.data, 1)
_, predicted += (predicted == labels).sum().item()
running_correct if (i+1) % 100 == 0:
print (f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{n_total_steps}], Loss: {loss.item():.4f}')
############## TENSORBOARD ########################
'training loss', running_loss / 100, epoch * n_total_steps + i)
writer.add_scalar(= running_correct / 100 / predicted.size(0)
running_accuracy 'accuracy', running_accuracy, epoch * n_total_steps + i)
writer.add_scalar(= 0
running_correct = 0.0
running_loss
writer.flush()###################################################
Epoch [1/1], Step [100/938], Loss: 0.4240
Epoch [1/1], Step [200/938], Loss: 0.2560
Epoch [1/1], Step [300/938], Loss: 0.1293
Epoch [1/1], Step [400/938], Loss: 0.1558
Epoch [1/1], Step [500/938], Loss: 0.1048
Epoch [1/1], Step [600/938], Loss: 0.0294
Epoch [1/1], Step [700/938], Loss: 0.1048
Epoch [1/1], Step [800/938], Loss: 0.1394
Epoch [1/1], Step [900/938], Loss: 0.0257
# Test the model
# In test phase, we don't need to compute gradients (for memory efficiency)
= []
class_labels = []
class_preds with torch.no_grad():
= 0
n_correct = 0
n_samples for images, labels in test_loader:
# images = images.reshape(-1, 28*28).to(device)
= images.to(device)
images = labels.to(device)
labels = model(images)
outputs # max returns (value ,index)
= torch.max(outputs.data, 1)
values, predicted += labels.size(0)
n_samples += (predicted == labels).sum().item()
n_correct
= [F.softmax(output, dim=0) for output in outputs]
class_probs_batch
class_preds.append(class_probs_batch)
class_labels.append(labels)
# 10000, 10, and 10000, 1
# stack concatenates tensors along a new dimension
# cat concatenates tensors in the given dimension
= torch.cat([torch.stack(batch) for batch in class_preds])
class_preds = torch.cat(class_labels)
class_labels
= 100.0 * n_correct / n_samples
acc print(f'Accuracy of the network on the 10000 test images: {acc} %')
############## TENSORBOARD ########################
= range(10)
classes for i in classes:
= class_labels == i
labels_i = class_preds[:, i]
preds_i str(i), labels_i, preds_i, global_step=0)
writer.add_pr_curve(
writer.flush()###################################################
Accuracy of the network on the 10000 test images: 97.53 %
writer.close()