Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
WISP / src /wisp_fast /funnel /dictionary.py
Zipeng365's picture
wisp-har 0.4.0: WISP-Funnel search and the adaptive HMM switch (default decoder)
93a9282 verified
Raw History Blame Contribute Delete
8.35 kB
"""Funnel proxy: candidate-specific Ridge subspaces of one shared, training-only feature dictionary."""
from dataclasses import replace
from time import perf_counter
import numpy as np
from scipy.linalg.lapack import dposv
from wisp.core.metrics import score_from_metrics
from wisp.search.cascade import validate_candidate_spec
from .controller import WISPSearchController
from wisp.search.profiles import get_profile
from .cis_lite import CISLite
from .decoder import FastSmoother,_order
from .score_panels import PanelDecoder,CompactReporter
from .engine import FORCE_HMM
# The proxy evaluates a real linear model on each selected subspace. These
# columns approximate the seven families. Forests / ensembles are not fitted
# until shortlisted. Budgets and the mapping are fixed before held-out use.
MIX = {
"har_sculpt_forest_hmm": (0,24,24),
"rocket_sketch_ridge_hmm": (12,0,0),
"symbolic_interval_forest": (0,12,40),
"hybrid_cpu_ensemble": (8,20,20),
"invariant_interval_shape_forest": (4,36,16),
"state_rocket_interval_hmm": (10,20,24),
"posterior_cpu_ensemble": (8,24,20),
}
class Dictionary:
def __init__(self, train, valid, seed, *, retain_raw=False,bank_preset="wide"):
start=perf_counter()
mapper=CISLite(bank_preset, seed=seed)
mapper._plan(train.X.shape[1:])
A=mapper.transform(train.X)
V=mapper.transform(valid.X)
if retain_raw:self.raw=(A,V);self.mapper=mapper
A=A.astype(np.float64);V=V.astype(np.float64)
self.widths=mapper.block_widths_
self.classes, encoded, counts=np.unique(train.y,return_inverse=True,return_counts=True)
self.n_classes=train.n_classes
w=len(train.y)/(len(counts)*counts[encoded])
mu=A.mean(axis=0)
sd=np.maximum(A.std(axis=0),1e-4)
A=(A-mu)/sd
V=(V-mu)/sd
self.center=mu;self.scale=sd
self.mean=np.average(A,axis=0,weights=w)
A-=self.mean
self.valid=np.asfortranarray(V-self.mean)
targets=(2*(encoded[:,None]==np.arange(len(counts)))-1).astype(np.float64)
self.bias=np.average(targets,axis=0,weights=w)
self.cross=A.T@(w[:,None]*(targets-self.bias))
weighted=A*np.sqrt(w)[:,None]
self.gram=weighted.T@weighted
self.report=CompactReporter(valid.y,train.n_classes)
self._order,self._bounds=_order(len(valid.y),valid.subject,valid.time_index)
self._valid_panel=valid
self.smoother=FastSmoother().fit(train.y,groups=train.subject,time_index=train.time_index,n_classes=train.n_classes)
self.decoder=PanelDecoder(self._order,self._bounds,self.classes,self.smoother)
self.build_s=perf_counter()-start
def columns(self,spec):
r,i,s=MIX[spec.name]
p=spec.params
if r and "n_kernels" in p:
r=min(24,max(3,round(p["n_kernels"]/32)))
if i and "n_intervals" in p:
i=int(min(36,max(6,p["n_intervals"])))
if s and "symbolic_bins" in p:
s=int(min(40,max(8,4*p["symbolic_bins"])))
rng=np.random.default_rng(int(p.get("seed",42)))
rr,ii,ss=self.widths
kr=rng.choice(rr//4,size=min(r,rr//4),replace=False)
return np.sort(np.r_[ (4*kr[:,None]+np.arange(4)).ravel(),
rr+rng.choice(ii,min(i,ii),replace=False),
rr+ii+rng.choice(ss,min(s,ss),replace=False)].astype(np.intp))
def fitted(self,spec):
cols=self.columns(spec)
alpha={"ridge":8.0,"logreg":2.0,"rf":16.0,"extratrees":24.0}.get(
spec.params.get("classifier","extratrees"),8.0)
return cols,self.solve_columns(cols,alpha),alpha
def solve_columns(self,cols,alpha):
gram=self.gram[np.ix_(cols,cols)]
gram.flat[::len(cols)+1]+=alpha
_,weight,info=dposv(gram,self.cross[cols],lower=1)
if info:raise np.linalg.LinAlgError("Invalid ridge Gram pivot")
return weight
def predict(self,cols,weights,use_hmm,valid):
return self.decode_logits(self.valid[:,cols]@weights+self.bias,use_hmm,valid)
def decode_logits(self,logits,use_hmm,valid,panels=1):
if not use_hmm:return self.classes[logits.argmax(1)]
if valid is self._valid_panel:
y=self.decoder.decode(logits,panels,gauge=getattr(self,"logit_gauge",False))
if y is not None:return y
if panels>1:
return np.concatenate([self.decode_logits(panel,use_hmm,valid) for panel in np.split(logits,panels)])
local=np.exp(logits-logits.max(1,keepdims=True))
prob=np.zeros((len(local),self.n_classes))
prob[:,self.classes]=local/local.sum(1,keepdims=True)
if valid is self._valid_panel:return self.smoother._decode_ordered(prob,self._order,self._bounds)
return self.smoother.predict_from_proba(prob,groups=valid.subject,time_index=valid.time_index)
class _DictionarySearch(WISPSearchController):
def _evaluate_one(self,spec):
valid=validate_candidate_spec(spec)
if not valid.passed:
return "rejected",dict(error=valid.message,predictive_utility=-1e9)
t=perf_counter()
cols,weights,alpha=self.bank.fitted(spec)
fit=perf_counter()-t
t=perf_counter()
force=FORCE_HMM.get()
use_hmm=spec.params.get("use_hmm",True) if force is None else bool(force)
pred=self.bank.predict(cols,weights,use_hmm,self.valid)
pred_s=perf_counter()-t
metrics=self.bank.report(self.valid.y,pred,label_names=self.valid.label_names,
fit_seconds=fit,predict_seconds=pred_s)
complexity=len(spec.operators)+spec.params.get("n_kernels",0)/256.+spec.params.get("n_shapelets",0)/64.
utility=score_from_metrics(metrics,cost_weight=self.cost_weight,complexity=complexity)
accepted=bool(np.isfinite(utility) and (self.train.n_classes==1 or len(np.unique(pred))>1))
metrics.update(predictive_utility=utility,cascade_score=utility,accepted=accepted,
score=utility,proxy=getattr(self.bank,"kind","dictionary-ridge"),proxy_columns=cols.tolist(),proxy_alpha=alpha)
return ("ok" if accepted else "rejected"),metrics
def dictionary_search(method,train,valid,*,seed,evaluations,population,workers,n_jobs,output,
family_scope,cost_weight,bank=None,controller=_DictionarySearch,rows_out=None):
if min(workers,n_jobs)<1 or population<3:raise ValueError("Invalid workers/population")
scopes={"all":None,"cis":("state_rocket_interval_hmm",),
"cis-related":("state_rocket_interval_hmm","rocket_sketch_ridge_hmm","posterior_cpu_ensemble")}
if family_scope not in scopes:raise ValueError("Unknown family_scope")
profile=replace(get_profile(method),max_evaluations=evaluations,allowed_cpu_families=scopes[family_scope],
use_stress=False)
step=max(population-max(2,population//3),1)
generations=max(1,(evaluations-min(9,evaluations)+step-1)//step)
search=controller(train,valid,output,seed=seed,population=population,generations=generations,
profile=profile,cost_weight=cost_weight,robustness_weight=0,
candidate_workers=workers,n_jobs_per_candidate=n_jobs)
search.bank=Dictionary(train,valid,seed) if bank is None else bank
search.retain_rows=rows_out is not None
result=search.run()
if rows_out is not None:
rows_out.extend(search.evaluated_rows)
if result["n_evaluated"]!=evaluations:raise RuntimeError("Incomplete dictionary search")
return result,dict(kind=getattr(search.bank,"kind","dictionary-ridge"),seed=seed,block_widths=search.bank.widths,
preparation_s=search.bank.build_s,proxy_uses_stress=False,
training_windows=len(train.y),validation_windows=len(valid.y))
def diversified(rows, count):
"""Take one strong program per (family, nominal classifier, decoder), then fill by rank."""
choices=[];strata=set();deferred=[]
for row in rows:
spec=row["spec"];p=spec["params"]
key=(spec["name"],p.get("classifier"),p.get("use_hmm",True))
if key not in strata and len(choices)<count:
strata.add(key);choices.append(row)
else:deferred.append(row)
return choices+deferred