GithubHelp home page GithubHelp logo

wandb / edu Goto Github PK

View Code? Open in Web Editor NEW
470.0 470.0 232.0 23.94 MB

Educational materials on deep learning by Weights & Biases

Home Page: http://wandb.ai

License: GNU General Public License v2.0

Python 9.56% Jupyter Notebook 90.29% HTML 0.05% JavaScript 0.05% Shell 0.02% Dockerfile 0.03%

edu's Introduction

Weights & Biases Weights & Biases

Use W&B to build better models faster. Track and visualize all the pieces of your machine learning pipeline, from datasets to production machine learning models. Get started with W&B today, sign up for a free account!


Building an LLM app? Track, debug, evaluate, and monitor LLM apps with Weave, our new suite of tools for GenAI.

 

Documentation

Weights and Biases Experiments Weights and Biases Reports Weights and Biases Artifacts Weights and Biases Tables Weights and Biases Sweeps Weights and Biases Launch Weights and Biases Model Management Weights and Biases Prompts

See the W&B Developer Guide and API Reference Guide for a full technical description of the W&B platform.

Quickstart

Get started with W&B in four steps:

  1. First, sign up for a free W&B account.

  2. Second, install the W&B SDK with pip. Navigate to your terminal and type the following command:

pip install wandb
  1. Third, log into W&B:
wandb.login()
  1. Use the example code snippet below as a template to integrate W&B to your Python script:
import wandb

# Start a W&B Run with wandb.init
run = wandb.init(project="my_first_project")

# Save model inputs and hyperparameters in a wandb.config object
config = run.config
config.learning_rate = 0.01

# Model training code here ...

# Log metrics over time to visualize performance with wandb.log
for i in range(10):
    run.log({"loss": loss})

That's it! Navigate to the W&B App to view a dashboard of your first W&B Experiment. Use the W&B App to compare multiple experiments in a unified place, dive into the results of a single run, and much more!

Example W&B Dashboard that shows Runs from an Experiment.

 

Integrations

Use your favorite framework with W&B. W&B integrations make it fast and easy to set up experiment tracking and data versioning inside existing projects. For more information on how to integrate W&B with the framework of your choice, see the Integrations chapter in the W&B Developer Guide.

🔥 PyTorch

Call .watch and pass in your PyTorch model to automatically log gradients and store the network topology. Next, use .log to track other metrics. The following example demonstrates an example of how to do this:

import wandb

# 1. Start a new run
run = wandb.init(project="gpt4")

# 2. Save model inputs and hyperparameters
config = run.config
config.dropout = 0.01

# 3. Log gradients and model parameters
run.watch(model)
for batch_idx, (data, target) in enumerate(train_loader):
    ...
    if batch_idx % args.log_interval == 0:
        # 4. Log metrics to visualize performance
        run.log({"loss": loss})
🌊 TensorFlow/Keras Use W&B Callbacks to automatically save metrics to W&B when you call `model.fit` during training.

The following code example demonstrates how your script might look like when you integrate W&B with Keras:

# This script needs these libraries to be installed:
#   tensorflow, numpy

import wandb
from wandb.keras import WandbMetricsLogger, WandbModelCheckpoint

import random
import numpy as np
import tensorflow as tf


# Start a run, tracking hyperparameters
run = wandb.init(
    # set the wandb project where this run will be logged
    project="my-awesome-project",
    # track hyperparameters and run metadata with wandb.config
    config={
        "layer_1": 512,
        "activation_1": "relu",
        "dropout": random.uniform(0.01, 0.80),
        "layer_2": 10,
        "activation_2": "softmax",
        "optimizer": "sgd",
        "loss": "sparse_categorical_crossentropy",
        "metric": "accuracy",
        "epoch": 8,
        "batch_size": 256,
    },
)

# [optional] use wandb.config as your config
config = run.config

# get the data
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train, y_train = x_train[::5], y_train[::5]
x_test, y_test = x_test[::20], y_test[::20]
labels = [str(digit) for digit in range(np.max(y_train) + 1)]

# build a model
model = tf.keras.models.Sequential(
    [
        tf.keras.layers.Flatten(input_shape=(28, 28)),
        tf.keras.layers.Dense(config.layer_1, activation=config.activation_1),
        tf.keras.layers.Dropout(config.dropout),
        tf.keras.layers.Dense(config.layer_2, activation=config.activation_2),
    ]
)

# compile the model
model.compile(optimizer=config.optimizer, loss=config.loss, metrics=[config.metric])

# WandbMetricsLogger will log train and validation metrics to wandb
# WandbModelCheckpoint will upload model checkpoints to wandb
history = model.fit(
    x=x_train,
    y=y_train,
    epochs=config.epoch,
    batch_size=config.batch_size,
    validation_data=(x_test, y_test),
    callbacks=[
        WandbMetricsLogger(log_freq=5),
        WandbModelCheckpoint("models"),
    ],
)

# [optional] finish the wandb run, necessary in notebooks
run.finish()

Get started integrating your Keras model with W&B today:

🤗 Hugging Face Transformers

Pass wandb to the report_to argument when you run a script using a Hugging Face Trainer. W&B will automatically log losses, evaluation metrics, model topology, and gradients.

Note: The environment you run your script in must have wandb installed.

The following example demonstrates how to integrate W&B with Hugging Face:

# This script needs these libraries to be installed:
#   numpy, transformers, datasets

import wandb

import os
import numpy as np
from datasets import load_dataset
from transformers import TrainingArguments, Trainer
from transformers import AutoTokenizer, AutoModelForSequenceClassification


def tokenize_function(examples):
    return tokenizer(examples["text"], padding="max_length", truncation=True)


def compute_metrics(eval_pred):
    logits, labels = eval_pred
    predictions = np.argmax(logits, axis=-1)
    return {"accuracy": np.mean(predictions == labels)}


# download prepare the data
dataset = load_dataset("yelp_review_full")
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")

small_train_dataset = dataset["train"].shuffle(seed=42).select(range(1000))
small_eval_dataset = dataset["test"].shuffle(seed=42).select(range(300))

small_train_dataset = small_train_dataset.map(tokenize_function, batched=True)
small_eval_dataset = small_train_dataset.map(tokenize_function, batched=True)

# download the model
model = AutoModelForSequenceClassification.from_pretrained(
    "distilbert-base-uncased", num_labels=5
)

# set the wandb project where this run will be logged
os.environ["WANDB_PROJECT"] = "my-awesome-project"

# save your trained model checkpoint to wandb
os.environ["WANDB_LOG_MODEL"] = "true"

# turn off watch to log faster
os.environ["WANDB_WATCH"] = "false"

# pass "wandb" to the `report_to` parameter to turn on wandb logging
training_args = TrainingArguments(
    output_dir="models",
    report_to="wandb",
    logging_steps=5,
    per_device_train_batch_size=32,
    per_device_eval_batch_size=32,
    evaluation_strategy="steps",
    eval_steps=20,
    max_steps=100,
    save_steps=100,
)

# define the trainer and start training
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=small_train_dataset,
    eval_dataset=small_eval_dataset,
    compute_metrics=compute_metrics,
)
trainer.train()

# [optional] finish the wandb run, necessary in notebooks
wandb.finish()
⚡️ PyTorch Lightning

Build scalable, structured, high-performance PyTorch models with Lightning and log them with W&B.

# This script needs these libraries to be installed:
#   torch, torchvision, pytorch_lightning

import wandb

import os
from torch import optim, nn, utils
from torchvision.datasets import MNIST
from torchvision.transforms import ToTensor

import pytorch_lightning as pl
from pytorch_lightning.loggers import WandbLogger


class LitAutoEncoder(pl.LightningModule):
    def __init__(self, lr=1e-3, inp_size=28, optimizer="Adam"):
        super().__init__()

        self.encoder = nn.Sequential(
            nn.Linear(inp_size * inp_size, 64), nn.ReLU(), nn.Linear(64, 3)
        )
        self.decoder = nn.Sequential(
            nn.Linear(3, 64), nn.ReLU(), nn.Linear(64, inp_size * inp_size)
        )
        self.lr = lr

        # save hyperparameters to self.hparamsm auto-logged by wandb
        self.save_hyperparameters()

    def training_step(self, batch, batch_idx):
        x, y = batch
        x = x.view(x.size(0), -1)
        z = self.encoder(x)
        x_hat = self.decoder(z)
        loss = nn.functional.mse_loss(x_hat, x)

        # log metrics to wandb
        self.log("train_loss", loss)
        return loss

    def configure_optimizers(self):
        optimizer = optim.Adam(self.parameters(), lr=self.lr)
        return optimizer


# init the autoencoder
autoencoder = LitAutoEncoder(lr=1e-3, inp_size=28)

# setup data
batch_size = 32
dataset = MNIST(os.getcwd(), download=True, transform=ToTensor())
train_loader = utils.data.DataLoader(dataset, shuffle=True)

# initialise the wandb logger and name your wandb project
wandb_logger = WandbLogger(project="my-awesome-project")

# add your batch size to the wandb config
wandb_logger.experiment.config["batch_size"] = batch_size

# pass wandb_logger to the Trainer
trainer = pl.Trainer(limit_train_batches=750, max_epochs=5, logger=wandb_logger)

# train the model
trainer.fit(model=autoencoder, train_dataloaders=train_loader)

# [optional] finish the wandb run, necessary in notebooks
wandb.finish()
💨 XGBoost Use W&B Callbacks to automatically save metrics to W&B when you call `model.fit` during training.

The following code example demonstrates how your script might look like when you integrate W&B with XGBoost:

# This script needs these libraries to be installed:
#   numpy, xgboost

import wandb
from wandb.xgboost import WandbCallback

import numpy as np
import xgboost as xgb


# setup parameters for xgboost
param = {
    "objective": "multi:softmax",
    "eta": 0.1,
    "max_depth": 6,
    "nthread": 4,
    "num_class": 6,
}

# start a new wandb run to track this script
run = wandb.init(
    # set the wandb project where this run will be logged
    project="my-awesome-project",
    # track hyperparameters and run metadata
    config=param,
)

# download data from wandb Artifacts and prep data
run.use_artifact("wandb/intro/dermatology_data:v0", type="dataset").download(".")
data = np.loadtxt(
    "./dermatology.data",
    delimiter=",",
    converters={33: lambda x: int(x == "?"), 34: lambda x: int(x) - 1},
)
sz = data.shape

train = data[: int(sz[0] * 0.7), :]
test = data[int(sz[0] * 0.7) :, :]

train_X = train[:, :33]
train_Y = train[:, 34]

test_X = test[:, :33]
test_Y = test[:, 34]

xg_train = xgb.DMatrix(train_X, label=train_Y)
xg_test = xgb.DMatrix(test_X, label=test_Y)
watchlist = [(xg_train, "train"), (xg_test, "test")]

# add another config to the wandb run
num_round = 5
run.config["num_round"] = 5
run.config["data_shape"] = sz

# pass WandbCallback to the booster to log its configs and metrics
bst = xgb.train(
    param, xg_train, num_round, evals=watchlist, callbacks=[WandbCallback()]
)

# get prediction
pred = bst.predict(xg_test)
error_rate = np.sum(pred != test_Y) / test_Y.shape[0]

# log your test metric to wandb
run.summary["Error Rate"] = error_rate

# [optional] finish the wandb run, necessary in notebooks
run.finish()
🧮 Sci-Kit Learn Use wandb to visualize and compare your scikit-learn models' performance:
# This script needs these libraries to be installed:
#   numpy, sklearn

import wandb
from wandb.sklearn import plot_precision_recall, plot_feature_importances
from wandb.sklearn import plot_class_proportions, plot_learning_curve, plot_roc

import numpy as np
from sklearn import datasets
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split


# load and process data
wbcd = datasets.load_breast_cancer()
feature_names = wbcd.feature_names
labels = wbcd.target_names

test_size = 0.2
X_train, X_test, y_train, y_test = train_test_split(
    wbcd.data, wbcd.target, test_size=test_size
)

# train model
model = RandomForestClassifier()
model.fit(X_train, y_train)
model_params = model.get_params()

# get predictions
y_pred = model.predict(X_test)
y_probas = model.predict_proba(X_test)
importances = model.feature_importances_
indices = np.argsort(importances)[::-1]

# start a new wandb run and add your model hyperparameters
run = wandb.init(project="my-awesome-project", config=model_params)

# Add additional configs to wandb
run.config.update(
    {
        "test_size": test_size,
        "train_len": len(X_train),
        "test_len": len(X_test),
    }
)

# log additional visualisations to wandb
plot_class_proportions(y_train, y_test, labels)
plot_learning_curve(model, X_train, y_train)
plot_roc(y_test, y_probas, labels)
plot_precision_recall(y_test, y_probas, labels)
plot_feature_importances(model)

# [optional] finish the wandb run, necessary in notebooks
run.finish()

 

W&B Hosting Options

Weights & Biases is available in the cloud or installed on your private infrastructure. Set up a W&B Server in a production environment in one of three ways:

  1. Production Cloud: Set up a production deployment on a private cloud in just a few steps using terraform scripts provided by W&B.
  2. Dedicated Cloud: A managed, dedicated deployment on W&B's single-tenant infrastructure in your choice of cloud region.
  3. On-Prem/Bare Metal: W&B supports setting up a production server on most bare metal servers in your on-premise data centers. Quickly get started by running wandb server to easily start hosting W&B on your local infrastructure.

See the Hosting documentation in the W&B Developer Guide for more information.

 

Contribution guidelines

Weights & Biases ❤️ open source, and we welcome contributions from the community! See the Contribution guide for more information on the development workflow and the internals of the wandb library. For wandb bugs and feature requests, visit GitHub Issues or contact [email protected] .

 

W&B Community

Be a part of the growing W&B Community and interact with the W&B team in our Discord. Stay connected with the latest ML updates and tutorials with W&B Fully Connected.

 

License

MIT License

edu's People

Contributors

agata-mlynarczyk avatar ap-wb avatar arig23498 avatar ash0ts avatar charlesfrye avatar dansbecker avatar hamelsmu avatar jxnl avatar kldarek avatar morganmcg1 avatar parambharat avatar parulnith avatar peacekurella avatar ritog avatar scottire avatar tcapelle avatar vanpelt avatar zythosec avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

edu's Issues

Consider creating a nn utilities file

On the one hand, it will reduce code duplication across Colabs, but on the other, it really needs to be done well if it's going to be used everywhere -- have to make sure it's e.g. DDP compatible, the logging could be done better (more callbacks?), and want to use PL best practices as much as possible.

run Colab-specific installs only on colab

right now, the installs are run regardless of environment, but they should only be run in Colab.

just need to move the !pip install commands into the appropriate if branch and then apply %%capture to the cell

add more exercises to probability nb

Ideas:

  1. PMFs and PDFs. Dictionaries versus densities.
  2. Softmax. Define a softmax function for a PMF.
  3. Cross entropy loss. Implement it, based on a formula? Based on PMF or PDF?

fix Binder compatibility

The Binder setup needs to be tweaked now that we're in v2. The Dockerfile is no longer in the right place, which is going to be a PITA to fix. That Dockerfile also needs to be updated.

Add more autograded exercises to calculus notebook

Ideas

  1. Little-o exercises. They'd be multiple choice, basically. And/or: provide a function that is little-o of x^2? I can check this with sympy.
  2. Linearity of the gradient approximation. Write a function to compute gradient approx, given the function and its gradient. Compare to finite differences?

rethink the logging code from the LoggedLitModule

This was written when I had half as much experience with Lighting as I have now, and before the most recent integration.

I should rethink it, with emphasis on the following:

  • moving material into callbacks
  • cutting down on the magic parameter inference
  • improve robustness (see #25)
  • reorganize and make hierarchical (see #20)
  • writing some tests, including e2e tests that write runs to wandb and pull metrics

Move SVD material

The SVD material is interesting, but hard to make concrete and compelling with the constraints we have (unless I come up with a slick "LA-as-programming" explanation of kernels and maybe also eigenvalues, which is tougher).

I should move it into a separate notebook.

improve the robustness of the utils

Networks that are being pruned with torch.nn.prune break certain assumptions in e.g. the parameter counting, as do I believe the quantized networks.

These should be resolved (incorporating fixes from the relevant notebooks, when possible) so that the utils are more robust.

Add more "Linear Algebra as Programming" Exercises

This is the core idea of the lecture slides, but there aren't enough exercises for it. They require a certain amount of creativity, but here's a few possibilities:

  1. Shapes and types. Debug a shape issue? Write code that checks a shape, by analogy with checking a type?
  2. Batch dimensions/broadcasting and for loops. Write a for loop that does the same thing as a broadcasted multiply. Make it a "batch" application.
  3. Convolutions and zips. Harder: write a zip that's equivalent to a convolution.
  4. Parallel composition. Use concat/stack operations to implement applying two "functions" to the same input. Something else with the Kronecker product?
  5. repeat. Use matrix multiplication (an outer product?) to copy the input k times.

Make extras compatible with Colab

Would like to drop reliance on the W&B Hub, and Colab seems like the least-bad choice.

Options:

  1. Local. No lol. Installation issues are a dealbreaker.
  2. Binder. Ephemerality will be frustrating for students, and is a dealbreaker for classes where the HW is done between sessions.
  3. Colab. Avoids ephemerality. The limitation to the matplotlib inline backend for interactive charts is frustrating but not a showstopper.
  4. Gradient. Requires a login, possibly expensive, unclear if better, GPUs not needed for this class.

fix mixed 2-space and 4-space indentation

Colab defaults to 2-space but much of the code is in 4-space -- and other Jupyter instances don't like 2-space indentation

In calculus exercises: is_little_o, identity, constant

Make a better test for the array_is_pmf question

Right now, doesn't appropriately test whether there are values above 1 or below 0 because the examples don't add up to 1, which is what most folks test for.

np.array([-1, 2]) and/or np.array([-0.5, 1.1, 0.2, 0.2]) would do it.

move MLP utilities to Lightning

The utils I wrote for the lightning/mlp/ notebooks are useful more broadly in the lightning material.

They should perhaps be moved up to the lightning/ folder. This will require changing some code in the extant lightning colabs.

Fix typos in autoencoder-mnist.ipynb

  1. Choosing an output activate
    • Review the logged outputs of your neural network in the Weights & Biases interface and look for issues caused by this choice of activation.
    • For example, here they are always between 0 and 1, to match the data they are compared against, while while activation values have no such restriction.
  2. Change Hyperparameters
    • Better results can be obtained by tweaking the hyperparameters.
  3. Challenge: Regularization and Learned Weights
    • With the default settings, the leanred filters don't look like that at all.

https://github.com/wandb/edu/blob/4a438532a4cd53a025c348ecabed93bcc5dee646/lightning/autoencoder/autoencoder-mnist.ipynb

Add implementing gradient descent to the calclulus nb

Given the gradient and parameters, apply one gradient descent step and return the new parameters.

Check that:

  • Stationary at a maximum and a minimum
  • Stationary if lr is 0
  • Optimizes quadratic in one step with the right lr
  • Works on vector-valued versus scalar-valued inputs?

Deduplicate projects

Architecture search was put under cnn/ when it fits much better under projects/. For now it's duplicated, but it should only be in projects/.

There are currently two versions of the FER project, but there should only be one. Once the more robust NN utils are in place (#20, #25, #26) can reduce.

Better DataLoader practices

should make the dataloaders more configurable for the AbstractMNISTDataModule -- can probably fix pin_memory to True, but should allow configuration of num_workers (with a default of 2 or nproc, depending on how far we want to go)

Better model saving for PyTorch

Caught on a dilemma with saving PyTorch models for viewing in Netron

  • OT1H, just saving as a .pt file results in unreliable performance by Netron, who can't really be expected to handle all the possible choices in both major libraries, and so prefers ONNX and lets them handle the conversion, but
  • OTOH, ONNX can't handle e.g. the AdapativeAvgPool2d layer, important for CNNs that are easy to play with. Seems like a pretty fundamental limitation w.r.t adaptive layers.

In particular, for the MLP and CNN in PyTorch, I want to emphasize reusability and enable easy extension, and so I'm using ModuleLists and custom Modules. Neither is really gonna play nicely with Netron.

For now, I'm sticking with .pt files, which save but aren't visualized well (the ModuleLists aren't expanded, and that's where all the action is!).

Organize Math4ML into folders

Adding more content and at the same time de-emphasizing existing content, but we don't want to delete that content, so let's make a nested structure with folders and subfolders

-- 00_{topic}/
  |  exercises.ipynb
  --  extras/
    |  {subtopic}.ipynb
-- 01_{topic}/ 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.