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

DropBlock1D #30

Open
kayuksel opened this issue Feb 15, 2019 · 3 comments
Open

DropBlock1D #30

kayuksel opened this issue Feb 15, 2019 · 3 comments
Labels
enhancement New feature or request v0.4

Comments

@kayuksel
Copy link

Can you please also include DropBlock1D implementation to use it for time-series? Thank you very much.

@kayuksel kayuksel changed the title DropBlock2D DropBlock1D Feb 15, 2019
@kayuksel
Copy link
Author

kayuksel commented Feb 15, 2019

Would this be a right implementation?


class DropBlock1D(nn.Module):
    def __init__(self, drop_prob = 0.2, block_size = 3):
        super(DropBlock1D, self).__init__()
        self.p = drop_prob
        self.bs = block_size

    def forward(self, x):
        if not self.training: return x
        gamma = self.p / (self.bs ** 2)
        mask = (torch.rand(x.shape[0], *x.shape[2:]) < gamma)
        bm = self._compute_block_mask(mask.float().to(x.device))
        return x * bm[:, None, :] * bm.numel() / bm.sum()

    def _compute_block_mask(self, mask):
        block_mask = F.max_pool1d(input=mask[:, None, :],
        kernel_size=self.bs, stride=1, padding=self.bs // 2)
        if self.bs % 2 == 0: block_mask = block_mask[:, :, :-1]
        return (1 - block_mask.squeeze(1))

@miguelvr
Copy link
Owner

looks fine to me. you can make a PR with that code and add a few unit tests (check the current ones). I'll happily review it for you.

@miguelvr miguelvr added enhancement New feature or request v0.4 labels Feb 21, 2019
@kayuksel
Copy link
Author

@miguelvr Thanks for your response. I now checked many of the existing unit tests. Also, using it already in two projects where in both it contributed. I am not very familiar with pull requests, etc. Therefore, please feel free to use the above block within the project if you would like including it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request v0.4
Projects
None yet
Development

No branches or pull requests

2 participants