TensorBoard is a visualization and experiment-tracking tool for inspecting machine-learning training and active-learning behavior. In progressive molecular docking, it records metrics that show whether a surrogate model is becoming a useful proxy for expensive docking experiments. section 9.2.3
A TensorBoard dashboard can help you:
- Track model-performance metrics and convergence across training or active-learning iterations. section 9.2.3
- Inspect distribution shifts as the selected compound pool changes. section 9.2.3
- Compare hyperparameters, acquisition strategies, and other experimental factors. section 9.2.3 section 9.2.7
Progressive docking dashboard
A TensorBoard run for the 4AG8 docking experiment can display best docking score, top molecules found, and training loss. In one result, the best score approached -20.7 kcal/mol, the workflow found 73 of the top 100 molecules, and loss decreased from about 6 to 2 over 100 epochs. section 9.2.7
Listing 9.6 Training the Deep Learning Surrogate Model
def train_deep_model(
model, X_train, y_train, device, writer=None, global_step=0,
epochs=10, batch_size=32, lr=0.001, **kwargs
):
history = {"train_loss": []}
criterion = nn.MSELoss() #A
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
X_train_tensor = torch.tensor(
X_train, dtype=torch.float32
).to(device) #B
y_train_tensor = torch.tensor(
y_train, dtype=torch.float32
).view(-1, 1).to(device)
train_data = torch.utils.data.TensorDataset(
X_train_tensor, y_train_tensor
) #C
train_loader = torch.utils.data.DataLoader(
train_data, batch_size=batch_size, shuffle=True
)
for epoch in range(epochs):
model.train()
epoch_loss = 0.0
num_batches = 0
for inputs, labels in train_loader:
optimizer.zero_grad() #D
outputs = model(inputs) #E
loss = criterion(outputs, labels) #F
loss.backward() #G
optimizer.step()
epoch_loss += loss.item() #H
num_batches += 1
avg_epoch_loss = epoch_loss / num_batches
history["train_loss"].append(avg_epoch_loss)
if writer is not None: #I
writer.add_scalar(
'Training/Loss', avg_epoch_loss,
global_step*epochs + epoch
)
print(f"Epoch {epoch + 1}/{epochs}, Loss: {avg_epoch_loss:.4f}")
return historyThe training implementation accepts an optional writer and can log metrics while mini-batch PyTorch training updates the surrogate model. section 9.2.3
Listing 9.10 Active Learning Loop Implementation
def active_learning_loop(X_pool, smiles_pool, n_initial_samples,
n_iterations, budget_per_iteration, device,
reference_df=None, receptor_file=None,
top_reference_smiles=[],
initial_selection_method="random",
oracle=deepdock_oracle,
box=None, acquisition_function="greedy", xi=0.01,
log_dir="tensorboard_logs"):
if device is None:
device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu"
)
writer = initialize_tensorboard(log_dir) #A
initial_indices = initialize_samples(
X_pool, initial_selection_method, n_initial_samples
) #B
X_train = X_pool[initial_indices]
smiles_train = [smiles_pool[i] for i in initial_indices]
y_train = oracle(
smiles_train, box=box, idx_list=initial_indices,
reference_df=reference_df, receptor_file=receptor_file
) #C
best_score = min(y_train) #D
top_molecules_found = calculate_top_molecules_captured(
smiles_train, top_reference_smiles
)
writer.add_scalar(
'Performance/TopMoleculesFound', top_molecules_found, 0
)
writer.add_scalar('Performance/BestScore', best_score, 0)
print(f"Selected {len(X_train)} initial samples. "
f"Found {top_molecules_found} top molecules.")
# Remove initial samples from the pool
mask = np.ones(len(X_pool), dtype=bool) #E
mask[initial_indices] = False
X_pool = X_pool[mask]
smiles_pool = [s for i, s in enumerate(smiles_pool) if mask[i]]
model = DeepDockingModel(
input_size=X_train.shape[1], hidden_size=512, output_size=1
).to(device) #F
train_history = train_deep_model(
model, X_train, y_train, device, writer, global_step=0,
epochs=10, batch_size=32
)
results = []
for iteration in range(n_iterations): #G
print(f"\nStarting iteration {iteration + 1}/{n_iterations}")
print(f"Selecting {budget_per_iteration} molecules "
f"using {acquisition_function} acquisition...")
start_time = time.time()
best_indices, best_smiles = select_next_molecule(
model, X_pool, smiles_pool, device, best_score,
batch_size=budget_per_iteration,
acquisition_function=acquisition_function, xi=xi,
)
iteration_time = time.time() - start_time
print("Docking selected molecules...")
true_scores = oracle(
best_smiles, box=box, idx_list=best_indices,
reference_df=reference_df, receptor_file=receptor_file
)
best_score = min(best_score, min(true_scores))
selected_smiles_iter = best_smiles #H
selected_scores_iter = true_scores
# Capture selected fingerprints before shrinking pool
X_new = X_pool[best_indices].copy()
# Update training data
smiles_train.extend(selected_smiles_iter)
y_train = np.concatenate([
y_train, np.asarray(selected_scores_iter, dtype=float)
])
X_train = np.vstack([X_train, X_new])
# Update pool (remove selected molecules)
mask = np.ones(len(X_pool), dtype=bool) #I
mask[best_indices] = False
X_pool = X_pool[mask]
smiles_pool = [s for i, s in enumerate(smiles_pool) if mask[i]]
top_molecules_found = calculate_top_molecules_captured(
smiles_train, top_reference_smiles
) #J
writer.add_scalar(
'Performance/TopMoleculesFound', top_molecules_found, iteration
)
writer.add_scalar('Performance/BestScore', best_score, iteration)
print(f"Selected {len(smiles_train)} initial samples. "
f"Found {top_molecules_found} top molecules.")
results.append({
'iteration': iteration + 1,
'total_molecules': len(smiles_train),
'top_molecules_found': top_molecules_found,
'best_score': best_score,
'time_per_iteration': iteration_time
})
print("Retraining surrogate model...") #K
train_deep_model(
model, X_train, y_train, device, writer, iteration + 1
)
plot_chemical_space(
writer, X_pool, X_train, iteration + 1
) #L
if len(smiles_pool) == 0:
print("Molecule pool is empty. "
"Terminating the active learning loop.")
break
writer.close()
return pd.DataFrame(results)The active-learning implementation initializes a TensorBoard writer, logs progress during selection and docking, and supports later inspection of iteration-based plots. section 9.2.7


