def predict(self, data: BaseModel.Dataset): """ The returned prediction is a 2D-array, each row corresponds to all the candidates, and the ground-truth item poses the first. Example: ground-truth items: [1, 2], 2 negative items for each instance: [[3,4], [5,6]] predictions like: [[1,3,4], [2,5,6]] """ data.model.eval() predictions = list() js_div = list() dis = list() dl = DataLoader(data, batch_size=self.eval_batch_size, shuffle=False, num_workers=self.num_workers, collate_fn=data.collate_batch, pin_memory=self.pin_memory) for batch in tqdm(dl, leave=False, ncols=100, mininterval=1, desc='Predict'): out_dict = data.model(utils.batch_to_gpu(batch, data.model.device)) predictions.extend(out_dict['prediction'].cpu().data.numpy()) if 'js' in out_dict: js_div.extend(out_dict['js'].cpu().data.numpy()) if 'dis' in out_dict: dis.extend(out_dict['dis'].cpu().data.numpy()) if len(js_div) > 0: print('JS DIV:', np.mean(js_div)) np.save('../log/js', np.array(js_div)) if len(dis) > 0: print('Interest Dis:', np.mean(dis)) return np.array(predictions)
def predict(self, data: BaseModel.Dataset) -> np.ndarray: """ The returned prediction is a 2D-array, each row corresponds to all the candidates, and the ground-truth item poses the first. Example: ground-truth items: [1, 2], 2 negative items for each instance: [[3,4], [5,6]] predictions like: [[1,3,4], [2,5,6]] """ data.model.eval() predictions = list() dl = DataLoader(data, batch_size=self.eval_batch_size, shuffle=False, num_workers=self.num_workers, collate_fn=data.collate_batch, pin_memory=self.pin_memory) for batch in tqdm(dl, leave=False, ncols=100, mininterval=1, desc='Predict'): prediction = data.model( utils.batch_to_gpu(batch, data.model.device))['prediction'] predictions.extend(prediction.cpu().data.numpy()) return np.array(predictions)