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

Fit callbacks #145

Open
wants to merge 3 commits into
base: feature_jsonSerialization
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion fastFM-core2
4 changes: 2 additions & 2 deletions fastFM/als.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self, n_iter=100, init_stdev=0.1, rank=8, random_state=123,
self.solver = "cd"
self.iter_count = 0

def fit(self, X, y, n_more_iter=0):
def fit(self, X, y, n_more_iter=0, callback = None):
""" Fit model with specified loss.

Parameters
Expand Down Expand Up @@ -97,7 +97,7 @@ def fit(self, X, y, n_more_iter=0):

settings_dict = _settings_factory(self)
ffm2.ffm_fit(self.w0_, self.w_, self.V_, X, y, self.rank,
settings_dict)
settings_dict, callback)

self.iter_count += self.n_iter
return self
Expand Down
6 changes: 6 additions & 0 deletions fastFM/cpp_ffm.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ cdef extern from "../fastFM-core2/fastFM/fastfm.h" namespace "fastfm":

cdef void fit(Settings* s, Model* m, Data* d)

# NEW CALLBACK DEVELOPMENTS #########################################
ctypedef void (*fit_callback_t)(int iter, void* python_func)
cdef void fit_with_callback(Settings* s, Model* m, Data*,
fit_callback_t callback, void* python_callback_func)

######################################################################
cdef void predict(Model* m, Data* d)
20 changes: 19 additions & 1 deletion fastFM/ffm2.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,25 @@ def ffm_predict(np.ndarray[np.float64_t, ndim = 1] w_0,

return y

# The main piece of the glue between Python, Cython and C++
# In essence it wraps the python function so it can be used in C++ space.
# Convert the python function from a pointer back into a python object and invoke
# with other parameters. (For now just one, `current_iter`)
cdef void fit_callback_wrapper(int current_iter, void* python_function):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thoughts on this:

  • callback is not None. If callback is None inform C++ side somehow so no cycles are wasted invoking the callback. This might be harder than it sounds.
  • callback corresponds to fit_callback_t. One way to check is to catch TypeError which is raised if a function is invoked with less or more than the required number of arguments.
  • passing all parameters as dict and invoking the python callback via **kwargs for greater flexibility

f = (<object>python_function)

if f is not None:
f(current_iter)

from typing import Callable, NoReturn

def ffm_fit(np.ndarray[np.float64_t, ndim = 1] w_0,
np.ndarray[np.float64_t, ndim = 1] w,
np.ndarray[np.float64_t, ndim = 2] V,
X, np.ndarray[np.float64_t, ndim = 1] y, int rank, dict settings):
X, np.ndarray[np.float64_t, ndim = 1] y,
int rank, dict settings,
# callback is a python object
callback : Callable[int, NoReturn]):
assert isinstance(settings, dict)
assert X.shape[0] == len(y) # test shapes

Expand All @@ -96,6 +110,10 @@ def ffm_fit(np.ndarray[np.float64_t, ndim = 1] w_0,

cpp_ffm.fit(s, m, d)

# Proof of concept `dummy fit` with a callback.
# we must cast the python object into void* so it can be passed to C++
cpp_ffm.fit_with_callback(s, m, d, fit_callback_wrapper, (<void*> callback))

del d
del m
del s
Expand Down