If else loop problem in nn.module


I am having a small problem in my code snippet below. Normally, when to end the if loop, I write pass. But, here neither pass nor break works. I get an invalid syntax. I do not understand why I cannot let the code else do nothing. Could anyone find my mistake here please? Thanks!

> class ConvBlock(nn.Module):
>     def __init__(self, in_channels, out_channels, down=True, use_act=True, **kwargs):
>         super().__init__()
>         self.conv = nn.Sequential(
>             nn.Conv1d(in_channels, out_channels, padding_mode="reflect", **kwargs)
>             if down
>             else nn.ConvTranspose1d(in_channels, out_channels, **kwargs),
>             nn.Dropout()
>             if drop
>             pass,
>             nn.BatchNorm1d(out_channels, affine=True),
>             nn.Mish(out_channels) if use_act else nn.Identity()
>             )

Probably there is an else case missing here.
You could do

nn.Dropout() if drop else nn.Identity(),
1 Like

I can’t completely understand the operation that nn.Identity() does. But I know it will change the output.

Here is the documentation where you can see that the output should be the same as the input.

But if you want to be sure, you can take a look at the source code and see that the output is the same as the input.

1 Like