-
Notifications
You must be signed in to change notification settings - Fork 15.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from VowpalWabbit/fix_embedding_w_indexes
proper embeddings and rolling window average
- Loading branch information
Showing
6 changed files
with
184 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,66 @@ | ||
from typing import TYPE_CHECKING, Dict, List, Optional, Union | ||
from collections import deque | ||
from typing import TYPE_CHECKING, Dict, List, Union | ||
|
||
if TYPE_CHECKING: | ||
import pandas as pd | ||
|
||
|
||
class MetricsTracker: | ||
class MetricsTrackerAverage: | ||
def __init__(self, step: int): | ||
self._history: List[Dict[str, Union[int, float]]] = [] | ||
self._step: int = step | ||
self._i: int = 0 | ||
self._num: float = 0 | ||
self._denom: float = 0 | ||
self.history: List[Dict[str, Union[int, float]]] = [{"step": 0, "score": 0}] | ||
self.step: int = step | ||
self.i: int = 0 | ||
self.num: float = 0 | ||
self.denom: float = 0 | ||
|
||
@property | ||
def score(self) -> float: | ||
return self._num / self._denom if self._denom > 0 else 0 | ||
return self.num / self.denom if self.denom > 0 else 0 | ||
|
||
def on_decision(self) -> None: | ||
self._denom += 1 | ||
self.denom += 1 | ||
|
||
def on_feedback(self, score: Optional[float]) -> None: | ||
self._num += score or 0 | ||
self._i += 1 | ||
if self._step > 0 and self._i % self._step == 0: | ||
self._history.append({"step": self._i, "score": self.score}) | ||
def on_feedback(self, score: float) -> None: | ||
self.num += score or 0 | ||
self.i += 1 | ||
if self.step > 0 and self.i % self.step == 0: | ||
self.history.append({"step": self.i, "score": self.score}) | ||
|
||
def to_pandas(self) -> "pd.DataFrame": | ||
import pandas as pd | ||
|
||
return pd.DataFrame(self._history) | ||
return pd.DataFrame(self.history) | ||
|
||
|
||
class MetricsTrackerRollingWindow: | ||
def __init__(self, window_size: int, step: int): | ||
self.history: List[Dict[str, Union[int, float]]] = [{"step": 0, "score": 0}] | ||
self.step: int = step | ||
self.i: int = 0 | ||
self.window_size: int = window_size | ||
self.queue: deque = deque() | ||
self.sum: float = 0.0 | ||
|
||
@property | ||
def score(self) -> float: | ||
return self.sum / len(self.queue) if len(self.queue) > 0 else 0 | ||
|
||
def on_decision(self) -> None: | ||
pass | ||
|
||
def on_feedback(self, value: float) -> None: | ||
self.sum += value | ||
self.queue.append(value) | ||
self.i += 1 | ||
|
||
if len(self.queue) > self.window_size: | ||
old_val = self.queue.popleft() | ||
self.sum -= old_val | ||
|
||
if self.step > 0 and self.i % self.step == 0: | ||
self.history.append({"step": self.i, "score": self.sum / len(self.queue)}) | ||
|
||
def to_pandas(self) -> "pd.DataFrame": | ||
import pandas as pd | ||
|
||
return pd.DataFrame(self.history) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.