Skip to content

Commit

Permalink
WIP Debugging pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
lenhoanglnh committed Jan 5, 2025
1 parent 2142346 commit 84d6639
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 50 deletions.
14 changes: 10 additions & 4 deletions solidago/src/solidago/pipeline/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,16 @@ def json_keys(self) -> list:
def __str__(self) -> str:
return repr(self)

def __repr__(self) -> str:
return f"{type(self).__name__}(\n\t" + "\n\t".join([
f"{key}={getattr(self, key)}" for key in self.json_keys()
]) + "\n)"
def __repr__(self, n_indents: int=0) -> str:
def sub_repr(key):
value = getattr(self, key)
return value.__repr__(n_indents + 1) if isinstance(value, StateFunction) else value

indent = "\t" * (n_indents + 1)
last_indent = "\t" * n_indents
return f"{type(self).__name__}(\n{indent}" + f",\n{indent}".join([
f"{key}={sub_repr(key)}" for key in self.json_keys()
]) + f"\n{last_indent})"

def to_json(self):
return type(self).__name__, { key: getattr(self, key) for key in self.json_keys() }
2 changes: 1 addition & 1 deletion solidago/src/solidago/pipeline/scaling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"""

from .mehestan import Mehestan
from .quantile_zero_shift import QuantileShift, QuantileZeroShift
from .quantile_shift import QuantileShift, QuantileZeroShift
from .standardize import Standardize
24 changes: 12 additions & 12 deletions solidago/src/solidago/pipeline/scaling/mehestan.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@

class Mehestan(StateFunction):
def __init__(self,
lipschitz=0.1,
min_activity=10.0,
n_scalers_max=100,
privacy_penalty=0.5,
large_number_of_activities=1000,
user_comparison_lipschitz=10.0,
p_norm_for_multiplicative_resilience=4.0,
n_entity_to_fully_compare_max=100,
n_diffs_sample_max=1000,
default_multiplicator_dev=0.8,
default_translation_dev=1.,
error=1e-5
lipschitz: float=0.1,
min_activity: float=10.0,
n_scalers_max: float=100,
privacy_penalty: float=0.5,
large_number_of_activities: float=1000,
user_comparison_lipschitz: float=10.0,
p_norm_for_multiplicative_resilience: float=4.0,
n_entity_to_fully_compare_max: float=100,
n_diffs_sample_max: float=1000,
default_multiplicator_dev: float=0.8,
default_translation_dev: float=1.,
error: float=1e-5
):
""" Mehestan performs Lipschitz-resilient collaborative scaling.
It is based on "Robust Sparse Voting", by Youssef Allouah,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,10 @@ def __call__(self, entities: Entities, user_models: UserModels) -> UserModels:


class QuantileZeroShift(QuantileShift):
def __init__(
self,
def __init__(self,
zero_quantile: float = 0.15,
lipschitz: float = 0.1,
error: float = 0.00001
):
super().__init__(zero_quantile, target_score=0.0, lipschitz=lipschitz, error=error)

def to_json(self):
return type(self).__name__, dict(
zero_quantile=self.quantile,
lipschitz=self.lipschitz,
error=self.error
)
14 changes: 13 additions & 1 deletion solidago/src/solidago/pipeline/sequential.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ class Sequential(StateFunction):
def __init__(self, **kwargs):
super().__init__()
for key, value in kwargs.items():
setattr(self, key, value)
if isinstance(value, StateFunction):
setattr(self, key, value)
elif isinstance(value, (list, tuple)) and len(value) == 2:
import solidago.pipeline as pipeline
setattr(self, key, getattr(pipeline, value[0])(**value[1]))
else:
print(f"Sequential.__init__: Got unhandled input key={key}, type(value)={type(value).__name__}")

@property
def modules(self):
Expand Down Expand Up @@ -48,3 +54,9 @@ def load(cls, d: Union[dict, str]) -> "Sequential":
d = json.load(d)
import solidago.pipeline as pipeline
return cls(**{ key: getattr(pipeline, d[key][0])(**d[key][1]) for key in d })

def json_keys(self) -> list:
return list(
key for key in self.__dict__
if key[0] != "_" and hasattr(self, key)
)
9 changes: 5 additions & 4 deletions solidago/src/solidago/state/models/user_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@ def default_value(self) -> ScoringModel:
def score(self, entity: Union[str, "Entity", "Entities"]) -> MultiScore:
from solidago.state import Entity, Entities
if isinstance(entity, (str, Entity)):
result = NestedDictOfTuples(key_names=["username", "criterion"])
result = MultiScore(key_names=["username", "criterion"])
for username, model in self:
multiscore = model(entity)
for criterion, score in multiscore:
result[username, criterion] = score.to_triplet()
result[username, criterion] = score
return result
assert isinstance(entity, Entities)
entities = entity
result = NestedDictOfTuples(key_names=["username", "entity_name", "criterion"])
result = MultiScore(key_names=["username", "entity_name", "criterion"])
for username, model in self:
for entity in model.evaluated_entities(entities):
multiscore = model(entity)
for criterion, score in multiscore:
result[username, str(entity), criterion] = score.to_triplet()
result[username, str(entity), criterion] = score
return result

def __getitem__(self, user: Union[str, "User"]) -> ScoringModel:
if str(user) not in self.keys():
Expand Down
45 changes: 25 additions & 20 deletions solidago/tests/pipeline/test_pipeline.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,31 @@
"overtrust_ratio": 0.1
}],
"scaling": ["Sequential", {
"collaborative_scaling": ["Mehestan", {
"lipschitz": 1,
"min_activity": 1,
"n_scalers_max": 100,
"privacy_penalty": 0.5,
"p_norm_for_multiplicative_resilience": 4.0,
"error": 1e-05
}],
"common_translation": ["QuantileZeroShift", {
"zero_quantile": 0.15,
"lipschitz": 0.1,
"error": 1e-05
}],
"common_multiplicator": ["Standardize", {
"zero_quantile": 0.15,
"lipschitz": 0.1,
"error": 1e-05
}]
}
],
"collaborative_scaling": ["Mehestan", {
"lipschitz": 1,
"min_activity": 1,
"n_scalers_max": 100,
"privacy_penalty": 0.5,
"large_number_of_activities": 1000,
"user_comparison_lipschitz": 10.0,
"p_norm_for_multiplicative_resilience": 4.0,
"n_entity_to_fully_compare_max": 100,
"n_diffs_sample_max": 1000,
"default_multiplicator_dev": 0.8,
"default_translation_dev": 1.0,
"error": 1e-05
}],
"common_translation": ["QuantileZeroShift", {
"zero_quantile": 0.15,
"lipschitz": 0.1,
"error": 1e-05
}],
"common_multiplicator": ["Standardize", {
"dev_quantile": 0.9,
"lipschitz": 0.1,
"error": 1e-05
}]
}],
"aggregation": ["EntitywiseQrQuantile", {
"quantile": 0.2,
"lipschitz": 0.1,
Expand Down

0 comments on commit 84d6639

Please sign in to comment.