Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Votings supported #118

Open
London38 opened this issue Jun 15, 2022 · 5 comments
Open

Votings supported #118

London38 opened this issue Jun 15, 2022 · 5 comments
Labels
new feature Feature request to work on

Comments

@London38
Copy link

hi @xuyxu, I've been taking a look at Voting.py, If I'm not mistaken, only soft voting is implemented. are there any plans for implementing majority/hard, plurality and weighted voting as well?

Thanks in advance

@xuyxu
Copy link
Member

xuyxu commented Jun 16, 2022

Hi @London38, great idea! It would be nice if we could support various sub-type of voting during the inference stage. I will work on it when I get a moment ;-)

@xuyxu xuyxu added the new feature Feature request to work on label Jun 16, 2022
@London38
Copy link
Author

London38 commented Jun 17, 2022

Thanks @xuyxu , the source where I read about them is: Ensemble Methods: Foundations and Algorithms. Zhou, Zhi-Hua. Page 71. Here relativly new one: https://machinelearningmastery.com/horizontal-voting-ensemble/:
Edit: more info
imagen

@gardberg
Copy link
Contributor

Hi, @xuyxu I wanted to give implementing another voting strategy a try, but am a bit unsure of what needs to be changed (I have never contributed before :) ). I started implementing majority voting in the forward method of the VotingClassifier class, but realized the averaging is implemented in some other placed as well, such as the validation part of the training loop. Would you or anyone be able to give me some pointers as to how to structure it? My idea is that you would want to pass a voting_strategy parameter to the constructor of the VotingClassifier. I've pasted a rough idea of it below.

@torchensemble_model_doc(
    """Implementation on the VotingClassifier.""", "model"
)
class VotingClassifier(BaseClassifier):

    def __init__(self, voting_strategy="soft", **kwargs):
        super(VotingClassifier, self).__init__(**kwargs)

        self.voting_strategy = voting_strategy

    @torchensemble_model_doc(
        """Implementation on the data forwarding in VotingClassifier.""",
        "classifier_forward",
    )
    def forward(self, *x):
        # Average over class distributions from all base estimators.

        # output: (n_estimators, batch_size, n_classes)
        outputs = [
            F.softmax(estimator(*x), dim=1) for estimator in self.estimators_
        ]

        # This is where another voting strategy can be implemented.
        if self.voting_strategy == "soft": proba = op.average(outputs)
        elif self.voting_strategy == "hard":
            # Do hard majority voting
            # votes: (batch_size)
            votes = torch.stack(outputs).argmax(dim=2).mode(dim=0)[0]
            # Set the probability of the most voted class to 1
            proba = torch.zeros_like(outputs[0])
            proba.scatter_(1, votes.view(-1, 1), 1)

        # Returns averaged class probabilities for each sample
        # proba shape: (batch_size, n_classes) 
        return proba

@xuyxu
Copy link
Member

xuyxu commented Sep 20, 2022

Hi @LukasGardberg, thanks for your contribution!

Is it enough to add the same code snippet to the _forward function in fit method (Line171)?

Maybe you could open a pull request first, and we can implement this feature request step by step :D

@gardberg
Copy link
Contributor

@xuyxu Cool, sure, I just opened a pull request with a first idea :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature Feature request to work on
Projects
None yet
Development

No branches or pull requests

3 participants