コード例 #1
0
 def predict(self, model: nn.Module, 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 order: [[1,3,4], [2,5,6]]
     """
     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 = model(utils.batch_to_gpu(
             batch, model.device))['prediction']  #将batch中的数据转换为tensor
         #对象初始化调用init方法,对象传参调用__call__方法。给model中传参会调用forword方法,因为在model中继承nn.Module,
         #nn.Module中定义的__call__方法,其中调用了forword方法
         predictions.extend(prediction.cpu().data.numpy())
     return np.array(predictions)
コード例 #2
0
    def fit(self,
            model: nn.Module,
            data: BaseModel.Dataset,
            epoch=-1) -> float:
        gc.collect()  #垃圾回收
        torch.cuda.empty_cache()  #显存才会在Nvidia-smi中释放
        if model.optimizer is None:
            model.optimizer = self._build_optimizer(model)
        data.actions_before_epoch(
        )  # must sample before multi thread start  多线程启动前必须采样(负样本)

        model.train()
        loss_lst = list()
        dl = DataLoader(data,
                        batch_size=self.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,
                          desc='Epoch {:<3}'.format(epoch),
                          ncols=100,
                          mininterval=1):  #batch_size中商品的另一个标签怎么确定?
            batch = utils.batch_to_gpu(batch, model.device)
            model.optimizer.zero_grad()
            out_dict = model(batch)
            loss = model.loss(out_dict)
            loss.backward()
            model.optimizer.step()
            loss_lst.append(loss.detach().cpu().data.numpy())
        return np.mean(loss_lst).item()
コード例 #3
0
    def fit(self, data: BaseModel.Dataset, epoch=-1) -> float:
        model = data.model
        if model.optimizer is None:
            model.optimizer = self._build_optimizer(model)
        data.actions_before_epoch()  # must sample before multi thread start

        model.train()
        loss_lst = list()
        dl = DataLoader(data,
                        batch_size=self.batch_size,
                        shuffle=True,
                        num_workers=self.num_workers,
                        collate_fn=data.collate_batch,
                        pin_memory=self.pin_memory)
        for batch in tqdm(dl,
                          leave=False,
                          desc='Epoch {:<3}'.format(epoch),
                          ncols=100,
                          mininterval=1):
            batch['epoch'] = epoch
            batch = utils.batch_to_gpu(batch, model.device)
            model.optimizer.zero_grad()
            out_dict = model(batch)
            loss = model.loss(out_dict)
            loss.backward()
            model.optimizer.step()
            loss_lst.append(loss.detach().cpu().data.numpy())
        return np.mean(loss_lst).item()
コード例 #4
0
ファイル: TiMiRunner.py プロジェクト: THUwangcy/ReChorus
 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)
コード例 #5
0
ファイル: BaseRunner.py プロジェクト: youngzw/ReChorus
 def predict(self, model, data):
     """
     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 order: [[1,3,4], [2,5,6]]
     """
     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 = model(utils.batch_to_gpu(batch))
         predictions.extend(prediction.cpu().data.numpy())
     return np.array(predictions)
コード例 #6
0
ファイル: BaseRunner.py プロジェクト: youngzw/ReChorus
    def fit(self, model, data, epoch=-1):
        gc.collect()
        torch.cuda.empty_cache()
        if model.optimizer is None:
            model.optimizer = self._build_optimizer(model)
        data.negative_sampling()  # must sample before multi thread start

        model.train()
        loss_lst = list()
        dl = DataLoader(data, batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers,
                        collate_fn=data.collate_batch, pin_memory=self.pin_memory)
        for batch in tqdm(dl, leave=False, desc='Epoch {:<3}'.format(epoch), ncols=100, mininterval=1):
            batch = utils.batch_to_gpu(batch)
            model.optimizer.zero_grad()
            prediction = model(batch)
            loss = model.loss(prediction)
            loss.backward()
            model.optimizer.step()
            loss_lst.append(loss.detach().cpu().data.numpy())
        return np.mean(loss_lst)