<jupyter_start><jupyter_text>reference to https://jackiexiao.github.io/eat_pytorch_in_20_days/5.%E4%B8%AD%E9%98%B6API/5-4%2CTensorBoard%E5%8F%AF%E8%A7%86%E5%8C%96/<jupyter_code>import torch 
from torch import nn
from torch.utils.tensorboard import SummaryWriter
from torchkeras import Model, summary
import datetime

logs_dir_root = "./logs/"
class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3,out_channels=32,kernel_size = 3)
        self.pool = nn.MaxPool2d(kernel_size = 2,stride = 2)
        self.conv2 = nn.Conv2d(in_channels=32,out_channels=64,kernel_size = 5)
        self.dropout = nn.Dropout2d(p = 0.1)
        self.adaptive_pool = nn.AdaptiveMaxPool2d((1,1))
        self.flatten = nn.Flatten()
        self.linear1 = nn.Linear(64,32)
        self.relu = nn.ReLU()
        self.linear2 = nn.Linear(32,1)
        self.sigmoid = nn.Sigmoid()

    def forward(self,x):
        x = self.conv1(x)
        x = self.pool(x)
        x = self.conv2(x)
        x = self.pool(x)
        x = self.dropout(x)
        x = self.adaptive_pool(x)
        x = self.flatten(x)
        x = self.linear1(x)
        x = self.relu(x)
        x = self.linear2(x)
        y = self.sigmoid(x)
        return y

net = Net()
print(net)
summary(net, input_shape= (3, 32, 32))
timestamp = "{0:%Y%m%d_%H%M%S/}".format(datetime.datetime.now())
logs_dir = f"./{logs_dir_root}/tensorboard_sigmods/{timestamp}"
writer = SummaryWriter(logs_dir)

writer.add_graph(net, input_to_model = torch.rand(1, 3, 32, 32))
writer.close()
import numpy as np 
import torch 
import datetime
from torch.utils.tensorboard import SummaryWriter

# f(x) = a*x**2 + b*x + c的最小值
x = torch.tensor(0.0, requires_grad = True) # x需要被求导
a = torch.tensor(1.0)
b = torch.tensor(-2.0)
c = torch.tensor(0.5)

optimizer = torch.optim.SGD(params=[x],lr = 0.01)

def f(x):
    result = a*torch.pow(x,2) + b*x + c 
    return(result)

timestamp = "{0:%Y%m%d_%H%M%S/}".format(datetime.datetime.now())
logs_dir = f'{logs_dir_root}/funcx/{timestamp}'
writer = SummaryWriter(logs_dir)
for i in range(500):
    optimizer.zero_grad()
    y = f(x)
    y.backward()
    optimizer.step()
    writer.add_scalar("x", x.item(), i) #日志中记录x在第step i 的值
    writer.add_scalar("y", y.item(), i) #日志中记录y在第step i 的值

writer.close()
print("y =", f(x).data, ";" , "x =", x.data)
import numpy as np
import torch
from torch.utils.tensorboard import SummaryWriter
import datetime

# create a normalized distribution matrix
def norm(mean, std):
    t = std * torch.randn((100, 20)) + mean
    return t
timestamp = "{0:%Y%m%d_%H%M%S/}".format(datetime.datetime.now())
logs_dir = f'{logs_dir_root}/distribution/{timestamp}'
writer = SummaryWriter(logs_dir)

for step, mean in enumerate(range(-10, 10, 1)):
    w = norm(mean, 1)
    writer.add_histogram("w", w, step)
    writer.flush()
writer.close()
import datetime
import torch
import torchvision
from torch import nn
from torch.utils.data import Dataset,DataLoader
from torch.utils.tensorboard import SummaryWriter


transform_train = torchvision.transforms.Compose(
    [torchvision.transforms.ToTensor()])
transform_valid = torchvision.transforms.Compose(
    [torchvision.transforms.ToTensor()])
data_root = "./data/"
# # when set download=True, download error(SSLCertVerificationError), so I download manually via http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz and
# # then cd ${data_cifar10} and tar zxf cifar-10-python.tar.gz
data_cifar10 = f'{data_root}/cifar20/'
print(data_cifar10)
ds_train = torchvision.datasets.CIFAR10(root=data_cifar10, train=True, download=True, transform=transform_train)
ds_valid = torchvision.datasets.CIFAR10(root=data_cifar10, train=False, download=False, transform=transform_valid)
# 
data_cifar100 = f'{data_root}/cifar100/'
# download url http://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz
# cd ${data_cifar100} and tar zxf cifar-100-python.tar.gz
ds_train = torchvision.datasets.CIFAR100(root=data_cifar100, train=True, download=True, transform=transform_train)
ds_valid = torchvision.datasets.CIFAR100(root=data_cifar100, train=False, download=False, transform=transform_valid)
print(ds_train.class_to_idx)

dl_train = DataLoader(ds_train,batch_size = 50,shuffle = True,num_workers=3)
dl_valid = DataLoader(ds_valid,batch_size = 50,shuffle = True,num_workers=3)

dl_train_iter = iter(dl_train)
images, labels = dl_train_iter.next()

timestamp = "{0:%Y%m%d_%H%M%S}/".format(datetime.datetime.now())
logs_dir = f"{logs_dir_root}/visualize_raw_img/{timestamp}"
writer = SummaryWriter(logs_dir)

# display a raw image
writer.add_image('images[0]', images[0])
writer.close()

# 将多张图片拼接成一张图片，中间用黑色网格分割
# concat multipule images to one
# create grid of images
img_grid = torchvision.utils.make_grid(images)
writer.add_image('image_grid', img_grid)
writer.close()

writer.add_images("images", images, global_step = 0)
writer.close()
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
from matplotlib import pyplot as plt 

figure = plt.figure(figsize=(8,8)) 
for i in range(9):
    img,label = ds_train[i]
    img = img.permute(1,2,0)
    ax=plt.subplot(3,3,i+1)
    ax.imshow(img.numpy())
    ax.set_title("label = %d"%label)
    ax.set_xticks([])
    ax.set_yticks([]) 
plt.show()

timestamp = "{0:%Y%m%d_%H%M%S}/".format(datetime.datetime.now())
logs_dir = f'{logs_dir_root}/visualize_manually_plot/{timestamp}'
writer = SummaryWriter(logs_dir)
writer.add_figure('figure', figure, global_step=0)
writer.close()
# reference to https://stackoverflow.com/questions/55970686/tensorboard-not-found-as-magic-function-in-jupyter
%load_ext tensorboard
%tensorboard --logdir {logs_dir_root}
from tensorboard import notebook
notebook.list() # read data from ~\AppData\Local\Temp\.tensorboard-info on windows
notebook.start(f"--logdir {logs_dir_root}")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14674838270343887, 'fraction_numerical': 0.0296220633299285, 'mean_word_length': 4.665380906460945, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '490886', 'license': 'Apache-2.0', 'n_tokens_mistral': 2321, 'n_tokens_neox': 2233, 'n_words': 401, 'path': 'python/third-party/tensorboard/pytorch/visualizing_pics.ipynb', 'repo_name': 'algorithmlover2016/learn_notes', 'size': '5874'}