Exemplo n.º 1
0
    def predict(self, user_ids, item_ids=None):
        """
        Make predictions: given a user id, compute the recommendation
        scores for items.

        Parameters
        ----------

        user_ids: int or array
           If int, will predict the recommendation scores for this
           user for all items in item_ids. If an array, will predict
           scores for all (user, item) pairs defined by user_ids and
           item_ids.
        item_ids: array, optional
            Array containing the item ids for which prediction scores
            are desired. If not supplied, predictions for all items
            will be computed.

        Returns
        -------

        predictions: np.array
            Predicted scores for all items in item_ids.
        """
        user_ids, item_ids = _predict_process_ids(user_ids, item_ids,
                                                  self.num_items, False)

        return np.random.random(size=len(item_ids))
Exemplo n.º 2
0
    def predict(self, user_ids, item_ids=None):
        """
        Make predictions: given a user id, compute the recommendation
        scores for items.

        Parameters
        ----------

        user_ids: int or array
           If int, will predict the recommendation scores for this
           user for all items in item_ids. If an array, will predict
           scores for all (user, item) pairs defined by user_ids and
           item_ids.
        item_ids: array, optional
            Array containing the item ids for which prediction scores
            are desired. If not supplied, predictions for all items
            will be computed.

        Returns
        -------

        predictions: np.array
            Predicted scores for all items in item_ids.
        """

        self._check_input(user_ids, item_ids, allow_items_none=True)
        self._net.train(False)

        user_ids, item_ids = _predict_process_ids(user_ids, item_ids,
                                                  self._num_items,
                                                  self._use_cuda)

        out = self._net(user_ids, item_ids)

        return cpu(out).detach().numpy().flatten()
Exemplo n.º 3
0
    def predict(self, user_ids, item_ids=None):

        """
        Make predictions: given a user id, compute the recommendation
        scores for items.

        Parameters
        ----------

        user_ids: int or array
           If int, will predict the recommendation scores for this
           user for all items in item_ids. If an array, will predict
           scores for all (user, item) pairs defined by user_ids and
           item_ids.
        item_ids: array, optional
            Array containing the item ids for which prediction scores
            are desired. If not supplied, predictions for all items
            will be computed.

        Returns
        -------

        predictions: np.array
            Predicted scores for all items in item_ids.

        a[a > 0.5] = 1
        a[a <= 0.5] = 0
        """

        self._check_input(user_ids, item_ids, allow_items_none=True)
        self._net.train(False)

        user_ids, item_ids = _predict_process_ids(user_ids, item_ids,
                                                  self._num_items,
                                                  self._use_cuda)

        totQuery = int(user_ids.size()[0])
        # pdb.Pdb.complete = rlcompleter.Completer(locals()).complete
        # pdb.set_trace()

        uniqItems = np.unique(item_ids.data.numpy())
        totUniqItems = uniqItems.shape[0]

        pred = []

        user_ids = user_ids.data.numpy()
        item_ids = item_ids.data.numpy()

        # pdb.Pdb.complete =rlcompleter.Completer(locals()).complete
        # pdb.set_trace()

        for qID in range(totQuery):
            u = int(user_ids[qID])
            i1 = int(item_ids[qID])
            items2 = np.unique(np.random.randint(0, totUniqItems, size=self.k_sample))
            t1 = torch.LongTensor([u, i1])
            t1 = t1.repeat(items2.shape[0], 1)
            t2 = torch.LongTensor([items2])
            t2 = torch.transpose(t2, 0, 1)
            t3 = torch.cat((t1, t2), 1)
            y = self._net(t3)

            # new change
            y = y.data
            y[y > 0.5] = 1.0
            y[y <= 0.5] = 0.0
            i1_score = (y.sum()/self.k_sample)*5
            pred.append(int(i1_score))
            # old
            # pred.append(y.mean().data[0])

        # pdb.Pdb.complete =rlcompleter.Completer(locals()).complete
        # pdb.set_trace()
        return np.array(pred)