TorchEnsemble-Community / Ensemble-Pytorch

A unified ensemble framework for PyTorch to improve the performance and robustness of your deep learning model.
https://ensemble-pytorch.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.08k stars 95 forks source link

Examples on features and labels #135

Closed binunalex closed 1 year ago

binunalex commented 1 year ago

Dear Friends, Please explain to me how to specify features and labels in the train loader.

Also, which input should be fed to model.predict(...) ?I obtain the answer like ValueError: The type of input X should be one of {{torch.Tensor, np.ndarray}}.

I would be grateful for working examples.

Sincerely yours, Alex Binun

xuyxu commented 1 year ago

how to specify features and labels in the train loader

Please refer to the example here.

which input should be fed to model.predict(...)

predict accepts a tensor or numpy array that corresponds to a batch of data. If you want to directly use the dataloader, please use evaluate instead.

evelyn0414 commented 1 year ago

Hi Alex, have you figured out what to do to solve the problem? I'm getting exactly the same problem as you did. My data is loaded from another library and each batch looks like this:

[tensor([[ 0.3166,  0.5657, -0.6852,  0.1726, -0.4200,  1.4839, -0.7935, -0.1271,
          2.0005, -0.5244, -1.0496, -0.4166, -0.5625],
        [-0.9301,  0.5657, -0.6852,  0.4608, -0.4200,  1.2923, -0.7935, -0.8732,
          2.0005, -0.5244, -1.0496, -0.4166, -0.5625],
        [-0.5145,  0.5657, -1.2063,  0.0978, -0.4200,  1.1006, -0.7935,  0.0595,
          2.0005, -0.5244, -1.0496, -0.4166, -0.5625],
        [ 0.7321,  0.5657, -0.1641, -0.1477, -0.4200, -0.2791,  1.2577,  1.3653,
         -0.4988, -0.5244,  0.9508, -0.4166,  1.7741]]), tensor([[0.],
        [0.],
        [1.],
        [1.]])]

It gets this same error when trying to make prediction using test loader. With train loader it seemed to be working but generated logs with weird results like "Correct: 12/4" so I don't think it was training properly either.

I'm wondering if you could kindly instruct what to do here. @xuyxu

Best, Evelyn

xuyxu commented 1 year ago

Hi @evelyn0414, thanks for reporting. Could you provide a code snippet that reproduces the problem, so that I can take a closer look.

binunalex commented 1 year ago

Evelyn , good afternoon, What I did is a slight correction in the file GradientBoostingClassifier https://github.com/TorchEnsemble-Community/Ensemble-Pytorch/blob/master/torchensemble/gradient_boosting.py, Line 425: the statement *F.softmax(output, dim=1*) is replaced by F.softmax(output)

That is, the "dim" parameter is removed.

By the way, which version of PyTorch and Python are used by you ? Mine are Python 3.8, Pytorch 1.12.1.

Best Regards, Alex

On Sat, Dec 24, 2022 at 1:22 AM Evelyn Zhang @.***> wrote:

Hi Alex, have you figured out what to do to solve the problem? I'm getting exactly the same problem as you did. My data is loaded from another library and each batch looks like this:

[tensor([[ 0.3166, 0.5657, -0.6852, 0.1726, -0.4200, 1.4839, -0.7935, -0.1271, 2.0005, -0.5244, -1.0496, -0.4166, -0.5625], [-0.9301, 0.5657, -0.6852, 0.4608, -0.4200, 1.2923, -0.7935, -0.8732, 2.0005, -0.5244, -1.0496, -0.4166, -0.5625], [-0.5145, 0.5657, -1.2063, 0.0978, -0.4200, 1.1006, -0.7935, 0.0595, 2.0005, -0.5244, -1.0496, -0.4166, -0.5625], [ 0.7321, 0.5657, -0.1641, -0.1477, -0.4200, -0.2791, 1.2577, 1.3653, -0.4988, -0.5244, 0.9508, -0.4166, 1.7741]]), tensor([[0.], [0.], [1.], [1.]])]

It gets this same error when trying to make prediction using test loader. With train loader it seemed to be working but generated logs with weird results like "Correct: 12/4" so I don't think it was training properly either.

I'm wondering if you could kindly instruct what to do here. @xuyxu https://github.com/xuyxu

Best, Evelyn

— Reply to this email directly, view it on GitHub https://github.com/TorchEnsemble-Community/Ensemble-Pytorch/issues/135#issuecomment-1364393480, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMMCYXLDQGDRCNTVUQ4RTG3WOYXZ3ANCNFSM6AAAAAASPYOVPE . You are receiving this because you authored the thread.Message ID: @.*** com>

evelyn0414 commented 1 year ago

Thanks Alex! I will investigate and try out your correction! Mine are Python 3.8.13, Pytorch 1.13.0.

Also thank you @xuyxu Here's the code snippet.

model = VotingClassifier(
        estimator=Baseline(),
        n_estimators=1,
        cuda=False,
    )

    criterion = nn.BCELoss()
    model.set_criterion(criterion)

    model.set_optimizer('Adam',  # parameter optimizer
                        lr=0.001,  # learning rate of the optimizer
                        weight_decay=5e-4)  # weight decay of the optimizer

    # Training
    model.fit(train_loader=train_loader,  # training data
              epochs=NUM_EPOCHS_POOLED)                 # the number of training epochs

    # Evaluating
    accuracy = model.predict(test_loader)
    print(accuracy)

in which Baseline() is defined as:

class Baseline(nn.Module):
    def __init__(self, input_dim=13, output_dim=1):
        super(Baseline, self).__init__()
        self.linear = torch.nn.Linear(input_dim, output_dim)

    def forward(self, x):
        return torch.sigmoid(self.linear(x))

Thank you both very much!!

xuyxu commented 1 year ago

Thanks @evelyn0414, @binunalex, will take a look at how to fix this problem these days.