Skip to content

Commit

Permalink
Improve recency weighting (#154)
Browse files Browse the repository at this point in the history
* improve recency weighting

* bump version

* apply weights on evaluation
  • Loading branch information
L-M-Sherlock authored Dec 23, 2024
1 parent 9b9b700 commit 4970509
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "FSRS-Optimizer"
version = "5.6.0"
version = "5.6.1"
readme = "README.md"
dependencies = [
"matplotlib>=3.7.0",
Expand Down
17 changes: 14 additions & 3 deletions src/fsrs_optimizer/fsrs_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,8 @@ def train(
plots.append(trainer.plot())
else:
if recency_weight:
self.dataset["weights"] = np.linspace(0.5, 1.5, len(self.dataset))
x = np.linspace(0, 1, len(self.dataset))
self.dataset["weights"] = 0.25 + 0.75 * np.power(x, 3)
trainer = Trainer(
self.dataset,
None,
Expand Down Expand Up @@ -1541,6 +1542,11 @@ def evaluate(self, save_to_file=True):
lambda row: -np.log(row["p"]) if row["y"] == 1 else -np.log(1 - row["p"]),
axis=1,
)
self.dataset["log_loss"] = (
self.dataset["log_loss"]
* self.dataset["weights"]
/ self.dataset["weights"].mean()
)
loss_before = self.dataset["log_loss"].mean()

my_collection = Collection(self.w, self.float_delta_t)
Expand All @@ -1554,6 +1560,11 @@ def evaluate(self, save_to_file=True):
lambda row: -np.log(row["p"]) if row["y"] == 1 else -np.log(1 - row["p"]),
axis=1,
)
self.dataset["log_loss"] = (
self.dataset["log_loss"]
* self.dataset["weights"]
/ self.dataset["weights"].mean()
)
loss_after = self.dataset["log_loss"].mean()
if save_to_file:
tmp = self.dataset.copy()
Expand Down Expand Up @@ -2100,10 +2111,10 @@ def count_lapse(r_history, t_history):
)
tmp = (
tmp.groupby(["delta_t", "i", "lapse"])
.agg({"y": "mean", "p": "mean", "card_id": "count"})
.agg({"y": "mean", "p": "mean", "weights": "sum"})
.reset_index()
)
return root_mean_squared_error(tmp["y"], tmp["p"], sample_weight=tmp["card_id"])
return root_mean_squared_error(tmp["y"], tmp["p"], sample_weight=tmp["weights"])


def wrap_short_term_ratings(r_history, t_history):
Expand Down

0 comments on commit 4970509

Please sign in to comment.