Yes, you can write the script without using the * (unpacking operator) by directly passing the list comprehension to nn.Sequential without unpacking it. Here's how you can do it:
self.encoder_level1 = nn.Sequential(
[TransformerBlock(dim=dim, num_heads=heads[0], ffn_expansion_factor=ffn_expansion_factor, bias=bias, LayerNorm_type=LayerNorm_type) for i in range(num_blocks[0])]
)
However, this will actually create a nn.Sequential with a single element, which is the list of TransformerBlock instances, rather than a nn.Sequential with each TransformerBlock as a separate layer. This is not what you want.
To properly create a nn.Sequential with each TransformerBlock as a separate layer without using the * operator, you can use nn.ModuleList inside nn.Sequential:
self.encoder_level1 = nn.Sequential(
nn.ModuleList([TransformerBlock(dim=dim, num_heads=heads[0], ffn_expansion_factor=ffn_expansion_factor, bias=bias, LayerNorm_type=LayerNorm_type) for i in range(num_blocks[0])])
)
But this is not equivalent to the original code, as nn.ModuleList doesn't provide the same sequential execution as nn.Sequential with unpacked layers.
The most straightforward way to avoid the * operator while maintaining the same functionality is to create the layers individually and pass them to nn.Sequential:
blocks = [TransformerBlock(dim=dim, num_heads=heads[0], ffn_expansion_factor=ffn_expansion_factor, bias=bias, LayerNorm_type=LayerNorm_type) for i in range(num_blocks[0])]
self.encoder_level1 = nn.Sequential()
for block in blocks:
self.encoder_level1.add_module(str(len(self.encoder_level1)), block)
This achieves the same result without using the unpacking operator. However, the version with * is more concise and Pythonic, so unless you have a specific reason to avoid it, I'd recommend keeping the original version.