Skip to content
Aabha AI Academy
Skip to reading

Deep Learning

Stages

Compare these references after your own attempt. You can read them at any time; they do not record a live model or vendor run.

import torch
from torch import nn

def predict_batch(x,w,b):
    return x @ w + b

def fit_standardizer(x_train):
    return x_train.mean(0), x_train.std(0,unbiased=False).clamp_min(1e-8)

def make_classifier():
    return nn.Sequential(nn.Linear(2,16),nn.ReLU(),nn.Linear(16,1))

def training_step(model,optimizer,xb,yb):
    model.train()
    optimizer.zero_grad(set_to_none=True)
    logits=model(xb)
    assert logits.shape == yb.shape
    loss=nn.functional.binary_cross_entropy_with_logits(logits,yb)
    loss.backward()
    optimizer.step()
    return loss.item()

def infer_probabilities(model,xb):
    model.eval()
    with torch.no_grad():
        return model(xb).sigmoid()

Download this source file