Neptune.ai Experiment Tracking
Neptune.ai Experiment Tracking provides a centralized platform for logging, organizing, visualizing, and comparing machine learning experiments. It addresses the challenges of reproducibility, collaboration, and efficient iteration inherent in ML development by offering a robust system to track every aspect of an experiment. This capability ensures that developers can systematically manage their work, understand model behavior, and accelerate the path from experimentation to production.
Core Capabilities
Neptune.ai offers a comprehensive suite of features designed to streamline the ML development lifecycle.
Logging Experiment Metadata
Neptune.ai enables the logging of diverse metadata associated with each experiment run. This includes:
- Parameters: Track hyperparameters, model configurations, and dataset versions.
- Metrics: Log scalar metrics (e.g., loss, accuracy, F1-score) over time, allowing for real-time monitoring and post-hoc analysis.
- Artifacts: Store models, datasets, plots, images, and other files generated during a run. This ensures all relevant outputs are linked directly to the experiment that produced them.
- Code and Environment: Automatically captures the source code, Git commit information, installed dependencies, and system environment details. This is crucial for ensuring reproducibility.
- Text and Rich Media: Log notes, debug messages, and interactive plots (e.g., Plotly, Matplotlib figures) directly within the run context.
Logging data is straightforward. For instance, to log a metric and a parameter:
import neptune
# Initialize a new run
run = neptune.init_run(project="my-team/my-project", api_token="YOUR_API_TOKEN")
# Log parameters
run["hyperparameters/learning_rate"] = 0.001
run["hyperparameters/epochs"] = 10
# Log metrics during training
for epoch in range(10):
# ... train model ...
current_loss = 0.1 / (epoch + 1) # Example loss
current_accuracy = 0.9 + (epoch * 0.01) # Example accuracy
run["metrics/loss"].log(current_loss)
run["metrics/accuracy"].log(current_accuracy)
# Stop the run
run.stop()
Visualizing and Comparing Runs
The Neptune.ai UI provides interactive dashboards to visualize logged data. Developers can:
- Monitor Training Progress: View real-time plots of metrics like loss and accuracy.
- Compare Multiple Runs: Overlay plots from different experiments to quickly identify the best performing models or parameter configurations.
- Analyze Artifacts: Browse and download logged models, datasets, and generated plots directly from the UI.
- Filter and Sort: Easily navigate through hundreds or thousands of runs using tags, parameters, and metric values.
Ensuring Reproducibility
A key aspect of Neptune.ai is its focus on reproducibility. By automatically logging code snapshots, Git commit hashes, and environment dependencies, it provides a complete record of how an experiment was conducted. This allows any team member to recreate the exact conditions of a past run, facilitating debugging, validation, and knowledge transfer.
Facilitating Collaboration
Neptune.ai supports team collaboration by allowing multiple users to work within the same project. Features include:
- Shared Dashboards: Teams can share and view each other's experiments and dashboards.
- Commenting: Add comments to specific runs or logged data points for discussion and feedback.
- Role-Based Access Control: Manage permissions for different team members.
Common Use Cases
Neptune.ai is invaluable across various stages of the ML workflow.
- Hyperparameter Optimization: Track hundreds of hyperparameter tuning runs, compare their performance, and identify optimal configurations efficiently.
- Model Versioning and Comparison: Log different model architectures, training strategies, and their resulting performance metrics. This enables systematic comparison and version control for models.
- Debugging and Iteration: Quickly pinpoint issues in training runs by examining logged metrics, parameters, and artifacts. The detailed history helps in understanding why a model failed or performed unexpectedly.
- Team Collaboration on ML Projects: Provides a single source of truth for all experiments, allowing data scientists and engineers to share findings, review results, and collaborate effectively without manual data aggregation.
- Research and Development: Document and organize exploratory experiments, ensuring that all findings, even negative ones, are recorded and accessible for future reference.
Integrating Neptune.ai into Your Workflow
Integrating Neptune.ai typically involves initializing a run, logging relevant data, and stopping the run.
Initializing an Experiment Run
Every interaction with Neptune.ai occurs within the context of a "run." A run represents a single execution of your training script, a hyperparameter sweep, or any other ML activity you want to track.
import neptune
# Initialize a run. This creates a new entry in your Neptune.ai project.
# The project name should be in the format "workspace-name/project-name".
# The API token is required for authentication.
run = neptune.init_run(
project="my-team/my-project",
api_token="YOUR_API_TOKEN",
name="My First Training Run", # Optional: give your run a descriptive name
tags=["baseline", "pytorch", "classification"] # Optional: add tags for easy filtering
)
# Your ML code goes here
# ...
run.stop() # Don't forget to stop the run when done
It is best practice to use a with statement for run initialization to ensure the run is stopped automatically, even if errors occur:
import neptune
with neptune.init_run(project="my-team/my-project", api_token="YOUR_API_TOKEN") as run:
run["hyperparameters/learning_rate"] = 0.001
# ... your training code ...
run["metrics/accuracy"].log(0.95)
Logging Parameters and Metrics
Parameters are typically logged once at the beginning of a run, while metrics are logged iteratively during training.
import neptune
import random
with neptune.init_run(project="my-team/my-project", api_token="YOUR_API_TOKEN") as run:
# Log parameters
params = {
"learning_rate": 0.001,
"optimizer": "Adam",
"epochs": 5,
"batch_size": 32
}
run["parameters"] = params # Log a dictionary of parameters
# Simulate training loop
for epoch in range(params["epochs"]):
# Simulate loss and accuracy
loss = 1.0 / (epoch + 1) + random.uniform(-0.1, 0.1)
accuracy = 0.5 + (epoch * 0.1) + random.uniform(-0.05, 0.05)
# Log metrics
run["metrics/loss"].log(loss)
run["metrics/accuracy"].log(accuracy)
print(f"Epoch {epoch+1}: Loss={loss:.4f}, Accuracy={accuracy:.4f}")
# Log a final metric
run["metrics/final_accuracy"] = accuracy
Managing Artifacts
Artifacts can be any file generated during your experiment, such as trained models, preprocessed datasets, or generated plots.
import neptune
import matplotlib.pyplot as plt
import numpy as np
# Assume 'model.pth' is a trained model file
# Assume 'data_sample.csv' is a processed dataset sample
with neptune.init_run(project="my-team/my-project", api_token="YOUR_API_TOKEN") as run:
# Upload a model file
run["model_checkpoints/best_model"].upload("model.pth")
# Upload a dataset sample
run["datasets/processed_sample"].upload("data_sample.csv")
# Log a Matplotlib figure
fig, ax = plt.subplots()
ax.plot(np.random.rand(10))
ax.set_title("Random Plot")
run["plots/random_data_plot"].upload(fig)
plt.close(fig) # Close the figure to free memory
Integrating with Machine Learning Frameworks
Neptune.ai provides integrations with popular ML frameworks like PyTorch, TensorFlow/Keras, Scikit-learn, and Hugging Face Transformers through dedicated callbacks or wrappers. These integrations simplify logging by automating the capture of common metrics, parameters, and model checkpoints.
For example, with Keras:
import neptune
from neptune.integrations.keras import NeptuneCallback
import tensorflow as tf
# Initialize Neptune run
with neptune.init_run(project="my-team/my-project", api_token="YOUR_API_TOKEN") as run:
# Define a simple Keras model
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation='relu', input_shape=(784,)),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Create a Neptune callback
neptune_callback = NeptuneCallback(run=run)
# Train the model with the callback
model.fit(
tf.random.normal((100, 784)), tf.random.uniform((100,), maxval=10, dtype=tf.int32),
epochs=5,
callbacks=[neptune_callback]
)
Similar callbacks exist for PyTorch Lightning, Hugging Face Trainer, and other frameworks, significantly reducing the boilerplate code required for logging.
Stopping a Run
It is crucial to stop a run explicitly using run.stop() or by using the with neptune.init_run(...) as run: context manager. Failing to stop a run can lead to incomplete data being logged or the run remaining active indefinitely in the Neptune.ai UI.
Important Considerations and Best Practices
- Performance Overhead: While Neptune.ai is optimized for minimal impact, logging a very high frequency of data points or extremely large artifacts can introduce some overhead. Batching metric logs or logging less frequently for very short-lived operations can mitigate this.
- Structured Logging: Organize your logged data using a hierarchical structure (e.g.,
run["metrics/train/loss"],run["hyperparameters/model_config/learning_rate"]). This makes navigation and comparison in the UI much easier. - Run Naming and Tags: Use descriptive names for your runs and apply relevant tags (e.g.,
["production_candidate", "resnet50", "gpu_training"]). This helps in filtering and finding specific experiments later. - API Token Security: Never hardcode your Neptune.ai API token directly in your code. Use environment variables (
NEPTUNE_API_TOKEN) or a configuration management system. - Offline Mode: For environments with intermittent internet access or strict network policies, Neptune.ai supports an offline mode where data is stored locally and synced later. Initialize with
mode="offline". - Resource Management: Ensure that runs are properly stopped. Unstopped runs consume resources and can clutter your project view. The
withstatement is the recommended approach.