Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions hobj/benchmarks/make_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
This module provides an alternative interface for instantiating a linear learning model.
"""
from hobj.learning_models.linear import LinearLearner, RepresentationalModel
import hobj.learning_models.linear.update_rules as update_rules
from typing import Literal, Dict
import mref
import numpy as np
from typing import List


# %%
def make_linear_learner_from_features(
ref_to_features: Dict[mref.ImageRef, np.ndarray],
calibration_images: List[mref.ImageRef],
update_rule_name: Literal[
'Prototype',
'Square',
'Perceptron',
'Hinge',
'MAE',
'Exponential',
'CE',
'REINFORCE'
] = 'Square',
alpha: float = 1,
) -> LinearLearner:
"""
Instantiates a linear learning model from precomputed features.
:param ref_to_features: Dict[mref.ImageRef, np.ndarray], the features to use.
:param calibration_images: List[mref.ImageRef], the images that will be used to calibrate the features (i.e. for mean centering and ensuring they fit within a unit ball).
:param update_rule_name: str, the name of the update rule to use.
:param alpha: float, the learning rate.
:return: LinearLearner
"""

f_calibration = np.array([ref_to_features[ref] for ref in calibration_images])
mu_calibration = np.mean(f_calibration, axis=0)
norms_calibration = np.linalg.norm(f_calibration - mu_calibration, axis=1)
norm_cutoff = np.quantile(norms_calibration, 0.999) # Will clip the rest

ref_to_calibrated_features = {}
for ref in ref_to_features:
f = ref_to_features[ref]
fc = f - mu_calibration
fcn = fc / norm_cutoff
norm_cur = np.linalg.norm(fcn)
if norm_cur > 1:
fcn = fcn / norm_cur
ref_to_calibrated_features[ref] = np.array(fcn)

update_rule_name = getattr(update_rules, update_rule_name)
return LinearLearner(
representational_model=RepresentationalModel.from_precomputed_features(
image_ref_to_features=ref_to_calibrated_features
),
update_rule=update_rule_name(alpha=alpha)
)
4 changes: 4 additions & 0 deletions hobj/benchmarks/mut_highvar_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ def __init__(self):
super().__init__(
config=config
)

if __name__ == '__main__':
experiment = MutatorHighVarBenchmark()
print(sorted(experiment.config.subtask_name_to_data.keys()))
2 changes: 1 addition & 1 deletion hobj/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from pathlib import Path

cachedir: Path = Path.home() / 'hobj_cache2'
cachedir: Path = Path.home() / 'hobj_cache'
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies = [
"pydantic>=2.10",
"xarray>=2025.1",
"pytest>=8.3",
#"mref @ git+https://github.com/himjl/mref.git",
"mref @ git+https://github.com/himjl/mref.git",
]

[project.urls]
Expand Down
Loading