All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ranksys.fm.PreferenceFM Maven / Gradle / Ivy

Go to download

RankSys module, providing factorisation machines for recommendation tasks using JavaFM.

The newest version!
/*
 * Copyright (C) 2016 RankSys http://ranksys.org
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.ranksys.fm;

import es.uam.eps.ir.ranksys.core.preference.IdPref;
import es.uam.eps.ir.ranksys.fast.index.FastItemIndex;
import es.uam.eps.ir.ranksys.fast.index.FastUserIndex;
import es.uam.eps.ir.ranksys.fast.preference.IdxPref;
import java.util.function.Function;
import java.util.stream.Stream;
import org.ranksys.javafm.FM;
import org.ranksys.javafm.FMInstance;

/**
 * Wraps a factorisation machine to work with RankSys user-preference pairs.
 *
 * @author Saúl Vargas ([email protected])
 * @param  type of users
 * @param  type of items
 */
public class PreferenceFM implements FastUserIndex, FastItemIndex {

    private static final double[] UI_VALUES = {1.0, 1.0};

    private final FastUserIndex ui;
    private final FastItemIndex ii;
    private final FM fm;
    private final Function, IdxPref> uPrefFun;

    /**
     * Constructor with default converter to IdxPref.
     *
     * @param users user index
     * @param items item index
     * @param fm factorisation machine
     */
    public PreferenceFM(FastUserIndex users, FastItemIndex items, FM fm) {
        this(users, items, fm, p -> new IdxPref(items.item2iidx(p)));
    }

    /**
     * Constructor with custom default converter to IdxPref.
     *
     * @param users user index
     * @param items item index
     * @param fm factorisation machine
     * @param uPrefFun converter to IdxPref
     */
    public PreferenceFM(FastUserIndex users, FastItemIndex items, FM fm, Function, IdxPref> uPrefFun) {
        this.ui = users;
        this.ii = items;
        this.fm = fm;
        this.uPrefFun = uPrefFun;
    }

    /**
     * Returns the enclosed factorisation machine.
     *
     * @return factorisation machine
     */
    public FM getFM() {
        return fm;
    }

    /**
     * Predicts the preference by a user to an item preference.
     *
     * @param u user
     * @param pref preference
     * @return predicted score
     */
    public double predict(U u, IdPref pref) {
        return predict(user2uidx(u), uPrefFun.apply(pref));
    }

    /**
     * Predicts the preference by a user to an item preference (fast version).
     *
     * @param uidx user
     * @param pref preference
     * @return predicted score
     */
    public double predict(int uidx, IdxPref pref) {
        return fm.predict(new FMInstance(pref.v2, new int[]{uidx, pref.v1 + numUsers()}, UI_VALUES));
    }

    @Override
    public int user2uidx(U u) {
        return ui.user2uidx(u);
    }

    @Override
    public U uidx2user(int i) {
        return ui.uidx2user(i);
    }

    @Override
    public boolean containsUser(U u) {
        return ui.containsUser(u);
    }

    @Override
    public int numUsers() {
        return ui.numUsers();
    }

    @Override
    public Stream getAllUsers() {
        return ui.getAllUsers();
    }

    @Override
    public int item2iidx(I i) {
        return ii.item2iidx(i);
    }

    @Override
    public I iidx2item(int i) {
        return ii.iidx2item(i);
    }

    @Override
    public boolean containsItem(I i) {
        return ii.containsItem(i);
    }

    @Override
    public int numItems() {
        return ii.numItems();
    }

    @Override
    public Stream getAllItems() {
        return ii.getAllItems();
    }

}