def predict(self, sequences, item_ids=None): """ Make predictions: given a sequence of interactions, predict the next item in the sequence. Parameters ---------- sequences: array, (1 x max_sequence_length) Array containing the indices of the items in the sequence. item_ids: array (num_items x 1), optional Array containing the item ids for which prediction scores are desired. If not supplied, predictions for all items will be computed. Returns ------- predictions: array Predicted scores for all items in item_ids. """ self._net.train(False) if self._representation != "mylstm": sequences = np.atleast_2d(sequences) if item_ids is None: item_ids = np.arange(self._num_items).reshape(-1, 1) self._check_input(item_ids) self._check_input(sequences) sequences = torch.from_numpy( sequences.astype(np.int64).reshape(1, -1)) item_ids = torch.from_numpy(item_ids.astype(np.int64)) sequence_var = gpu(sequences, self._use_cuda) item_var = gpu(item_ids, self._use_cuda) _, sequence_representations = self._net.user_representation( sequence_var) size = (len(item_var), ) + sequence_representations.size()[1:] out = self._net( sequence_representations.expand(*size).unsqueeze(2), item_var) return cpu(out).detach().numpy().flatten() else: sequences = torch.from_numpy(sequences.astype(np.int64)) sequence_var = gpu(sequences, self._use_cuda) sequence_representations, _ = self._net.user_representation( sequence_var) #print("s",sequence_representations.size()) out = self._net(sequence_representations, 0) #print("o",out.size()) return cpu(out).detach().numpy()
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()
def predict(self, sequences, item_ids=None): """ Make predictions: given a sequence of interactions, predict the next item in the sequence. Parameters ---------- sequences: array, (1 x max_sequence_length) Array containing the indices of the items in the sequence. item_ids: array (num_items x 1), optional Array containing the item ids for which prediction scores are desired. If not supplied, predictions for all items will be computed. Returns ------- predictions: array Predicted scores for all items in item_ids. """ self._net.train(False) sequences = np.atleast_2d(sequences) if item_ids is None: item_ids = np.arange(self._num_items).reshape(-1, 1) self._check_input(item_ids) self._check_input(sequences) sequences = torch.from_numpy(sequences.astype(np.int64).reshape(1, -1)) item_ids = torch.from_numpy(item_ids.astype(np.int64)) sequence_var = Variable(gpu(sequences, self._use_cuda)) item_var = Variable(gpu(item_ids, self._use_cuda)) _, sequence_representations = self._net.user_representation( sequence_var) out = self._net(sequence_representations.repeat(len(item_var), 1), item_var) return cpu(out.data).numpy().flatten()
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. """ if item_ids is None: item_ids = np.arange(self._num_items) if isinstance(user_ids, int): user_ids = np.repeat(user_ids, len(item_ids)) self._check_input(user_ids, item_ids) user_ids = torch.from_numpy(user_ids.reshape(-1, 1).astype(np.int64)) item_ids = torch.from_numpy(item_ids.reshape(-1, 1).astype(np.int64)) user_var = Variable(gpu(user_ids, self._use_cuda)) item_var = Variable(gpu(item_ids, self._use_cuda)) out = self._net(user_var, item_var) return cpu(out.data).numpy().flatten()
def predict(self, sequences, item_ids=None): """ Make predictions: given a sequence of interactions, predict the next item in the sequence. Parameters ---------- sequences: array, (1 x max_sequence_length) Array containing the indices of the items in the sequence. item_ids: array (num_items x 1), optional Array containing the item ids for which prediction scores are desired. If not supplied, predictions for all items will be computed. Returns ------- predictions: array Predicted scores for all items in item_ids. """ self._net.train(False) sequences = np.atleast_2d(sequences) if item_ids is None: item_ids = np.arange(self._num_items).reshape(-1, 1) sequences = torch.from_numpy(sequences.astype(np.int64).reshape(1, -1)) item_ids = torch.from_numpy(item_ids.astype(np.int64)) sequence_var = Variable(gpu(sequences, self._use_cuda)) item_var = Variable(gpu(item_ids, self._use_cuda)) _, sequence_representations = self._net.user_representation(sequence_var) out = self._net(sequence_representations.repeat(len(item_var), 1), item_var) return cpu(out.data).numpy().flatten()
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. """ if item_ids is None: item_ids = np.arange(self._num_items) if isinstance(user_ids, int): user_ids = np.repeat(user_ids, len(item_ids)) user_ids = torch.from_numpy(user_ids.reshape(-1, 1).astype(np.int64)) item_ids = torch.from_numpy(item_ids.reshape(-1, 1).astype(np.int64)) user_var = Variable(gpu(user_ids, self._use_cuda)) item_var = Variable(gpu(item_ids, self._use_cuda)) out = self._net(user_var, item_var) return cpu(out.data).numpy().flatten()