motefly / DeepGBM

SIGKDD'2019: DeepGBM: A Deep Learning Framework Distilled by GBDT for Online Prediction Tasks
647 stars 135 forks source link

Add dot cpu method for the variable 'outputs' #4

Open chengxuanying opened 5 years ago

chengxuanying commented 5 years ago
Traceback (most recent call last):
  File "main.py", line 100, in <module>
    main()
  File "main.py", line 83, in main
    train_cateModels(args, cate_data, plot_title, key="")
  File "C:\Users\ying\Desktop\DeepGBM\experiments\train_models.py", line 116, in train_cateModels
    opt, args.max_epoch, args.batch_size, 1, key)
  File "C:\Users\ying\Desktop\DeepGBM\experiments\helper.py", line 168, in TrainWithLog
    test_loss, pred_y = EvalTestset(test_x, test_y, model, args.test_batch_size, test_x_opt)
  File "C:\Users\ying\Desktop\DeepGBM\experiments\helper.py", line 89, in EvalTestset
    return sum_loss / test_len, np.concatenate(y_preds, 0)
  File "C:\Users\ying\Miniconda3\lib\site-packages\torch\tensor.py", line 458, in __array__
    return self.numpy()
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

Solution:

def EvalTestset(test_x, test_y, model, test_batch_size, test_x_opt=None):
    test_len = test_x.shape[0]
    test_num_batch = math.ceil(test_len / test_batch_size)
    sum_loss = 0.0
    y_preds = []
    model.eval()
    with torch.no_grad():
        for jdx in range(test_num_batch):
            tst_st = jdx * test_batch_size
            tst_ed = min(test_len, tst_st + test_batch_size)
            inputs = torch.from_numpy(test_x[tst_st:tst_ed].astype(np.float32)).to(device)
            if test_x_opt is not None:
                inputs_opt = torch.from_numpy(test_x_opt[tst_st:tst_ed].astype(np.float32)).to(device)
                outputs = model(inputs, inputs_opt)
            else:
                outputs = model(inputs)
            targets = torch.from_numpy(test_y[tst_st:tst_ed]).to(device)
            if isinstance(outputs, tuple):
                outputs = outputs[0]

            #########################This Line#################################
            y_preds.append(outputs.cpu()) # y_preds.append(outputs) -> y_preds.append(outputs.cpu())

            loss_tst = model.true_loss(outputs, targets).item()
            sum_loss += (tst_ed - tst_st) * loss_tst
    return sum_loss / test_len, np.concatenate(y_preds, 0)

y_preds.append(outputs) -> y_preds.append(outputs.cpu())

Aliang-CN commented 4 years ago

thx

sm807983636 commented 4 years ago

thx