How to use the cornac.models.recommender.Recommender.fit function in cornac

To help you get started, we’ve selected a few cornac examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github PreferredAI / cornac / cornac / models / mter / recom_mter.py View on Github external
"""Fit the model to observations.

        Parameters
        ----------
        train_set: :obj:`cornac.data.Dataset`, required
            User-Item preference data as well as additional modalities.

        val_set: :obj:`cornac.data.Dataset`, optional, default: None
            User-Item preference data for model selection purposes (e.g., early stopping).

        Returns
        -------
        self : object
        """

        Recommender.fit(self, train_set, val_set)

        from ...utils import get_rng
        from ...utils.init_utils import uniform
        rng = get_rng(self.seed)

        (rating_matrix, user_item_aspect, user_aspect_opinion,
         item_aspect_opinion, user_item_pairs) = self._build_data(self.train_set)

        U_shape = (self.train_set.num_users, self.n_user_factors)
        I_shape = (self.train_set.num_items, self.n_item_factors)
        A_shape = (self.train_set.sentiment.num_aspects+1, self.n_aspect_factors)
        O_shape = (self.train_set.sentiment.num_opinions,
                   self.n_opinion_factors)
        G1_shape = (self.n_user_factors, self.n_item_factors,
                    self.n_aspect_factors)
        G2_shape = (self.n_user_factors, self.n_aspect_factors,
github PreferredAI / cornac / cornac / models / ncf / recom_neumf.py View on Github external
def fit(self, train_set, val_set=None):
        """Fit the model to observations.

        Parameters
        ----------
        train_set: :obj:`cornac.data.Dataset`, required
            User-Item preference data as well as additional modalities.

        val_set: :obj:`cornac.data.Dataset`, optional, default: None
            User-Item preference data for model selection purposes (e.g., early stopping).

        Returns
        -------
        self : object
        """
        Recommender.fit(self, train_set, val_set)

        if self.trainable:
            self._fit_neumf()

        return self
github PreferredAI / cornac / cornac / models / sorec / recom_sorec.py View on Github external
def fit(self, train_set, val_set=None):
        """Fit the model to observations.

        Parameters
        ----------
        train_set: :obj:`cornac.data.Dataset`, required
            User-Item preference data as well as additional modalities.

        val_set: :obj:`cornac.data.Dataset`, optional, default: None
            User-Item preference data for model selection purposes (e.g., early stopping).

        Returns
        -------
        self : object
        """
        Recommender.fit(self, train_set, val_set)

        import math
        from cornac.models.sorec import sorec

        if self.trainable:
            # user-item interactions
            (rat_uid, rat_iid, rat_val) = train_set.uir_tuple

            # user social network
            map_uid = train_set.user_indices
            (net_uid, net_jid, net_val) = train_set.user_graph.get_train_triplet(map_uid, map_uid)

            if self.weight_link:
                degree = train_set.user_graph.get_node_degree(map_uid, map_uid)
                weighted_net_val = []
                for u, j, val in zip(net_uid, net_jid, net_val):
github PreferredAI / cornac / cornac / models / cdr / recom_cdr.py View on Github external
def fit(self, train_set, val_set=None):
        """Fit the model to observations.

        Parameters
        ----------
        train_set: :obj:`cornac.data.Dataset`, required
            User-Item preference data as well as additional modalities.

        val_set: :obj:`cornac.data.Dataset`, optional, default: None
            User-Item preference data for model selection purposes (e.g., early stopping).

        Returns
        -------
        self : object
        """
        Recommender.fit(self, train_set, val_set)

        from ...utils import get_rng
        from ...utils.init_utils import xavier_uniform

        self.seed = get_rng(self.seed)
        self.U = self.init_params.get('U', xavier_uniform((self.train_set.num_users, self.k), self.seed))
        self.V = self.init_params.get('V', xavier_uniform((self.train_set.num_items, self.k), self.seed))

        if self.trainable:
            self._fit_cdr()

        return self
github PreferredAI / cornac / cornac / models / conv_mf / recom_convmf.py View on Github external
def fit(self, train_set, val_set=None):
        """Fit the model to observations.

        Parameters
        ----------
        train_set: :obj:`cornac.data.Dataset`, required
            User-Item preference data as well as additional modalities.

        val_set: :obj:`cornac.data.Dataset`, optional, default: None
            User-Item preference data for model selection purposes (e.g., early stopping).

        Returns
        -------
        self : object
        """
        Recommender.fit(self, train_set, val_set)

        from ...utils import get_rng
        from ...utils.init_utils import xavier_uniform

        rng = get_rng(self.seed)

        self.U = self.init_params.get('U', xavier_uniform((self.train_set.num_users, self.dimension), rng))
        self.V = self.init_params.get('V', xavier_uniform((self.train_set.num_items, self.dimension), rng))
        self.W = self.init_params.get('W', xavier_uniform((self.train_set.item_text.vocab.size, self.emb_dim), rng))

        if self.trainable:
            self._fit_convmf()

        return self
github PreferredAI / cornac / cornac / models / mf / recom_mf.py View on Github external
def fit(self, train_set):
        """Fit the model to observations.

        Parameters
        ----------
        train_set: object of type TrainSet, required
            An object contains the user-item preference in csr scipy sparse format,\
            as well as some useful attributes such as mappings to the original user/item ids.\
            Please refer to the class TrainSet in the "data" module for details.
        """

        Recommender.fit(self, train_set)

        (rid, cid, val) = sp.find(train_set.matrix)

        self.u_factors, self.i_factors, self.u_biases, self.i_biases = mf.sgd(rid=rid, cid=cid, val=val,
                                                                              num_users=train_set.num_users,
                                                                              num_items=train_set.num_items,
                                                                              num_factors=self.k,
                                                                              max_iter=self.max_iter,
                                                                              lr=self.learning_rate,
                                                                              reg=self.lambda_reg,
                                                                              mu=train_set.global_mean,
                                                                              use_bias=self.use_bias,
                                                                              early_stop=self.early_stop,
                                                                              verbose=self.verbose)
        self.fitted = True
github PreferredAI / cornac / cornac / models / ncf / recom_gmf.py View on Github external
def fit(self, train_set, val_set=None):
        """Fit the model to observations.

        Parameters
        ----------
        train_set: :obj:`cornac.data.Dataset`, required
            User-Item preference data as well as additional modalities.

        val_set: :obj:`cornac.data.Dataset`, optional, default: None
            User-Item preference data for model selection purposes (e.g., early stopping).

        Returns
        -------
        self : object
        """
        Recommender.fit(self, train_set, val_set)

        if self.trainable:
            self._fit_gmf()

        return self
github PreferredAI / cornac / cornac / models / c2pf / recom_c2pf.py View on Github external
def fit(self, train_set, val_set=None):
        """Fit the model to observations.

        Parameters
        ----------
        train_set: :obj:`cornac.data.Dataset`, required
            User-Item preference data as well as additional modalities.

        val_set: :obj:`cornac.data.Dataset`, optional, default: None
            User-Item preference data for model selection purposes (e.g., early stopping).

        Returns
        -------
        self : object
        """
        Recommender.fit(self, train_set, val_set)

        X = sp.csc_matrix(self.train_set.matrix)

        # recover the striplet sparse format from csc sparse matrix X (needed to feed c++)
        (rid, cid, val) = sp.find(X)
        val = np.array(val, dtype='float32')
        rid = np.array(rid, dtype='int32')
        cid = np.array(cid, dtype='int32')
        tX = np.concatenate((np.concatenate(([rid], [cid]), axis=0).T, val.reshape((len(val), 1))), axis=1)
        del rid, cid, val

        if self.trainable:
            map_iid = train_set.item_indices
            (rid, cid, val) = train_set.item_graph.get_train_triplet(map_iid, map_iid)
            context_info = np.hstack((rid.reshape(-1, 1), cid.reshape(-1, 1), val.reshape(-1, 1)))
github PreferredAI / cornac / cornac / models / vaecf / recom_vaecf.py View on Github external
def fit(self, train_set, val_set=None):
        """Fit the model to observations.

        Parameters
        ----------
        train_set: :obj:`cornac.data.Dataset`, required
            User-Item preference data as well as additional modalities.

        val_set: :obj:`cornac.data.Dataset`, optional, default: None
            User-Item preference data for model selection purposes (e.g., early stopping).

        Returns
        -------
        self : object
        """
        Recommender.fit(self, train_set, val_set)

        import torch
        from .vaecf import learn

        self.device = torch.device("cuda:0") if (self.use_gpu and torch.cuda.is_available()) else torch.device("cpu")

        if self.trainable:
            self.vae = learn(self.train_set, k=self.k, h=self.h, n_epochs=self.n_epochs,
                             batch_size=self.batch_size, learn_rate=self.learning_rate, beta=self.beta,
                             verbose=self.verbose, seed=self.seed, device=self.device)
        elif self.verbose:
            print('%s is trained already (trainable = False)' % (self.name))

        return self