0

I saw the code for a ResNet CNN in Python3 and PyTorch here as follows:

def resnet_block(input_channels, num_channels, num_residuals,
                 first_block=False):
    blk = []
    for i in range(num_residuals):
        if i == 0 and not first_block:
            blk.append(Residual(input_channels, num_channels,
                                use_1x1conv=True, strides=2))
        else:
            blk.append(Residual(num_channels, num_channels))
    return blk

To add the modules, the following code is used-

b2 = nn.Sequential(*resnet_block(64, 64, 2, first_block=True))
b3 = nn.Sequential(*resnet_block(64, 128, 2))
b4 = nn.Sequential(*resnet_block(128, 256, 2))
b5 = nn.Sequential(*resnet_block(256, 512, 2))

What does "*resnet_block()" mean/do?

kmario23
  • 57,311
  • 13
  • 161
  • 150
Arun
  • 2,222
  • 7
  • 43
  • 78
  • It "unpack" list/tuple to positional arguments. It converts `[1,2,3]` to (1,2,3...). You can use it in any position. Also there is `**` operator - the same but for keyword arguments. Look this - https://www.geeksforgeeks.org/packing-and-unpacking-arguments-in-python/ – RandomB Mar 06 '21 at 07:43
  • Does this answer your question? [What does the star operator mean, in a function call?](https://stackoverflow.com/questions/2921847/what-does-the-star-operator-mean-in-a-function-call) – null Mar 06 '21 at 07:55

1 Answers1

2

Basically *iterable is used to unpack the items of an iterable object as positional arguments. In your question resnet_block returns a list, and the items of that list are passed to nn.Sequential rather than the list itself.

null
  • 1,944
  • 1
  • 14
  • 24
  • The iterable unpacking is not restricted only to positional arguments; It can be used in all scenarios: positional-only arguments, keword-only arguments, a combination of positional & keyword arguments. – kmario23 Mar 06 '21 at 10:10