REAL PYTORCH · CPU TRAINING AND AUTOGRAD

Project: train uncertainty and calibrate independent prediction intervals

Build the complete local synthetic heteroscedastic regression experiment with four disjoint partitions: training, validation selection, conformal calibration and final test. Train both mean and positive variance using Gaussian NLL, choose the checkpoint by validation NLL, then compute normalized absolute calibration residuals and the finite-sample ceil((n+1)*(1-alpha)) order statistic without interpolation. Export the learned state plus quantile, reload in a fresh model, and report actual test coverage, width and left/right-region coverage. Calibration and test labels must never fit or select model weights. Keep the full reference report schema. Marginal conformal coverage requires exchangeability and does not imply subgroup coverage.

Run this Lab in Pythilo

Open Labs, choose its topic and tap Download entire topic, or Download entire category for all its Labs, including the required Python packages, then load the starter or worked answer and tap Trace. Progress, pause, resume and removal are in Downloads. Independent checks execute real CPU training and gradients.

No GPU, external model weights or API key is required. After downloading the topic and packages, this Lab runs offline.

PyTorch is unavailable in this browser runtime. The code and lessons are readable here; execution and checks run in the app.

Pythilo on the App Store →

Read lesson: Project: train uncertainty and calibrate independent prediction intervals →

Your task

Build the complete local synthetic heteroscedastic regression experiment with four disjoint partitions: training, validation selection, conformal calibration and final test. Train both mean and positive variance using Gaussian NLL, choose the checkpoint by validation NLL, then compute normalized absolute calibration residuals and the finite-sample ceil((n+1)*(1-alpha)) order statistic without interpolation. Export the learned state plus quantile, reload in a fresh model, and report actual test coverage, width and left/right-region coverage. Calibration and test labels must never fit or select model weights. Keep the full reference report schema. Marginal conformal coverage requires exchangeability and does not imply subgroup coverage.

Starter

import math
import torch
from torch.nn import functional as F

def gaussian_nll(mean, raw_variance, target, floor=1e-4):
    values=(mean,raw_variance,target)
    if mean.numel()==0 or any(x.shape!=mean.shape or not x.is_floating_point() or not torch.isfinite(x).all() for x in values):raise ValueError('Expected aligned finite floating nonempty tensors.')
    if not math.isfinite(floor) or floor<=0:raise ValueError('Variance floor must be positive and finite.')
    variance=F.softplus(raw_variance)+floor
    result=.5*(math.log(2*math.pi)+variance.log()+(target-mean).square()/variance)
    return result.mean()

from torch.nn import functional as F
import copy
import hashlib
import io
import json

def data_hash(rows):
    return hashlib.sha256(json.dumps(rows,sort_keys=True,separators=(',',':')).encode()).hexdigest()

def state_hash(state):
    return data_hash({key:value.detach().cpu().tolist() for key,value in state.items()})

def export_payload(payload):
    stream=io.BytesIO()
    torch.save(payload,stream)
    return stream.getvalue()

def read_payload(blob):
    return torch.load(io.BytesIO(blob),map_location='cpu',weights_only=True)

def check_steps(steps):
    if type(steps)!=int or not 1<=steps<=500:raise ValueError('Choose 1..500 actual optimizer steps.')

def override_rows(original,replacement):
    if replacement is None:return original
    if not isinstance(replacement,list) or len(replacement)!=len(original):raise ValueError('Evaluation replacement must preserve row identities.')
    if [r['id'] for r in replacement]!=[r['id'] for r in original]:raise ValueError('Evaluation row identities must remain unchanged.')
    return copy.deepcopy(replacement)


def make_data(seed=7):
    result={}
    for split,n,offset in [('train',240,0),('validation',96,1000),('calibration',128,2000),('test',192,3000)]:
        rng=torch.Generator().manual_seed(seed+offset)
        x=torch.rand(n,generator=rng)*6-3
        sigma=.15+.35*torch.sigmoid(2*x)
        y=x.sin()+sigma*torch.randn(n,generator=rng)
        result[split]=[{'id':f'{split}-{i}','x':[x[i].item()],'y':y[i].item()} for i in range(n)]
    return result

def tensors(rows):return torch.tensor([r['x'] for r in rows]),torch.tensor([r['y'] for r in rows])

def build_model():return torch.nn.Sequential(torch.nn.Linear(1,16),torch.nn.Tanh(),torch.nn.Linear(16,2))

def predictions(model,rows):
    with torch.no_grad():
        x,y=tensors(rows);out=model(x)
        return out[:,0],(F.softplus(out[:,1])+1e-4).sqrt()

def likelihood(model,rows):
    with torch.no_grad():
        x,y=tensors(rows);out=model(x)
        return gaussian_nll(out[:,0],out[:,1],y).item()

def train_model(model,train,validation,steps):
    optimizer=torch.optim.Adam(model.parameters(),lr=.03)
    x,y=tensors(train);history=[];best=None;best_score=float('inf')
    for step in range(steps):
        optimizer.zero_grad(set_to_none=True)
        out=model(x);loss=gaussian_nll(out[:,0],out[:,1],y)
        loss.backward();optimizer.step()
        valid_loss=likelihood(model,validation)
        if valid_loss<best_score:
            best_score=valid_loss;best=copy.deepcopy(model.state_dict());best_step=step+1
        if step%20==0 or step==steps-1:
            history.append({'step':step+1,'loss':loss.item(),'validation_loss':valid_loss})
    model.load_state_dict(best)
    return best_step,history

def interval_metrics(model,rows,q):
    mean,std=predictions(model,rows);x,y=tensors(rows)
    covered=(y-mean).abs()<=q*std
    groups=[]
    for mask in (x[:,0]<0,x[:,0]>=0):
        groups.append(covered[mask].float().mean().item() if mask.any() else None)
    return {'nll':likelihood(model,rows),'coverage':covered.float().mean().item(),'mean_width':(2*q*std).mean().item(),'group_coverage':groups}

def run_experiment(seed=7,steps=140,alpha=.1,test_override=None,calibration_override=None):
    # TODO: implement run_experiment under the declared contract.
    return None

if __name__=='__main__':
    print('Implement run_experiment, then run the lesson checks.')
Inspect the worked answer
import math
import torch
from torch.nn import functional as F

def gaussian_nll(mean, raw_variance, target, floor=1e-4):
    values=(mean,raw_variance,target)
    if mean.numel()==0 or any(x.shape!=mean.shape or not x.is_floating_point() or not torch.isfinite(x).all() for x in values):raise ValueError('Expected aligned finite floating nonempty tensors.')
    if not math.isfinite(floor) or floor<=0:raise ValueError('Variance floor must be positive and finite.')
    variance=F.softplus(raw_variance)+floor
    result=.5*(math.log(2*math.pi)+variance.log()+(target-mean).square()/variance)
    return result.mean()

from torch.nn import functional as F
import copy
import hashlib
import io
import json

def data_hash(rows):
    return hashlib.sha256(json.dumps(rows,sort_keys=True,separators=(',',':')).encode()).hexdigest()

def state_hash(state):
    return data_hash({key:value.detach().cpu().tolist() for key,value in state.items()})

def export_payload(payload):
    stream=io.BytesIO()
    torch.save(payload,stream)
    return stream.getvalue()

def read_payload(blob):
    return torch.load(io.BytesIO(blob),map_location='cpu',weights_only=True)

def check_steps(steps):
    if type(steps)!=int or not 1<=steps<=500:raise ValueError('Choose 1..500 actual optimizer steps.')

def override_rows(original,replacement):
    if replacement is None:return original
    if not isinstance(replacement,list) or len(replacement)!=len(original):raise ValueError('Evaluation replacement must preserve row identities.')
    if [r['id'] for r in replacement]!=[r['id'] for r in original]:raise ValueError('Evaluation row identities must remain unchanged.')
    return copy.deepcopy(replacement)


def make_data(seed=7):
    result={}
    for split,n,offset in [('train',240,0),('validation',96,1000),('calibration',128,2000),('test',192,3000)]:
        rng=torch.Generator().manual_seed(seed+offset)
        x=torch.rand(n,generator=rng)*6-3
        sigma=.15+.35*torch.sigmoid(2*x)
        y=x.sin()+sigma*torch.randn(n,generator=rng)
        result[split]=[{'id':f'{split}-{i}','x':[x[i].item()],'y':y[i].item()} for i in range(n)]
    return result

def tensors(rows):return torch.tensor([r['x'] for r in rows]),torch.tensor([r['y'] for r in rows])

def build_model():return torch.nn.Sequential(torch.nn.Linear(1,16),torch.nn.Tanh(),torch.nn.Linear(16,2))

def predictions(model,rows):
    with torch.no_grad():
        x,y=tensors(rows);out=model(x)
        return out[:,0],(F.softplus(out[:,1])+1e-4).sqrt()

def likelihood(model,rows):
    with torch.no_grad():
        x,y=tensors(rows);out=model(x)
        return gaussian_nll(out[:,0],out[:,1],y).item()

def train_model(model,train,validation,steps):
    optimizer=torch.optim.Adam(model.parameters(),lr=.03)
    x,y=tensors(train);history=[];best=None;best_score=float('inf')
    for step in range(steps):
        optimizer.zero_grad(set_to_none=True)
        out=model(x);loss=gaussian_nll(out[:,0],out[:,1],y)
        loss.backward();optimizer.step()
        valid_loss=likelihood(model,validation)
        if valid_loss<best_score:
            best_score=valid_loss;best=copy.deepcopy(model.state_dict());best_step=step+1
        if step%20==0 or step==steps-1:
            history.append({'step':step+1,'loss':loss.item(),'validation_loss':valid_loss})
    model.load_state_dict(best)
    return best_step,history

def interval_metrics(model,rows,q):
    mean,std=predictions(model,rows);x,y=tensors(rows)
    covered=(y-mean).abs()<=q*std
    groups=[]
    for mask in (x[:,0]<0,x[:,0]>=0):
        groups.append(covered[mask].float().mean().item() if mask.any() else None)
    return {'nll':likelihood(model,rows),'coverage':covered.float().mean().item(),'mean_width':(2*q*std).mean().item(),'group_coverage':groups}

def run_experiment(seed=7,steps=140,alpha=.1,test_override=None,calibration_override=None):
    check_steps(steps)
    if not math.isfinite(alpha) or not 0<alpha<1:raise ValueError('alpha must lie strictly between zero and one.')
    data=make_data(seed)
    data['test']=override_rows(data['test'],test_override)
    data['calibration']=override_rows(data['calibration'],calibration_override)
    rank=math.ceil((len(data['calibration'])+1)*(1-alpha))
    if rank>len(data['calibration']):raise ValueError('This calibration sample is too small for a finite requested quantile.')
    rng=torch.get_rng_state()
    try:torch.manual_seed(seed);model=build_model()
    finally:torch.set_rng_state(rng)
    initial=copy.deepcopy(model.state_dict());initial_nll=likelihood(model,data['train'])
    best_step,history=train_model(model,data['train'],data['validation'],steps)
    mean,std=predictions(model,data['calibration']);_,labels=tensors(data['calibration'])
    scores=(labels-mean).abs()/std
    q=scores.kthvalue(rank).values.item()
    fit_hash=data_hash({k:data[k] for k in ('train','validation')})
    artifact=export_payload({'kind':'heteroscedastic-conformal','state':model.state_dict(),'initial':initial,'q':q,'alpha':alpha,'rank':rank,'fit_hash':fit_hash,'seed':seed})
    fresh=build_model();fresh.load_state_dict(read_payload(artifact)['state'])
    original_mean,original_std=predictions(model,data['test']);loaded_mean,loaded_std=predictions(fresh,data['test'])
    report={'artifact':artifact,'artifact_bytes':len(artifact),'q':q,'rank':rank,'alpha':alpha,'initial_train_nll':initial_nll,'train_nll':likelihood(model,data['train']),'validation_nll':likelihood(model,data['validation']),'test':interval_metrics(model,data['test'],q),'best_step':best_step,'history':history,'fit_hash':fit_hash,'data_hash':data_hash(data),'reload_max_error':max((original_mean-loaded_mean).abs().max().item(),(original_std-loaded_std).abs().max().item()),'ids':{k:[r['id'] for r in rows] for k,rows in data.items()},'steps':steps}
    return report

if __name__=='__main__':
    result=run_experiment()
    with open('uncertainty-model.pt','wb') as f:f.write(result['artifact'])
    with open('uncertainty-report.json','w') as f:json.dump({k:v for k,v in result.items() if k!='artifact'},f,indent=2)
    print('Saved uncertainty-model.pt and uncertainty-report.json')

About this algorithm

How Project: train uncertainty and calibrate independent prediction intervals works

The idea

A prediction is more useful with an honest range around it. This project builds one in two stages. First a small network learns a mean and a variance by minimising the Gaussian negative log-likelihood, on data whose noise grows from left to right. The learned variance shapes the interval: narrow where the data is quiet, wide where it is noisy.

Variance alone guarantees nothing about coverage, so the second stage calibrates it. On a separate calibration split, each absolute error is divided by the predicted standard deviation, and one order statistic of those scores becomes the multiplier q. The interval mean ± q * std covers new points from the same source at least 1 - alpha of the time on average, across all inputs together, not region by region.

How it works

  1. Generate four disjoint splits with make_data: 240 training, 96 validation, 128 calibration and 192 test rows.
  2. Build Linear(1, 16), Tanh, Linear(16, 2), whose two outputs are the mean and the raw variance.
  3. Train with Adam on gaussian_nll over the training rows and keep the state with the lowest validation NLL.
  4. On calibration rows compute scores = |y - mean| / std and take q as the score at rank ceil((n + 1) * (1 - alpha)) using kthvalue.
  5. On test rows, count how often |y - mean| <= q * std, and report coverage, mean width, and coverage for x < 0 and x >= 0.
  6. Export the state together with q, reload it into a fresh model, and record the largest difference in predictions.

What it costs

Model
66 parameters
Per step
full batch of 240 rows
Calibration
128 rows, rank 117

Training is 140 full-batch Adam steps on a 66-parameter network, and calibration is one pass over 128 held-out rows to find a single order statistic.

Where it is used

  • The MAPIE Python library implements split conformal prediction and conformalized quantile regression on top of scikit-learn estimators.
  • Romano, Patterson and Candès introduced conformalized quantile regression in 2019, calibrating intervals whose width adapts to the input.
  • Kendall and Gal's 2017 paper on uncertainty in computer vision trains depth regression networks to predict a per-pixel variance through a Gaussian likelihood.

Watch out for

  • Calibrating on training or validation rows: the model fits those better than new data, so q comes out too small.
  • Interpolating the quantile or using ceil(n * (1 - alpha)): the guarantee needs the exact order statistic at the n + 1 rank.
  • Reading 90% overall coverage as 90% everywhere. The report splits coverage by x < 0 and x >= 0 because the guarantee is only marginal.