Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide access to average loss values from each epoch #624

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions lightfm/_lightfm_fast.pyx.template
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ cdef class FastLightFM:
cdef double item_scale
cdef double user_scale

cdef readonly double avg_loss
cdef int avg_loss_ctr

def __init__(self,
flt[:, ::1] item_features,
flt[:, ::1] item_feature_gradients,
Expand Down Expand Up @@ -258,6 +261,9 @@ cdef class FastLightFM:

self.max_sampled = max_sampled

self.avg_loss = 0.0
self.avg_loss_ctr = 0


cdef inline flt sigmoid(flt v) nogil:
"""
Expand Down Expand Up @@ -533,6 +539,9 @@ cdef inline void update(double loss,
lightfm.item_scale *= (1.0 + item_alpha * avg_learning_rate)
lightfm.user_scale *= (1.0 + user_alpha * avg_learning_rate)

lightfm.avg_loss_ctr += 1
lightfm.avg_loss = (lightfm.avg_loss * (lightfm.avg_loss_ctr - 1) + loss) / lightfm.avg_loss_ctr


cdef void warp_update(double loss,
CSRMatrix item_features,
Expand Down Expand Up @@ -648,6 +657,9 @@ cdef void warp_update(double loss,
lightfm.item_scale *= (1.0 + item_alpha * avg_learning_rate)
lightfm.user_scale *= (1.0 + user_alpha * avg_learning_rate)

lightfm.avg_loss_ctr += 1
lightfm.avg_loss = (lightfm.avg_loss * (lightfm.avg_loss_ctr - 1) + loss) / lightfm.avg_loss_ctr


cdef void regularize(FastLightFM lightfm,
double item_alpha,
Expand Down