Experimentation tracking is a critical aspect of machine learning development, enabling practitioners to manage and track the progress of their experiments efficiently. Experimentation tracking involves recording and monitoring various factors, such as model performance, hyperparameters, and training data, to gain insights into the performance of the machine learning model.
Managing experiments and tracking results can be a challenging task for machine learning practitioners due to the complexity of the models, the large amounts of data involved, and the variety of tools and frameworks used in machine learning development. Here are some of the key challenges faced by practitioners in managing experiments and tracking results:
Weights & Biases (W&B) is a platform designed to help machine learning practitioners manage and track their experiments effectively. It provides a suite of tools for experiment tracking, visualization, and collaboration, making it easier for practitioners to develop robust and scalable machine learning models.
The main features and benefits of the W&B platform are:
Setting up W&B for tracking experiments involves several steps:
wandb.init()
wandb.log()
wandb.config
Here's an example of how to log metrics, models, hyperparameters, and visualizations using W&B:
import wandbimport numpy as npimport matplotlib.pyplot as pltimport tensorflow as tf# Step 1: Initialize W&Bwandb.init(project="my-project")# Step 2: Log hyperparametersconfig = { "learning_rate": 0.001, "batch_size": 32, "epochs": 10}wandb.config.update(config)# Step 3: Train model and log metricsmodel = tf.keras.models.Sequential([ tf.keras.layers.Dense(10, activation='relu', input_shape=(10,)), tf.keras.layers.Dense(1)])model.compile(optimizer=tf.keras.optimizers.Adam(config['learning_rate']), loss=tf.keras.losses.MeanSquaredError(), metrics=[tf.keras.metrics.MeanAbsoluteError()])for epoch in range(config['epochs']): # ... train model ... history = model.fit(x_train, y_train, batch_size=config['batch_size']) loss = history.history['loss'][-1] mae = history.history['mean_absolute_error'][-1] wandb.log({"epoch": epoch, "loss": loss, "mae": mae})# Step 4: Log visualizations and modelx = np.linspace(0, 10, 100)y = np.sin(x)fig, ax = plt.subplots()ax.plot(x, y)wandb.log({"my_plot": wandb.Image(fig)})wandb.save('my_model.h5')# Step 5: Finish runwandb.finish()
c
TrueFoundry integrates seamlessly with Weights & Biases Model Registry to allow you to deploy models logged in Weights & Biases to your Kubernetes Cluster using TrueFoundry.
📌
Aside from using Weights & Biases for Model management and Experimentation tracking, you can also use Truefoundry's Experimentation Tracking MLFoundy.
You can also follow along with the code below via the following colab notebook:Colab Notebook
Step 1 - Install and setup:
pip install wandb -qU
import wandbwandb.login()
pip install -U "servicefoundry"
sfy login
Step 2 - Train and log the model:
We will first use wandb.init() to intialize the projectThen we will train our ML model and save it as a joblib fileThen we will save the our joblib file to wandb via wandb.save()
wandb.save()
import wandbfrom sklearn.datasets import load_irisfrom sklearn.model_selection import train_test_splitfrom sklearn.linear_model import LogisticRegressionfrom sklearn.metrics import classification_reportimport joblib# Initialize W&Bwandb.init(project="iris-logistic-regression")# Load and preprocess the dataX, y = load_iris(as_frame=True, return_X_y=True)X = X.rename(columns={ "sepal length (cm)": "sepal_length", "sepal width (cm)": "sepal_width", "petal length (cm)": "petal_length", "petal width (cm)": "petal_width",})X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42, stratify=y)# Initialize the modelclf = LogisticRegression(solver="liblinear")# Fit the modelclf.fit(X_train, y_train)# Evaluate the modelpreds = clf.predict(X_test)# Log the model and the evaluation metrics to W&Bjoblib.dump(clf, "model.joblib")wandb.save("model.joblib")# Finish the runwandb.finish()
Step 3 - Create an inference application and dependecy file:
We will have to create two files to deploy on truefoundry, a app.py file that contains our application code, and a requirements.txt file that contains our dependencies.
app.py
requirements.txt
.├── app.py├── deploy.py └── requirements.txt
wandb.restore()
<name_of_saved_file>
"username/project/run_id"
wandb.restore("model.joblib", "adityajha-tfy/iris-logistic-regression/00r5xvyv")
import osimport wandbimport joblibimport pandas as pdfrom fastapi import FastAPIwandb.login(key=os.environ["WANDB_API_KEY"])# Retrieve the model from W&Bmodel_joblib = wandb.restore( 'model.joblib', run_path="adityajha-tfy/iris-logistic-regression/00r5xvyy", )model = joblib.load(model_joblib.name)# Load the modelapp = FastAPI(root_path=os.getenv("TFY_SERVICE_ROOT_PATH"))@app.post("/predict")def predict( sepal_length: float, sepal_width: float, petal_length: float, petal_width: float): data = dict( sepal_length=sepal_length, sepal_width=sepal_width, petal_length=petal_length, petal_width=petal_width, ) prediction = int(model.predict(pd.DataFrame([data]))[0]) return {"prediction": prediction}
fastapijoblibnumpypandasscikit-learnuvicornwandb
Step 4 - Use the truefoundry's python sdk and configure the deployment
import argparseimport loggingfrom servicefoundry import Build, PythonBuild, Service, Resources, Port# Setup the loggerlogging.basicConfig(level=logging.INFO)# Setup the argument parserparser = argparse.ArgumentParser()parser.add_argument("--workspace_fqn", required=True, type=str)parser.add_argument("--wandb_api_key", required=True, type=str)args = parser.parse_args()service = Service( name="fastapi", image=Build( build_spec=PythonBuild( command="uvicorn app:app --port 8000 --host 0.0.0.0", requirements_path="requirements.txt", ) ), ports=[ Port( port=8000, host="ml-deploy-aditya-ws-8000.demo.truefoundry.com", ) ], resources=Resources( cpu_request=0.25, cpu_limit=0.5, memory_request=200, memory_limit=400, ephemeral_storage_request=200, ephemeral_storage_limit=400, ), env={ "WANDB_API_KEY": args.wandb_api_key })service.deploy(workspace_fqn=args.workspace_fqn)
Step 5 - Deploy your Service via Truefoundry
Run the following command and pass in your
python deploy.py --workspace_fqn "<Your Workspace FQN>" --wandb_api_key "<Your Wandb API Key>"
And voila!!! In the logs you can find your deployed service's dashboard. And then on top right corner you will find your deployed applications endpoint.
TrueFoundry is a ML Deployment PaaS over Kubernetes to speed up developer workflows while allowing them full flexibility in testing and deploying models while ensuring full security and control for the Infra team. Through our platform, we enable Machine learning Teams to deploy and monitor models in 15 minutes with 100% reliability, scalability, and the ability to roll back in seconds - allowing them to save cost and release Models to production faster, enabling real business value realisation.
Join AI/ML leaders for the latest on product, community, and GenAI developments