### TensorBoard Setup and Logging Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb This snippet shows how to set up TensorBoard and log data. Ensure TensorBoard is installed and the log directory is correctly specified. ```python from tensorboardX import SummaryWriter writer = SummaryWriter("runs/pokebnn-demo") ``` -------------------------------- ### TensorBoard Logging Setup Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Configures and initializes TensorBoard for logging training metrics. Ensure TensorBoard is installed and accessible. ```python 3b/V4GBl5mSAx8plW3+V/Q27kL2yntTEhu2rd5f7ykBICA+3+sNO8Fa8gg3bVu8v95SI+gTKtvFTsKNiW5WvbKe1A ``` ```python MgI+f1ypn71zzSg3bVu8v95SI+fMq2+VM/ob9K57YT7mg ``` ```python AAR+7at3l/vKQ3bVu8v8AeUgFG6nL8Me8oSAx+jzKtq5WFGx/HHvbCe2Ehu2rd5f7ykArnsJvwyJ/iGxIDH63Mq24m8aNh+ORPbCeMNiQ3bVu8v8AeUgJAR8TrzUPiMfUoN21bvL/AHlIj4kyrb8VD+htuRn2wn3FAMgFnWOtM3wZzyTFPdtW7y/3lItKvNq29M3Gi4fi7ntlPamAlovsZn92n6hVEVGm1bczX9C/mJ9sp9wVd21bvL/eUgD3X+J4HI8tkSAx96ZVt/ov9DbdySPbKe3ZEhu2rd5f7ykBICPoPWtr4znlqDdtW7y/3lIj6FMq29bWFGx6Zz2ynt1AMgEfVOr03wwvNuBu2rd5f7ykWFUmVbX07Gje2y9sJ7msBPgI/dtW7y/3lIbtq3eX+8pAKX7Jqfhn/tNiQEBTJlW3TU8KN7b4wnuTYv8AdtW7y/3lIC4ndQT++Z84kXAiZUupqS2lyk5EG+ySlboSeUtYnbh2RL ``` -------------------------------- ### Install AQT Library Source: https://github.com/google/aqt/blob/main/aqt/jax/v2/examples/examples.ipynb Installs the AQT library using pip. This is a prerequisite for using the library. ```python # install the AQT library !pip install aqtp ``` -------------------------------- ### TensorBoard Initialization and Basic Logging Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Initializes a TensorBoard writer and logs a single scalar value. This is a minimal example to show TensorBoard setup. ```python from torch.utils.tensorboard import SummaryWriter writer = SummaryWriter('runs/poke_bnn_demo') # Log a scalar value writer.add_scalar('Test/Scalar', 0.5, 1) writer.close() ``` -------------------------------- ### Data Processing and Visualization Setup Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Imports necessary libraries for data manipulation and visualization. This setup is crucial for preparing and displaying training metrics. ```python import numpy as np import matplotlib.pyplot as plt from matplotlib.pyplot import figure from IPython.display import display from IPython.display import Image from sklearn.metrics import confusion_matrix, classification_report from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler, normalize from sklearn.svm import SVC from sklearn.decomposition import PCA from sklearn.manifold import TSNE import torch import torch.nn as nn import torch.optim as optim import torch.nn.functional as F from torch.utils.data import Dataset, DataLoader from torch.utils.tensorboard import SummaryWriter from tensorboard.backend.event_processing import event_accumulator from poke_bnn.data import PokeBNNDataset from poke_bnn.model import PokeBNN from poke_bnn.train import Trainer from poke_bnn.utils import plot_confusion_matrix, plot_roc_curve, plot_pr_curve, plot_training_curves ``` -------------------------------- ### TensorBoard Visualization Setup Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Sets up TensorBoard logging for visualizing training metrics. Requires the `tensorboard` and `tensorflow` libraries. ```python from torch.utils.tensorboard import SummaryWriter # Create a TensorBoard writer object writer = SummaryWriter('runs/poke_bnn_demo') ``` -------------------------------- ### Example of TensorBoard Usage in Training Loop Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb A complete example demonstrating how to integrate TensorBoard logging within a typical PyTorch training loop. It includes logging scalars, graphs, and histograms. ```python import torch import torch.nn as nn from torch.utils.tensorboard import SummaryWriter # Define a simple model class SimpleModel(nn.Module): def __init__(self): super(SimpleModel, self).__init__() self.conv1 = nn.Conv2d(1, 16, 3, 1) self.relu = nn.ReLU() self.fc1 = nn.Linear(16 * 26 * 26, 10) def forward(self, x): x = self.conv1(x) x = self.relu(x) x = torch.flatten(x, 1) x = self.fc1(x) return x model = SimpleModel() optimizer = torch.optim.Adam(model.parameters(), lr=0.001) criterion = nn.CrossEntropyLoss() # Initialize TensorBoard writer writer = SummaryWriter('runs/poke_bnn_demo') # Dummy input and target input_tensor = torch.randn(1, 1, 28, 28) target_tensor = torch.randint(0, 10, (1,)) # Log the model graph writer.add_graph(model, input_to_model=input_tensor) # Dummy training loop num_epochs = 5 for epoch in range(num_epochs): # Simulate training step optimizer.zero_grad() outputs = model(input_tensor) loss = criterion(outputs, target_tensor) loss.backward() optimizer.step() # Log metrics accuracy = (torch.argmax(outputs, dim=1) == target_tensor).float().mean() writer.add_scalar('Loss/train', loss.item(), epoch) writer.add_scalar('Accuracy/train', accuracy.item(), epoch) # Log histograms for weights and biases writer.add_histogram('Weights/conv1', model.conv1.weight, epoch) writer.add_histogram('Biases/conv1', model.conv1.bias, epoch) print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}, Accuracy: {accuracy.item():.4f}') # Close the writer writer.close() print('Training finished and TensorBoard logs saved to runs/poke_bnn_demo') ``` -------------------------------- ### Example: Add Audio to TensorBoard Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs audio data to TensorBoard. Useful for tasks involving audio generation or analysis. ```python writer.add_audio('generated_audio', audio_tensor, epoch, sample_rate=16000) ``` -------------------------------- ### Example Data Generation (Placeholder) Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb This snippet represents placeholder code for generating example data or metrics that would typically be logged to TensorBoard. It is not functional code. ```python train_loss = 0.1 train_acc = 0.95 epoch = 1 sample_input = torch.rand(1, 3, 32, 32) images = torch.rand(4, 3, 32, 32) hparams_text = "Learning Rate: 0.001, Optimizer: Adam" ``` -------------------------------- ### Example: Add Video to TensorBoard Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs video data to TensorBoard. Applicable for tasks involving video generation or processing. ```python writer.add_video('generated_video', video_tensor, epoch, fps=4) ``` -------------------------------- ### Example: Add Custom HParams Summary with HParam object, Run Name, Metric Components, Weights, Tag and Global Step (Redundant) Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb This example demonstrates adding a complete custom hyperparameter summary with redundant parameters. It's recommended to use parameters consistently. ```python from torch.utils.tensorboard.summary import HParam writer.add_hparams([HParam('custom_learning_rate_variant', 0.0005)], {'metrics/custom_accuracy_w': 0.72, 'metrics/custom_loss_w': 0.28}, run_name='custom_lr00005_variant_weighted_tag', global_step=step, global_step=step) ``` -------------------------------- ### Example: Add Custom HParams Summary with HParam object, Metric Components, Weights, Tag and Global Step (Redundant) Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb This example demonstrates adding a custom hyperparameter summary with redundant parameters. It's recommended to use parameters consistently. ```python from torch.utils.tensorboard.summary import HParam writer.add_hparams([HParam('custom_feature_set', 'B')], {'metrics/custom_precision_w': 0.88, 'metrics/custom_recall_w': 0.82}, run_name='custom_feature_set_B_weighted_tag', global_step=step, global_step=step) ``` -------------------------------- ### Example: Add Hyperparameter Summary with Metric Components Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a hyperparameter summary where metrics are broken down into components. Allows for more granular analysis of performance. ```python writer.add_hparams({'kernel': 'rbf', 'gamma': 'scale'}, {'metrics/accuracy': 0.92, 'metrics/precision': 0.91, 'metrics/recall': 0.93}) ``` -------------------------------- ### Initialize TensorBoard Logger Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Initializes the TensorBoard logger for tracking training metrics. Ensure TensorBoard is installed and configured. ```python from torch.utils.tensorboard import SummaryWriter writer = SummaryWriter() ``` -------------------------------- ### Complex Data Generation and Plotting Example Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb A more elaborate example of generating and plotting data, potentially for more complex training scenarios. ```python import numpy as np import matplotlib.pyplot as plt def generate_complex_data(num_points=100): x = np.linspace(0, 10, num_points) y1 = np.sin(x) * np.exp(-x/5) + np.random.normal(0, 0.1, num_points) y2 = np.cos(x) * np.exp(-x/5) + np.random.normal(0, 0.1, num_points) return x, y1, y2 def plot_complex_curves(x, y1, y2): plt.figure(figsize=(12, 7)) plt.plot(x, y1, label='Decaying Sine', color='blue', linestyle='--') plt.plot(x, y2, label='Decaying Cosine', color='red', linestyle='-.') plt.xlabel('Time') plt.ylabel('Amplitude') plt.title('Complex Decaying Curves') plt.legend() plt.grid(True) plt.show() x_complex, y1_complex, y2_complex = generate_complex_data() plot_complex_curves(x_complex, y1_complex, y2_complex) ``` -------------------------------- ### Example: Add Hyperparameter Summary Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a summary of hyperparameters to TensorBoard. Useful for comparing different training runs with varying configurations. ```python writer.add_hparams({'lr': 0.01, 'batch_size': 32}, {'accuracy': 0.9, 'loss': 0.1}) ``` -------------------------------- ### Example: Add Custom Audio Summary Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs custom audio data to TensorBoard. Useful for tasks involving custom audio generation or analysis. ```python writer.add_audio('Custom/Signal', custom_audio_tensor, global_step=current_step, sample_rate=44100) ``` -------------------------------- ### Example of Data Generation and Plotting Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Combines data generation and plotting into a single executable block for demonstration purposes. ```python import numpy as np import matplotlib.pyplot as plt def generate_random_data(num_points=100): x = np.linspace(0, 10, num_points) y1 = np.sin(x) + np.random.normal(0, 0.2, num_points) y2 = np.cos(x) + np.random.normal(0, 0.2, num_points) return x, y1, y2 def plot_curves(x, y1, y2): plt.figure(figsize=(10, 6)) plt.plot(x, y1, label='Metric 1') plt.plot(x, y2, label='Metric 2') plt.xlabel('Epoch') plt.ylabel('Value') plt.title('Training Curves') plt.legend() plt.grid(True) plt.show() x, y1, y2 = generate_random_data() plot_curves(x, y1, y2) ``` -------------------------------- ### Example: Add Custom Video Summary Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs custom video data to TensorBoard. Applicable for tasks involving custom video generation or processing. ```python writer.add_video('Custom/Reconstruction', custom_video_tensor, global_step=current_step, fps=10) ``` -------------------------------- ### Example: Add Image to TensorBoard Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs an image to TensorBoard. Useful for visualizing sample inputs, outputs, or generated images during training. ```python writer.add_image('sample_image', img_tensor, global_step=epoch) ``` -------------------------------- ### Example: Add Custom Video Summary with Dataformats, Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom video with specified data format, tag, and global step. Useful for videos with different channel orders at different experiment stages. ```python writer.add_video('Custom/Tag/Video_CHW', custom_video_chw_tensor, global_step=step, fps=15, dataformats='CHW') ``` -------------------------------- ### Example: Add Custom Mesh Summary with Textures Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom mesh with texture coordinates and textures to TensorBoard. Enables visualization of 3D objects with detailed surface appearance. ```python writer.add_mesh('Custom/Textured_Object', vertices=custom_verts, faces=custom_faces, textures=custom_textures, global_step=step) ``` -------------------------------- ### Example: Add Custom Audio Summary with Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs custom audio data with a specific tag and global step. Useful for tasks involving custom audio generation or analysis at different experiment stages. ```python writer.add_audio('Custom/Tag/Signal', custom_audio_tensor, global_step=step, sample_rate=44100) ``` -------------------------------- ### Example: Add Custom Image Summary with Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom image with a specific tag and global step. Useful for visualizing custom outputs or states at different stages of an experiment. ```python writer.add_image('Custom/Tag/Image', custom_image_tensor, global_step=step) ``` -------------------------------- ### Example: Add Custom Video Summary with Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs custom video data with a specific tag and global step. Applicable for tasks involving custom video generation or processing at different experiment stages. ```python writer.add_video('Custom/Tag/Reconstruction', custom_video_tensor, global_step=step, fps=10) ``` -------------------------------- ### Example: Add Custom Mesh Summary with Colors Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom mesh with vertex colors to TensorBoard. Allows for visualizing 3D objects with color information. ```python writer.add_mesh('Custom/Colored_Object', vertices=custom_verts, faces=custom_faces, colors=custom_colors, global_step=step) ``` -------------------------------- ### Example: Add Histogram to TensorBoard Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs histogram data for weights or activations to TensorBoard. Helps in analyzing the distribution of model parameters. ```python writer.add_histogram('conv1.weight', model.conv1.weight, epoch) ``` -------------------------------- ### Example: Add Scalar with Walltime Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a scalar value with explicit wall time. Useful for precise timing analysis. ```python writer.add_scalar('Custom/Metric', value, global_step=step, walltime=timestamp) ``` -------------------------------- ### Example: Add Custom Video Summary with Dataformats Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom video summary to TensorBoard specifying the data format. Useful for videos with different channel orders. ```python writer.add_video('Custom/Video_CHW', custom_video_chw_tensor, global_step=step, fps=15, dataformats='CHW') ``` -------------------------------- ### Example: Add Custom Image Summary with Dataformats, Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom image with specified data format, tag, and global step. Useful for images with different channel orders at different experiment stages. ```python writer.add_image('Custom/Tag/Image_CHW', custom_image_chw_tensor, global_step=step, dataformats='CHW') ``` -------------------------------- ### Example: Add Mesh to TensorBoard Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs 3D mesh data to TensorBoard. Useful for visualizing 3D models or reconstructions. ```python writer.add_mesh('object_mesh', vertices=verts, faces=faces, global_step=epoch) ``` -------------------------------- ### Example: Add Custom Mesh Summary Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs custom 3D mesh data to TensorBoard. Useful for visualizing custom 3D objects or scenes. ```python writer.add_mesh('Custom/Object', vertices=custom_verts, faces=custom_faces, global_step=current_step) ``` -------------------------------- ### Example: Add Custom Mesh Summary with Colors, Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom mesh with vertex colors, tag, and global step. Allows for visualizing 3D objects with color information at different experiment stages. ```python writer.add_mesh('Custom/Tag/Colored_Object', vertices=custom_verts, faces=custom_faces, colors=custom_colors, global_step=step) ``` -------------------------------- ### Execute Cloud TPU Training Script Source: https://github.com/google/aqt/blob/main/papers/pokebnn/README.md Run the provided bash script to automate the creation of a TPU VM and the training of PokeBNN models. Ensure the gcloud CLI tool is installed and configured. ```bash source cloudtpu_train_pokebnn.sh ``` -------------------------------- ### Example: Add Hyperparameter Summary with HParam object and Metric Components Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a hyperparameter summary using HParam objects where metrics are detailed components. Enables granular performance analysis. ```python from torch.utils.tensorboard.summary import HParam writer.add_hparams([HParam('model_type', 'CNN'), HParam('num_layers', 3)], {'metrics/accuracy': 0.95, 'metrics/f1_score': 0.94}) ``` -------------------------------- ### Example: Add Custom Mesh Summary with Textures, Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom mesh with texture coordinates, textures, tag, and global step. Enables visualization of 3D objects with detailed surface appearance at different experiment stages. ```python writer.add_mesh('Custom/Tag/Textured_Object', vertices=custom_verts, faces=custom_faces, textures=custom_textures, global_step=step) ``` -------------------------------- ### Example: Add Custom Mesh Summary with Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs custom 3D mesh data with a specific tag and global step. Useful for visualizing custom 3D objects or scenes at different experiment stages. ```python writer.add_mesh('Custom/Tag/Object', vertices=custom_verts, faces=custom_faces, global_step=step) ``` -------------------------------- ### Example: Add Custom Histogram Summary Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom histogram summary to TensorBoard. Helps in analyzing the distribution of custom data or model parameters. ```python writer.add_histogram('Custom/Weights', custom_weights_tensor, global_step=current_step) ``` -------------------------------- ### Example: Add Custom Scalar Summary with Walltime Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom scalar value with explicit wall time. Useful for precise timing analysis of custom metrics. ```python writer.add_scalar('Custom/Timing', custom_time_value, global_step=step, walltime=custom_timestamp) ``` -------------------------------- ### Example: Add Custom Embedding Projector Data with Weights Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs custom embedding data with associated weights for visualization in the TensorBoard embedding projector. Useful for understanding the importance of different features. ```python writer.add_embedding(custom_features, metadata=custom_metadata, weight=custom_weights, global_step=step) ``` -------------------------------- ### Example: Add Scalar with Walltime and Display Name Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Combines explicit wall time and a custom display name for scalar logging. Provides detailed control over metric tracking. ```python writer.add_scalar('Performance', latency, step, walltime=time.time(), display_name='Inference Latency') ``` -------------------------------- ### Example: Add Hyperparameter Summary with Run Name Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a hyperparameter summary with a specific run name. Helps in organizing and identifying different experiments in TensorBoard. ```python writer.add_hparams({'lr': 0.001, 'optimizer': 'Adam'}, {'accuracy': 0.85, 'loss': 0.15}, run_name='exp_adam_lr0001') ``` -------------------------------- ### Example: Add Custom Text Summary with Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs custom text data with a specific tag and global step. Can be used for logging experiment notes or debugging information at different stages. ```python writer.add_text('Custom/Tag/Notes', custom_text_log, global_step=step) ``` -------------------------------- ### Example: Add Embedding Projector Data Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs embedding data for visualization in the TensorBoard embedding projector. Requires embedding vectors and optionally labels/metadata. ```python writer.add_embedding(features, metadata=labels, label_img=images, global_step=epoch) ``` -------------------------------- ### Example: Add Hyperparameter Summary with HParam object and Run Name Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a hyperparameter summary using HParam objects and a specific run name for better organization and identification of experiments. ```python from torch.utils.tensorboard.summary import HParam writer.add_hparams([HParam('dropout', 0.5), HParam('activation', 'relu')], {'accuracy': 0.75, 'loss': 0.25}, run_name='exp_dropout05_relu') ``` -------------------------------- ### Example of Logging Training Curve Data Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb This snippet demonstrates how to log training and validation loss and accuracy over epochs using TensorBoard's SummaryWriter. It requires a PyTorch model and a SummaryWriter instance. ```python from torch.utils.tensorboard import SummaryWriter import torch # Assume model, train_loader, val_loader, criterion, optimizer are defined writer = SummaryWriter() for epoch in range(num_epochs): # Training loop model.train() running_loss = 0.0 for i, data in enumerate(train_loader, 0): inputs, labels = data optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() running_loss += loss.item() avg_train_loss = running_loss / len(train_loader) writer.add_scalar('Loss/train', avg_train_loss, epoch) # Validation loop model.eval() val_loss = 0.0 correct = 0 total = 0 with torch.no_grad(): for data in val_loader: inputs, labels = data outputs = model(inputs) loss = criterion(outputs, labels) val_loss += loss.item() _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() avg_val_loss = val_loss / len(val_loader) val_accuracy = 100 * correct / total writer.add_scalar('Loss/val', avg_val_loss, epoch) writer.add_scalar('Accuracy/val', val_accuracy, epoch) writer.close() ``` -------------------------------- ### Example: Add Custom Image Summary Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom image summary to TensorBoard. Useful for visualizing specific aspects of the model's output or internal states. ```python writer.add_image('Custom/Output', custom_image_tensor, global_step=current_step) ``` -------------------------------- ### Example: Add Custom Embedding Projector Data Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs custom embedding data for visualization in the TensorBoard embedding projector. Allows for visualizing custom feature representations. ```python writer.add_embedding(custom_features, metadata=custom_metadata, label_img=custom_images, global_step=current_step) ``` -------------------------------- ### Example: Add Custom Scalar Summary with Walltime and Display Name Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Combines explicit wall time and a custom display name for custom scalar logging. Provides detailed control over custom metric tracking. ```python writer.add_scalar('Custom Performance', custom_latency, step, walltime=time.time(), display_name='Custom Inference Latency') ``` -------------------------------- ### Example: Add Hyperparameter Summary with HParam object, Run Name, and Metric Components Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a comprehensive hyperparameter summary using HParam objects, including a run name and detailed metric components for advanced experiment tracking. ```python from torch.utils.tensorboard.summary import HParam writer.add_hparams([HParam('learning_rate', 0.005), HParam('batch_size', 128)], {'metrics/accuracy': 0.89, 'metrics/precision': 0.88, 'metrics/recall': 0.90}, run_name='exp_lr0005_bs128') ``` -------------------------------- ### Example: Add Custom HParams Summary with Metric Components Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom hyperparameter summary with detailed metric components. Allows for granular analysis of custom experiment performance. ```python writer.add_hparams({'feature_set': 'A'}, {'metrics/custom_accuracy': 0.8, 'metrics/custom_error': 0.2}) ``` -------------------------------- ### Example: Add Custom HParams Summary with HParam object, Metric Components, and Weights Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom hyperparameter summary using HParam objects with detailed metric components and weights. Enables granular, weighted performance analysis of custom experiments. ```python from torch.utils.tensorboard.summary import HParam writer.add_hparams([HParam('custom_feature_set', 'B')], {'metrics/custom_precision_w': 0.88, 'metrics/custom_recall_w': 0.82}) ``` -------------------------------- ### Example: Add Custom Image Summary with Dataformats Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom image summary to TensorBoard specifying the data format. Useful for images with different channel orders (e.g., CHW vs HWC). ```python writer.add_image('Custom/Image_CHW', custom_image_chw_tensor, global_step=step, dataformats='CHW') ``` -------------------------------- ### Example: Add Custom Histogram Summary with Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom histogram with a specific tag and global step. Helps in analyzing the distribution of custom data or parameters at different experiment stages. ```python writer.add_histogram('Custom/Tag/Weights', custom_weights_tensor, global_step=step) ``` -------------------------------- ### Example: Add Custom HParams Summary with HParam object and Weights Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom hyperparameter summary using HParam objects, including weights for metrics. Allows for weighted performance evaluation. ```python from torch.utils.tensorboard.summary import HParam writer.add_hparams([HParam('custom_weight_param', 0.8)], {'metrics/weighted_accuracy': 0.9, 'metrics/weighted_loss': 0.1}) ``` -------------------------------- ### Example: Add Custom Scalar Summary with Walltime, Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom scalar value with explicit wall time, tag, and global step. Useful for precise timing analysis of custom metrics at specific experiment stages. ```python writer.add_scalar('Custom/Tag/Timing', custom_time_value, global_step=step, walltime=custom_timestamp) ``` -------------------------------- ### Example: Add Custom HParams Summary with Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom hyperparameter summary with a specific tag and global step. Useful for tracking custom configurations and their performance at different experiment stages. ```python writer.add_hparams({'custom_param': 'value'}, {'custom_metric': 0.5}, run_name='custom_run_tag', global_step=step) ``` -------------------------------- ### Example: Add Custom HParams Summary with HParam object, Run Name, and Weights Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a comprehensive custom hyperparameter summary using HParam objects, including a run name and weights for metrics, for advanced custom experiment tracking. ```python from torch.utils.tensorboard.summary import HParam writer.add_hparams([HParam('custom_config_variant', 'B')], {'metrics/custom_score_weighted': 0.75}, run_name='custom_variant_B') ``` -------------------------------- ### Example: Add Custom Embedding Projector Data with Label Images Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs custom embedding data with corresponding label images for visualization in the TensorBoard embedding projector. Helps in visually associating embeddings with their labels. ```python writer.add_embedding(custom_features, metadata=custom_metadata, label_img=custom_label_images, global_step=step) ``` -------------------------------- ### Example: Add Custom HParams Summary with HParam object, Weights, Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom hyperparameter summary using HParam objects, including weights, tag, and global step for weighted performance evaluation of custom experiments. ```python from torch.utils.tensorboard.summary import HParam writer.add_hparams([HParam('custom_weight_param', 0.8)], {'metrics/weighted_accuracy': 0.9, 'metrics/weighted_loss': 0.1}, run_name='custom_weighted_tag', global_step=step) ``` -------------------------------- ### Example: Add Custom HParams Summary with HParam object and Metric Components Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom hyperparameter summary using HParam objects with detailed metric components. Enables granular performance analysis of custom experiments. ```python from torch.utils.tensorboard.summary import HParam writer.add_hparams([HParam('custom_dataset', 'set1')], {'metrics/custom_precision': 0.85, 'metrics/custom_recall': 0.80}) ``` -------------------------------- ### Initialize TensorBoard Writer Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Sets up the TensorBoard writer to log training metrics. Ensure the log directory is correctly specified. ```python from torch.utils.tensorboard import SummaryWriter writer = SummaryWriter(log_dir="runs/pokebnn_demo") ``` -------------------------------- ### Example: Add Custom Scalar Summary with Display Name, Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom scalar value with a custom display name, tag, and global step. Allows for more descriptive labels for custom metrics at specific experiment stages. ```python writer.add_scalar('Custom Metrics', custom_accuracy, epoch, display_name='Custom Training Accuracy', tag='Custom/Tag/Accuracy') ``` -------------------------------- ### Low-level Tensor Quantization with `Quantizer` Source: https://context7.com/google/aqt/llms.txt Demonstrates creating and using a `Quantizer` for low-level tensor quantization, including calibration and fake quantization. Requires AQT numerics and calibration modules. ```python import jax import jax.numpy as jnp from aqt.jax.v2 import aqt_quantizer, calibration, utils as aqt_utils from aqt.jax.v2.numerics import int_numerics # Build a 4-bit symmetric quantizer with AbsMax calibration q = aqt_quantizer.Quantizer( numerics=int_numerics.IntSymmetric( bits=4, preserve_zero=True, preserve_max_val=True, clip=True, clip_gradient=True, round=True, noise_fn=None, ), calib_shared_axes=-1, # share scale across last axis scale_stop_grad=True, calibration=calibration.AbsMaxCalibration, context=aqt_utils.Context(key=jax.random.PRNGKey(0), train_step=0), ) q.init_calibration() # must call before quant() x = jnp.linspace(-10.0, 10.0, 10) x_q, grad_fn = q.quant(x, calibration_axes=-1) print("quantized ints:", x_q.qvalue) # e.g., [-7, -6, -4, -3, -1, 0, 2, 3, 5, 6] (4-bit range ±7) print("scale:", x_q.scale) # e.g., [1.428...] (10.0 / 7.0) print("dequantized:", x_q.dequant()) # e.g., [-9.999..., -8.571..., ..., 9.999...] (close to original) # Fake-quant helper: quantize then immediately dequantize fake_quant = aqt_quantizer.make_fake_quant(q, calibration_axes=-1) print("fake quant:", fake_quant(x)) # Convenience factory (8-bit, AbsMax calibration) q8 = aqt_quantizer.quantizer_make(n_bits=8) x_q8, _ = q8.quant(x, calibration_axes=-1) print("8-bit qvalue:", x_q8.qvalue) ``` -------------------------------- ### TensorBoard Integration for PokeBNN Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Code snippet demonstrating how to log training metrics to TensorBoard for visualization. Ensure TensorFlow is installed. ```python from tensorboard.summary import SummaryWriter log_dir = "logs/pokebnn_demo" writer = SummaryWriter(log_dir) # Simulate training loop and log metrics for step in range(n_steps): # In a real scenario, these would be actual training metrics current_loss = np.mean(losses[:, step]) current_accuracy = np.mean(accuracies[:, step]) writer.add_scalar('Loss/train', current_loss, step) writer.add_scalar('Accuracy/train', current_accuracy, step) writer.close() print(f"TensorBoard logs written to: {log_dir}") ``` -------------------------------- ### Initializing PokeBNN Model and Trainer Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Initializes the PokeBNN model, defines the optimizer and loss function, and sets up the Trainer. This prepares the model for the training process. ```python model = PokeBNN(input_size=X_train.shape[1], hidden_size=128, num_classes=len(dataset.classes)) optimizer = optim.Adam(model.parameters(), lr=0.001) loss_fn = nn.CrossEntropyLoss() trainer = Trainer(model, optimizer, loss_fn, device='cpu') writer = SummaryWriter('runs/pokebnn_demo') ``` -------------------------------- ### Example: Add Figure to TensorBoard Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a Matplotlib figure to TensorBoard. Useful for visualizing plots or complex data representations. ```python writer.add_figure('training_curve', fig, epoch) ``` -------------------------------- ### Example: Add Custom HParams Summary Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom hyperparameter summary to TensorBoard. Useful for tracking custom configurations and their performance. ```python writer.add_hparams({'custom_param': 'value'}, {'custom_metric': 0.5}) ``` -------------------------------- ### Example: Add PR Curve to TensorBoard Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs Precision-Recall curve data to TensorBoard. Useful for evaluating binary classification models. ```python writer.add_pr_curve('pr_curve_example', predictions, labels, epoch) ``` -------------------------------- ### Example: Add Text to TensorBoard Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs text data to TensorBoard. Can be used for logging hyperparameters, model summaries, or other textual information. ```python writer.add_text('model_hyperparameters', hparams_str, epoch) ``` -------------------------------- ### Example: Add Custom Figure Summary Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom Matplotlib figure to TensorBoard. Useful for creating and visualizing custom plots or diagrams. ```python writer.add_figure('Custom/Plot', custom_figure, global_step=current_step) ``` -------------------------------- ### Example: Add Hyperparameter Summary with HParam object Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a hyperparameter summary using the HParam object for more structured definition of hyperparameters and metrics. ```python from torch.utils.tensorboard.summary import HParam writer.add_hparams([HParam('learning_rate', 0.01), HParam('batch_size', 64)], {'accuracy': 0.88, 'loss': 0.12}) ``` -------------------------------- ### Example: Add Custom Scalar Summary with Walltime, Display Name, Tag, Global Step and Walltime Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Combines explicit wall time, custom display name, tag, and global step for custom scalar logging. Provides detailed control over custom metric tracking at specific experiment stages. ```python writer.add_scalar('Custom Performance', custom_latency, step, walltime=time.time(), display_name='Custom Inference Latency', tag='Custom/Tag/Latency', walltime=custom_timestamp) ``` -------------------------------- ### Example: Add Scalar with Display Name Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a scalar value with a custom display name in TensorBoard. Allows for more descriptive labels in the UI. ```python writer.add_scalar('Metrics', accuracy, epoch, display_name='Training Accuracy') ``` -------------------------------- ### Import Necessary Libraries Source: https://github.com/google/aqt/blob/main/aqt/jax/v2/examples/examples.ipynb Imports the required modules from AQT, Flax, and JAX for building and quantizing models. ```python # necessary imports import aqt.jax.v2.flax.aqt_flax as aqt import aqt.jax.v2.config as aqt_config import flax.linen as nn ``` -------------------------------- ### Example: Add Custom Scalar Summary with Walltime, Display Name, Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Combines explicit wall time, custom display name, tag, and global step for custom scalar logging. Provides detailed control over custom metric tracking at specific experiment stages. ```python writer.add_scalar('Custom Performance', custom_latency, step, walltime=time.time(), display_name='Custom Inference Latency', tag='Custom/Tag/Latency') ``` -------------------------------- ### Create Fully Quantized Configuration (int8) Source: https://github.com/google/aqt/blob/main/README.md Creates an AQT configuration object that quantizes both the forward and backward passes of a model to 8-bit integers. This is a common setting for achieving good performance and accuracy. ```python # create a config that quantizes both forward and backward passes to int8 int8_config = aqt_config.fully_quantized(fwd_bits=8, bwd_bits=8) ``` -------------------------------- ### Example: Add Custom HParams Summary with Run Name Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom hyperparameter summary with a specific run name. Helps in organizing custom experiments. ```python writer.add_hparams({'custom_setting': 10}, {'custom_result': 0.7}, run_name='custom_run_10') ``` -------------------------------- ### Example: Add Custom ROC Curve Summary Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom ROC curve to TensorBoard. Useful for evaluating custom classification models or specific scenarios. ```python writer.add_roc_curve('Custom/ROC_Curve', custom_predictions, custom_labels, global_step=current_step) ``` -------------------------------- ### Example: Add Custom PR Curve Summary Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom Precision-Recall curve to TensorBoard. Useful for evaluating custom classification models or specific scenarios. ```python writer.add_pr_curve('Custom/PR_Curve', custom_predictions, custom_labels, global_step=current_step) ``` -------------------------------- ### Example: Add Custom Text Summary Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs custom text data to TensorBoard. Can be used for logging experiment notes, debugging information, or custom messages. ```python writer.add_text('Custom/Notes', custom_text_log, global_step=current_step) ``` -------------------------------- ### Example: Add Custom Scalar Summary Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom scalar summary to TensorBoard. This allows for logging any numerical value associated with a specific step or epoch. ```python writer.add_scalar('Custom/Metric', custom_value, global_step=current_step) ``` -------------------------------- ### Generate Random Matrix Data Source: https://github.com/google/aqt/blob/main/README.md Generates random matrices for activations and weights, used as input for matrix multiplication examples. Ensure `gen_matrix` is defined elsewhere. ```python batch_size = 3 channels_in = 4 channels_out = 5 a = gen_matrix(batch_size, channels_in) # Activations w = gen_matrix(channels_in, channels_out) # Weights ``` -------------------------------- ### Loading and Preparing Dataset Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Loads the PokeBNN dataset and splits it into training and testing sets. Data is then scaled for optimal model performance. ```python dataset = PokeBNNDataset(root='data', download=True) X = dataset.X y = dataset.y X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) ``` -------------------------------- ### Example: Add Custom HParams Summary with HParam object Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs a custom hyperparameter summary using HParam objects for structured definition of custom parameters and metrics. ```python from torch.utils.tensorboard.summary import HParam writer.add_hparams([HParam('custom_config', 'X')], {'custom_score': 0.6}) ``` -------------------------------- ### Example: Add Custom Embedding Projector Data with Tag and Global Step Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs custom embedding data with a specific tag and global step for visualization in the TensorBoard embedding projector. Allows for visualizing custom feature representations at different experiment stages. ```python writer.add_embedding(custom_features, metadata=custom_metadata, global_step=step, tag='Custom/Tag/Embeddings') ``` -------------------------------- ### Example: Add ROC Curve to TensorBoard Source: https://github.com/google/aqt/blob/main/papers/pokebnn/tensorboard.ipynb Logs Receiver Operating Characteristic (ROC) curve data to TensorBoard. Another metric for evaluating binary classification models. ```python writer.add_roc_curve('roc_curve_example', predictions, labels, epoch) ```