Using a sparse coo matrix with Ray Tune

Hello,

I’m using an algorithm that is called lightfm and it takes sparse matrices as input and it does some conversions to scipy coo matrices behind the scenes. So, I don’t have control over this conversion part.

When trying to do hyperparameter optimization, I get the following error

AttributeError: 'ray._raylet.ObjectRef' object has no attribute 'tocoo'

How can I solve this issue ? Here is the full code

from ray import air, tune, put
from ray.tune.search.optuna import OptunaSearch

# train interactions, test_interations & dataset_item_features are sparse matrices
ray_train_interactions = put(train_interactions)
ray_test_interactions = put(test_interactions)
ray_dataset_item_features = put(dataset_item_features)


def train_lightfm(config):
    
    model = LightFM(no_components=config["components"], 
                learning_rate=config["lr"],
                learning_schedule="adagrad",
                item_alpha=config["item_alpha"],
                loss="warp")
    
    for i in range(30):
        model = model.fit_partial(ray_train_interactions,
                        epochs=1, 
                        item_features=ray_dataset_item_features,
                        num_threads=2)
        maptk = precision_at_k(model, ray_test_interactions, item_features = ray_dataset_item_features, k=5).mean()
        out = -maptk
        
        # Handle some weird numerical going on
        if np.abs(out + 1) < 0.01 or out < -1.0:
            out = 0
        
        tune.report(out) 


search_space = {
    "lr": tune.loguniform(1e-5, 5e-1),
    "components": tune.uniform(10, 100),
    "item_alpha": tune.loguniform(1e-4, 5e-1)
}

tuner = tune.Tuner(
    train_lightfm,
    param_space=search_space,
    tune_config=tune.TuneConfig(
        search_alg=optuna_search,
    ),
)

results = tuner.fit()

Thanks

Maybe you should ask this question in Ray AIR cateogry…

cc: @kai @matthewdeng